-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.java
209 lines (170 loc) · 5.57 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
200
201
202
203
204
205
206
207
208
209
/* *****************************************************************************
* Name: Sebastian Zapata Mardini
* Date: 06/04/2019
* Description: Deque is a generalization of a stack and a queue that supports
* adding and removing items from either the front or the back of the data
* structure.
**************************************************************************** */
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private Node first;
private Node last;
private int n;
// Nested class to define nodes
private class Node {
Item item;
Node previous;
Node next;
}
// Returns `true if the Deque does not have any nodes otherwise this method
// returns `false`.
public boolean isEmpty() {
return first == null && last == null;
}
// Returns the number of items in the Deque.
public int size() {
return n;
}
// Add the item to front of the Deque.
public void addFirst(Item item) {
if (item == null) {
throw new IllegalArgumentException("Invalid item to be inserted!");
}
Node oldFirst = first;
Node newFirst = new Node();
newFirst.item = item;
newFirst.previous = null;
if (isEmpty()) {
newFirst.next = null;
first = last = newFirst;
} else {
newFirst.next = oldFirst;
oldFirst.previous = newFirst;
first = newFirst;
}
n++;
}
// Add the item to the end of the Deque
public void addLast(Item item) {
if (item == null) {
throw new IllegalArgumentException("Invalid item to be inserted!");
}
Node oldLast = last;
Node newLast = new Node();
newLast.item = item;
newLast.next = null;
if (isEmpty()) {
newLast.previous = null;
last = newLast;
first = last;
} else {
newLast.previous = oldLast;
oldLast.next = newLast;
last = newLast;
}
n++;
}
// Remove and return the item from the front.
public Item removeFirst() {
if (isEmpty()) {
throw new NoSuchElementException("The Deque is empty!");
}
Node target = first;
Node successor = first.next;
// If it is not the last node in the Deque and we have a successor update
// the `previous reference reference from the `successor` to be `null` `
// and update `first` to be equal to the new successor. Otherwise set
// `first` and `last` equal to `null`.
if (successor == null) {
last = null;
first = null;
} else {
successor.previous = null;
first = successor;
}
n--;
return target.item;
}
public Item removeLast() {
if (isEmpty()) {
throw new NoSuchElementException("The Deque is empty!");
}
Node target = last;
Node successor = last.previous;
// If it is not the last node in the Deque and we have a successor update
// the `next` reference from the `successor` to be `null` and update
// `last` to be equal to the new `successor`. Otherwise set `first` and
// `last` equal to `null`.
if (successor == null) {
first = null;
last = null;
} else {
successor.next = null;
last = successor;
}
n--;
return target.item;
}
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException("This method is not implemented.");
}
public Item next() {
if (current == null) {
throw new NoSuchElementException("No more items to be returned.");
}
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) {
Deque<Integer> deque = new Deque<Integer>();
deque.addLast(1);
deque.addLast(2);
deque.addLast(3);
deque.addLast(4);
deque.addLast(5);
deque.addFirst(1);
deque.addFirst(2);
deque.addFirst(3);
deque.addFirst(4);
deque.addFirst(5);
Iterator<Integer> i = deque.iterator();
System.out.println("Deque size: " + deque.size());
while (i.hasNext()) {
int n = i.next();
System.out.println(n);
}
// Remove 5s
deque.removeFirst();
deque.removeLast();
System.out.println("Deque size: " + deque.size());
// Remove 4s
deque.removeFirst();
deque.removeLast();
System.out.println("Deque size: " + deque.size());
// Remove 3s
deque.removeFirst();
deque.removeLast();
System.out.println("Deque size: " + deque.size());
// Remove 2s
deque.removeFirst();
deque.removeLast();
System.out.println("Deque size: " + deque.size());
// Remove 1s
deque.removeFirst();
deque.removeLast();
System.out.println("Deque size: " + deque.size());
// Raise a java.util.NoSuchElementException exception. The Deque is empty
deque.removeFirst();
}
}