Skip to content

Commit cf6ea46

Browse files
author
luoziyihao
committed
ok LinkedList
1 parent 1c4c321 commit cf6ea46

File tree

1 file changed

+23
-1
lines changed
  • group17/1204187480/code/homework/basic/src/main/java/com/coding/basic

1 file changed

+23
-1
lines changed

group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public class LinkedList implements List {
44

55
private Node head;
66
private int size = 0;
7+
private Iterator iterator = new LinkedListIterator();
8+
79

810
public void add(Object o) {
911
Node newNode = new Node(o, null);
@@ -95,7 +97,7 @@ public Object removeLast() {
9597
}
9698

9799
public Iterator iterator() {
98-
return null;
100+
return iterator;
99101
}
100102

101103
private void checkIndex(int index) {
@@ -122,4 +124,24 @@ public Node(Object data, Node next) {
122124
this.next = next;
123125
}
124126
}
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+
}
125147
}

0 commit comments

Comments
 (0)