Skip to content

Commit beaf0d3

Browse files
committed
Complete data structure assignment
1 parent 4004a03 commit beaf0d3

File tree

13 files changed

+71
-5
lines changed

13 files changed

+71
-5
lines changed

group11/395443277/a1/src/com/coding/basic/ArrayList.java renamed to group11/395443277/data_structure/src/com/coding/basic/ArrayList.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,26 @@ public int size(){
5757
return size;
5858
}
5959

60-
public Iterator iterator(){
61-
return null;
60+
public Iterator iterator (){
61+
return new SeqIterator();
62+
}
63+
64+
private class SeqIterator implements Iterator {
65+
int i = 0;
66+
67+
@Override
68+
public boolean hasNext() {
69+
return i < size;
70+
}
71+
72+
@Override
73+
public Object next() {
74+
if (!hasNext()) {
75+
return null;
76+
}
77+
return elementData[i++];
78+
}
79+
6280
}
6381

6482
}

group11/395443277/a1/src/com/coding/basic/ArrayListTest.java renamed to group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,21 @@ public void testRemove() {
6464
assertEquals(6, list.get(4));
6565
}
6666

67+
@Test
68+
public void testIterator() {
69+
ArrayList list = new ArrayList();
70+
list.add(5);
71+
list.add(4);
72+
list.add(3);
73+
list.add(2);
74+
list.add(1);
75+
76+
Iterator it = list.iterator();
77+
if(it.hasNext()) {
78+
assertEquals(5, it.next());
79+
assertEquals(4, it.next());
80+
}
81+
82+
}
83+
6784
}

group11/395443277/a1/src/com/coding/basic/LinkedList.java renamed to group11/395443277/data_structure/src/com/coding/basic/LinkedList.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.coding.basic;
22

3+
import java.util.NoSuchElementException;
4+
35
public class LinkedList implements List {
4-
56
private Node head;
67

78
public void add(Object o){
@@ -126,11 +127,28 @@ public Object removeLast(){
126127
return target;
127128
}
128129
public Iterator iterator(){
129-
return null;
130+
return new SeqIterator();
130131
}
131132

133+
private class SeqIterator implements Iterator {
134+
Node curr = head;
135+
136+
@Override
137+
public boolean hasNext() {
138+
return curr != null;
139+
}
140+
141+
@Override
142+
public Object next() {
143+
if (!hasNext()) throw new NoSuchElementException();
144+
Object target = curr.data;
145+
curr = curr.next;
146+
return target;
147+
}
148+
149+
}
132150

133-
private static class Node{
151+
private static class Node{
134152
Object data;
135153
Node next;
136154

group11/395443277/a1/src/com/coding/basic/LinkedListTest.java renamed to group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,17 @@ public void testRemoveLast() {
111111
assertEquals(3, list.removeLast());
112112
}
113113

114+
@Test
115+
public void testIterator() {
116+
LinkedList list = new LinkedList();
117+
list.add(4);
118+
list.add(3);
119+
list.add(2);
120+
121+
Iterator it = list.iterator();
122+
123+
assertEquals(4, it.next());
124+
assertEquals(3, it.next());
125+
assertEquals(2, it.next());
126+
}
114127
}

0 commit comments

Comments
 (0)