-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLists.java
More file actions
229 lines (197 loc) · 5.39 KB
/
Lists.java
File metadata and controls
229 lines (197 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/** Copyright 2011 Fabian Steeg, University of Cologne, http://github.com/spinfo */
package spinfo;
import static org.junit.Assert.assertEquals;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Test;
/** Lists: elementary data structures. */
public class Lists {
/** Low-level, non-OOP list implementation simulating a tuple/record/struct. */
@Test
public void tuple() {
/* Independent nodes: */
Object[] first = new Object[2];
Object[] second = new Object[2];
Object[] third = new Object[2];
/* Containing values: */
first[0] = "first";
second[0] = "second";
third[0] = "third";
/* Linked with pointers: */
first[1] = second;
second[1] = third;
/* Can be traversed: */
System.out.println("List traversal: ");
Object[] current = first;
while (current != null) {
System.out.println(current[0]);
current = (Object[]) current[1];
}
}
/** OOP implementation of a queue, a FIFO list (first in, first out). */
@Test
public void queue() {
Queue queue = new Queue();
/* Enqueue at end: */
queue.enqueue("first");
queue.enqueue("second");
queue.enqueue("third");
/* Iterate: */
System.out.println("Queue traversal: ");
Node current = queue.first;
while (current != null) {
System.out.println(current.value);
current = current.next;
}
/* Dequeue from front: */
assertEquals("first", queue.dequeue());
assertEquals("second", queue.dequeue());
assertEquals("third", queue.dequeue());
assertEquals(null, queue.dequeue());
}
/** A list element: wraps a value and a reference to the next element. */
static class Node {
Object value;
Node next;
Node(Object value) {
this.value = value;
}
}
/** The queue class enforces the restricted FIFO access. */
static class Queue /**/implements Iterable<Object> /* like implements List *//**/{
private Node first;
private Node last;
/** Add an object in constant time. */
public void enqueue(Object value) {
Node n = new Node(value);
if (first == null) {
first = n;
last = first;
} else {
last.next = n;
last = n;
}
}
/** Get an object in constant time. */
public Object dequeue() {
if (first == null)
return null;
Object result = first.value;
first = first.next;
return result;
}
/**/
@Override
public Iterator<Object> iterator() {
return new NodeIterator(first);
}
/**/
}
/** OOP implementation of a stack, a LIFO list (last in, first out). */
@Test
public void stack() {
Stack stack = new Stack();
/* Push on top, i.e. from front: */
stack.push("first");
stack.push("second");
stack.push("third");
/* Iterate: */
System.out.println("Stack traversal: ");
Node current = stack.first;
while (current != null) {
System.out.println(current.value);
current = current.next;
}
/* Pop from top, i.e. from front: */
assertEquals("third", stack.pop());
assertEquals("second", stack.pop());
assertEquals("first", stack.pop());
assertEquals(null, stack.pop());
}
/** The stack class enforces the restricted LIFO access. */
static class Stack /**/implements Iterable<Object> /* like implements List *//**/{
private Node first;
/** Add an object in constant time. */
public void push(Object value) {
Node n = new Node(value);
if (first == null) {
first = n;
} else {
n.next = first;
first = n;
}
}
/** Get an object in constant time. */
public Object pop() {
if (first == null)
return null;
Object result = first.value;
first = first.next;
return result;
}
/**/
@Override
public Iterator<Object> iterator() {
return new NodeIterator(first);
}
/**/
}
/** Common to both: linear order, both are iterable. */
@Test
public void list() {
Queue list = new Queue();
list.enqueue("first");
list.enqueue("second");
list.enqueue("third");
System.out.println("Iterate using Iterator: ");
Iterator<?> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("Iterate using Iterable: ");
for (Object o : list) {
System.out.println(o);
}
}
/** A sequence can be traversed. */
interface List {
Iterator<?> iterator();
}
/** Representation of the stateful traversal. */
interface SimpleIterator {
Object next();
boolean hasNext();
}
/** Iterator implementation based on linked nodes. */
static class NodeIterator implements Iterator<Object> /* like SimpleIterator */{
private Node current;
public NodeIterator(Node first) {
if (first != null)
current = first;
else
throw new IllegalArgumentException();
}
@Override
public Object next() {
if (current.value == null) {
throw new NoSuchElementException();
}
Object next = current.value;
current = current.next;
return next;
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public void remove() {
throw new IllegalStateException("Not implemented");
}
}
@Before
public void line() {
System.out.println();
}
}