-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.java
199 lines (156 loc) · 4.53 KB
/
Deque.java
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
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private Node<Item> first, last;
private int n;
private static class Node<Item> {
private Item item;
private Node<Item> prev;
private Node<Item> next;
}
// construct an empty deque
public Deque() {
first = null;
last = null;
n = 0;
}
// is the deque empty?
public boolean isEmpty() {
return first == null || last == null;
}
// return the number of items on the deque
public int size() {
return n;
}
// add the item to the front
public void addFirst(Item item) {
validateAdd(item);
Node<Item> oldFirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldFirst;
first.prev = null;
n += 1;
if (size() == 1) {
last = first;
} else if (size() == 2) {
first.next = last;
last.prev = first;
} else {
oldFirst.prev = first;
}
}
// add the item to the back
public void addLast(Item item) {
validateAdd(item);
Node<Item> oldLast = last;
last = new Node<Item>();
last.item = item;
last.prev = oldLast;
last.next = null;
n += 1;
if (size() == 1) {
first = last;
}
else if (size() == 2) {
first.next = last;
last.prev = first;
} else {
oldLast.next = last;
}
}
// remove and return the item from the front
public Item removeFirst() {
validateRemove();
Node<Item> oldFirst = first;
Item item = oldFirst.item;
first = oldFirst.next;
n -= 1;
if (size() == 0) {
first = null;
last = null;
}
return item;
}
// remove and return the item from the back
public Item removeLast() {
validateRemove();
Node<Item> oldLast = last;
Item item = oldLast.item;
oldLast.next = null;
last = oldLast.prev;
n -= 1;
if (size() == 0) {
first = null;
last = null;
}
return item;
}
// return an iterator over items in order from front to back
public Iterator<Item> iterator() {
return new ListIterator(first);
}
private class ListIterator implements Iterator<Item> {
private Node<Item> current;
public ListIterator(Node<Item> firstNode) {
current = firstNode;
}
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException("unsupported method");
}
public Item next() {
validate();
Item item = current.item;
current = current.next;
return item;
}
private void validate() {
if (!hasNext()) {
throw new NoSuchElementException();
}
}
}
private void validateAdd(Item item) {
if (item == null) {
throw new IllegalArgumentException();
}
}
private void validateRemove() {
if (isEmpty()) {
throw new NoSuchElementException();
}
}
// unit testing (required)
public static void main(String[] args) {
Deque<String> deque = new Deque<String>();
deque.addFirst("a");
StdOut.println(deque.removeLast() + " ");
StdOut.println("(" + deque.size() + " left on stack)");
deque.addLast("d");
StdOut.println(deque.removeFirst() + " ");
StdOut.println("(" + deque.size() + " left on stack)");
for (String item : deque) {
StdOut.println("item: " + item);
}
deque.addFirst("b");
deque.addLast("c");
deque.addLast("d");
deque.addFirst("a");
StdOut.println("(" + deque.size() + " left on stack)");
Iterator<String> i = deque.iterator();
while (i.hasNext()) {
String s = i.next();
StdOut.println("s: " + s);
}
StdOut.println(deque.removeLast() + " ");
StdOut.println(deque.removeFirst() + " ");
StdOut.println(deque.removeFirst() + " ");
StdOut.println(deque.removeFirst() + " ");
StdOut.println(deque.removeFirst() + " ");
}
}