File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
group17/1204187480/code/homework/basic/src/main/java/com/coding/basic Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ public class LinkedList implements List {
4
4
5
5
private Node head ;
6
6
private int size = 0 ;
7
+ private Iterator iterator = new LinkedListIterator ();
8
+
7
9
8
10
public void add (Object o ) {
9
11
Node newNode = new Node (o , null );
@@ -95,7 +97,7 @@ public Object removeLast() {
95
97
}
96
98
97
99
public Iterator iterator () {
98
- return null ;
100
+ return iterator ;
99
101
}
100
102
101
103
private void checkIndex (int index ) {
@@ -122,4 +124,24 @@ public Node(Object data, Node next) {
122
124
this .next = next ;
123
125
}
124
126
}
127
+
128
+ private class LinkedListIterator implements Iterator {
129
+
130
+ private Node next ;
131
+
132
+ @ Override
133
+ public boolean hasNext () {
134
+ return next != null ;
135
+ }
136
+
137
+ @ Override
138
+ public Object next () {
139
+ if (next == null ) {
140
+ throw new IndexOutOfBoundsException ("there is no node in list" );
141
+ }
142
+ Node ret = next ;
143
+ next = next .next ;
144
+ return ret .data ;
145
+ }
146
+ }
125
147
}
You can’t perform that action at this time.
0 commit comments