Repository navigation
Expand file tree
/
Copy pathDeque.java
More file actions
259 lines (232 loc) · 4.43 KB
/
Copy pathDeque.java
File metadata and controls
259 lines (232 loc) · 4.43 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package a02;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
*
* @author Gavin Rosenvall & Khoi Tran
*
* @param <Item>
*/
public class Deque<Item> implements Iterable<Item>
{
private Node head;
private Node tail;
private int n; // number of words in the list
/**
* Node of LinkedList that stores the item and references to the next and
* previous node.
*/
private class Node
{
private Item item;
private Node next;
private Node previous;
}
/**
* Constructs an empty deque.
*/
public Deque()
{
n = 0;
head = null;
tail = null;
}
/**
* Checks whether the deque is empty.
*
* @return
*/
public boolean isEmpty()
{
if (n == 0)
{
return true;
} else
return false;
}
/**
* Returns the number of items in the deque.
*
* @return
*/
public int size()
{
return n;
}
/**
* Inserts the item included in the arguments to the front.
*
* @param item
*/
public void addFirst(Item item)
{
checkNullPointerException(item);
Node newNode = new Node();
newNode.item = item;
if (isEmpty())
{
createNewDeque(newNode);
} else
{
head.previous = newNode;
newNode.next = head;
newNode.previous = null;
head = newNode;
}
n++;
}
/**
* Inserts the item included on the arguments at the end.
*
* @param item
*/
public void addLast(Item item)
{
checkNullPointerException(item);
Node newNode = new Node();
newNode.item = item;
if (isEmpty())
{
createNewDeque(newNode);
} else
{
tail.next = newNode;
newNode.previous = tail;
newNode.next = null;
tail = newNode;
}
n++;
}
/**
* Deletes the item at the front.
*
* @return
*/
public Item removeFirst()
{
checkNoSuchElementException();
Item removedItem = head.item;
if (n == 1)
{
destroyDeque();
} else
{
head = head.next;
head.previous = null;
}
n--;
return removedItem;
}
/**
* Deletes the item at the end.
*
* @return
*/
public Item removeLast()
{
checkNoSuchElementException();
Item removedItem = tail.item;
if (n == 1)
{
destroyDeque();
} else
{
tail = tail.previous;
tail.next = null;
}
n--;
return removedItem;
}
/**
* Returns an iterator over items in order from front to end. Throws an
* UnsupportedOperationException if client tries to remove. Throws a
* NoSuchElementException if the next node is null.
*
* @return
*/
public Iterator<Item> iterator()
{
return new DequeIterator();
}
private class DequeIterator implements Iterator<Item>
{
private Node current = head;
@Override
public boolean hasNext()
{
return current != null;
}
@Override
public Item next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
Item item = current.item;
current = current.next;
return item;
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}
// Added these private methods to avoid code duplication.
/**
* Checks the value of the introduced item to see whether it is null. If so,
* it throws a null pointer exception.
*
* @param item
*/
private void checkNullPointerException(Item item)
{
if (item == null)
{
throw new NullPointerException("The item's value can not be null.");
}
}
/**
* Checks if the deque has any elements to remove and/or iterate. If empty,
* it throws a no such element exception.
*/
private void checkNoSuchElementException()
{
if (isEmpty())
{
throw new NoSuchElementException("There are no elements in the deque to remove");
}
}
/**
* Assigns the head and tail values to the newly created node and makes all
* links null.
*
* @param newNode
*/
private void createNewDeque(Node newNode)
{
head = newNode;
tail = newNode;
head.previous = null;
head.next = null;
tail.previous = null;
tail.next = null;
}
/**
* Removes all of the values linked to the last remaining node as well as
* the value of the node itself. This is to prevent any loitering data.
*/
private void destroyDeque()
{
head = null;
head.next = null;
head.previous = null;
tail = null;
tail.next = null;
tail.previous = null;
}
public static void main(String[] args)
{
}
}