13
13
*/
14
14
public class SingleLinkedList <E > implements LinkedList <E > {
15
15
16
- Node <E > head ;
16
+ SingleLinkedNode <E > head ;
17
17
int size ;
18
18
19
19
@ Override
20
20
public boolean add (E item ) {
21
- Node <E > newNode = new Node <>(item , null );
21
+ SingleLinkedNode <E > newNode = new SingleLinkedNode <>(item , null );
22
22
if (head == null ) { // list empty
23
23
head = newNode ;
24
24
} else { // add to the end of list
25
- Node <E > curr = head ;
25
+ SingleLinkedNode <E > curr = head ;
26
26
while (curr .next != null ) {
27
27
curr = curr .next ;
28
28
}
@@ -39,8 +39,8 @@ public boolean add(int index, E item) {
39
39
if (index == 0 ) { // add at first
40
40
addFirst (item );
41
41
} else { // add at any other location
42
- Node <E > nodeAtPrevIndex = getPredecessorNode (index );
43
- Node <E > newNode = new Node <>(item , nodeAtPrevIndex .next );
42
+ SingleLinkedNode <E > nodeAtPrevIndex = getPredecessorNode (index );
43
+ SingleLinkedNode <E > newNode = new SingleLinkedNode <>(item , nodeAtPrevIndex .next );
44
44
nodeAtPrevIndex .next = newNode ;
45
45
size ++;
46
46
}
@@ -49,7 +49,7 @@ public boolean add(int index, E item) {
49
49
50
50
@ Override
51
51
public void addFirst (E item ) {
52
- Node <E > newNode = new Node <>(item , head );
52
+ SingleLinkedNode <E > newNode = new SingleLinkedNode <>(item , head );
53
53
head = newNode ;
54
54
size ++;
55
55
}
@@ -104,8 +104,8 @@ public E remove() {
104
104
public E remove (int index ) {
105
105
isLinkedListEmpty ();
106
106
107
- Node <E > prevNode = getPredecessorNode (index );
108
- Node <E > delNode ;
107
+ SingleLinkedNode <E > prevNode = getPredecessorNode (index );
108
+ SingleLinkedNode <E > delNode ;
109
109
if (prevNode == null ) { // index = 0
110
110
delNode = head ;
111
111
head = head .next ;
@@ -125,7 +125,7 @@ public boolean removeItem(E item) {
125
125
126
126
if (!contains (item )) return false ;
127
127
128
- Node <E > prevNode = getPredecessorNode (item );
128
+ SingleLinkedNode <E > prevNode = getPredecessorNode (item );
129
129
if (prevNode == null ) { // index = 0
130
130
head = head .next ;
131
131
size --;
@@ -138,7 +138,7 @@ public boolean removeItem(E item) {
138
138
139
139
@ Override
140
140
public E set (int index , E item ) {
141
- Node <E > node = getNode (index );
141
+ SingleLinkedNode <E > node = getNode (index );
142
142
node .item = item ;
143
143
return node .item ;
144
144
}
@@ -150,7 +150,7 @@ public int size() {
150
150
151
151
@ Override
152
152
public void printList () {
153
- Node <E > curr = head ;
153
+ SingleLinkedNode <E > curr = head ;
154
154
out .print ("[" );
155
155
if (curr == null ) {
156
156
out .println ("]" );
@@ -163,10 +163,10 @@ public void printList() {
163
163
out .println (curr .item + "]" );
164
164
}
165
165
166
- private Node <E > getPredecessorNode (int index ) {
166
+ private SingleLinkedNode <E > getPredecessorNode (int index ) {
167
167
isIndexOutOfBounds (index );
168
168
169
- Node <E > curr = head ;
169
+ SingleLinkedNode <E > curr = head ;
170
170
int i = 0 ;
171
171
while (i < index - 1 ) {
172
172
curr = curr .next ;
@@ -175,9 +175,9 @@ private Node<E> getPredecessorNode(int index) {
175
175
return (index == 0 ) ? null : curr ;
176
176
}
177
177
178
- private Node <E > getPredecessorNode (E item ) {
179
- Node <E > prev = null ;
180
- Node <E > curr = head ;
178
+ private SingleLinkedNode <E > getPredecessorNode (E item ) {
179
+ SingleLinkedNode <E > prev = null ;
180
+ SingleLinkedNode <E > curr = head ;
181
181
if (item == null ) {
182
182
while (curr != null ) {
183
183
if (curr .item == item ) { // when item is null, use == rather than equals()
@@ -198,10 +198,10 @@ private Node<E> getPredecessorNode(E item) {
198
198
return null ;
199
199
}
200
200
201
- private Node <E > getNode (int index ) {
201
+ protected SingleLinkedNode <E > getNode (int index ) {
202
202
isIndexOutOfBounds (index );
203
203
204
- Node <E > curr = head ;
204
+ SingleLinkedNode <E > curr = head ;
205
205
int i = 0 ;
206
206
while (i < index ) {
207
207
curr = curr .next ;
@@ -210,8 +210,8 @@ private Node<E> getNode(int index) {
210
210
return curr ;
211
211
}
212
212
213
- private Node <E > getNode (E item ) {
214
- Node <E > curr = head ;
213
+ protected SingleLinkedNode <E > getNode (E item ) {
214
+ SingleLinkedNode <E > curr = head ;
215
215
if (item == null ) {
216
216
while (curr != null ) { // when item is null, use == rather than equals()
217
217
if (curr .item == item ) {
@@ -241,21 +241,4 @@ private void isIndexOutOfBounds(int index) {
241
241
throw new IndexOutOfBoundsException ("Index must be less than " + size );
242
242
}
243
243
}
244
-
245
- private class Node <E > {
246
- E item ;
247
- Node <E > next ;
248
-
249
- Node (E item , Node <E > next ) {
250
- this .item = item ;
251
- this .next = next ;
252
- }
253
-
254
- Node (Node <E > node ) {
255
- if (node == null ) return ;
256
-
257
- this .item = node .item ;
258
- this .next = node .next ;
259
- }
260
- }
261
244
}
0 commit comments