1
+ // Student: Griffin Gowdey.
2
+ // Instructor: Daniel Goodman.
3
+ // Class Number: ITSE1479-86039.
4
+ // Class Name: Java Programming.
5
+ // Semester: Fall 2020.
6
+ // Lesson 16.
7
+
8
+ /*
9
+ Assignment:
10
+ Add a method reverse to our LinkedList implementation that
11
+ reverses the links in a list. Implement this method by directly
12
+ rerouting the links, not by using an iterator.
13
+ */
14
+
15
+ import java .util .NoSuchElementException ;
16
+
17
+ public class Lesson16
18
+ {
19
+ /*
20
+ A linked list is a sequence of nodes with efficient
21
+ element insertion and removal. This class
22
+ contains a subset of the methods of the standard
23
+ java.util.LinkedList class.
24
+ */
25
+ public class LinkedList
26
+ {
27
+ private Node first ;
28
+
29
+ // Constructs an empty linked list.
30
+ public LinkedList ()
31
+ {
32
+ first = null ;
33
+ }
34
+
35
+ // Reverses all elements in a linked list.
36
+ public void reverse ()
37
+ {
38
+ if (first == null ) return ;
39
+ Node previous = first ;
40
+ Node current = first .next ;
41
+ first .next = null ;
42
+ while (current != null )
43
+ {
44
+ Node next = current .next ;
45
+ current .next = previous ;
46
+ previous = current ;
47
+ current = next ;
48
+ }
49
+ first = previous ;
50
+ }
51
+
52
+ /* Returns the first element in the linked list.
53
+ @return the first element in the linked list. */
54
+ public Object getFirst ()
55
+ {
56
+ if (first == null )
57
+ throw new NoSuchElementException ();
58
+ return first .data ;
59
+ }
60
+
61
+ /* Removes the first element in the linked list.
62
+ @return the removed element. */
63
+ public Object removeFirst ()
64
+ {
65
+ if (first == null )
66
+ throw new NoSuchElementException ();
67
+ Object element = first .data ;
68
+ first = first .next ;
69
+ return element ;
70
+ }
71
+
72
+ /* Adds an element to the front of the linked list.
73
+ @param element the element to add. */
74
+ public void addFirst (Object element )
75
+ {
76
+ Node newNode = new Node ();
77
+ newNode .data = element ;
78
+ newNode .next = first ;
79
+ first = newNode ;
80
+ }
81
+
82
+ /* Returns an iterator for iterating through this list.
83
+ @return an iterator for iterating through this list. */
84
+ public ListIterator listIterator ()
85
+ {
86
+ return new LinkedListIterator ();
87
+ }
88
+
89
+ private class Node
90
+ {
91
+ public Object data ;
92
+ public Node next ;
93
+ }
94
+
95
+ private class LinkedListIterator implements ListIterator
96
+ {
97
+ private Node position ;
98
+ private Node previous ;
99
+
100
+ /* Constructs an iterator that points to the front
101
+ of the linked list. */
102
+ public LinkedListIterator ()
103
+ {
104
+ position = null ;
105
+ previous = null ;
106
+ }
107
+
108
+ /* Moves the iterator past the next element.
109
+ @return the traversed element. */
110
+ public Object next ()
111
+ {
112
+ if (!hasNext ())
113
+ throw new NoSuchElementException ();
114
+ previous = position ;
115
+
116
+ if (position == null )
117
+ position = first ;
118
+ else
119
+ position = position .next ;
120
+
121
+ return position .data ;
122
+ }
123
+
124
+ /* Tests if there is an element after the iterator position.
125
+ @return true if there is an element after the iterator position. */
126
+ public boolean hasNext ()
127
+ {
128
+ if (position == null )
129
+ return first != null ;
130
+ else
131
+ return position .next != null ;
132
+ }
133
+
134
+ /*
135
+ Adds an element before the iterator position
136
+ and moves the iterator past the inserted element.
137
+ @param element the element to add.
138
+ */
139
+ public void add (Object element )
140
+ {
141
+ if (position == null )
142
+ {
143
+ addFirst (element );
144
+ position = first ;
145
+ }
146
+ else
147
+ {
148
+ Node newNode = new Node ();
149
+ newNode .data = element ;
150
+ newNode .next = position .next ;
151
+ position .next = newNode ;
152
+ position = newNode ;
153
+ }
154
+ previous = position ;
155
+ }
156
+
157
+ /* Removes the last traversed element. This method may
158
+ only be called after a call to the next() method. */
159
+ public void remove ()
160
+ {
161
+ if (previous == position )
162
+ throw new IllegalStateException ();
163
+
164
+ if (position == first )
165
+ {
166
+ removeFirst ();
167
+ }
168
+ else
169
+ {
170
+ previous .next = position .next ;
171
+ }
172
+ position = previous ;
173
+ }
174
+
175
+ /* Sets the last traversed element to a different value.
176
+ @param element the element to set. */
177
+ public void set (Object element )
178
+ {
179
+ if (position == null )
180
+ throw new NoSuchElementException ();
181
+ position .data = element ;
182
+ }
183
+ }
184
+ }
185
+ }
0 commit comments