-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeque.java
More file actions
247 lines (231 loc) · 6.87 KB
/
Deque.java
File metadata and controls
247 lines (231 loc) · 6.87 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
/*************************************************************************
* Name: Nayan Bhat
* Login: nayanb@princeton.edu
* Precept: P06B
*
* Partner Name: Chris Hay
* Partner Login: chay@princeton.edu
* Partner Precept: P06B
*
* Compilation: javac Deque.java
* Execution: java Deque
* Dependencies:
*
* Description:
*
**************************************************************************/
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>
{
private Node first; //first item in Deque
private Node last; //last item in Deque
private int size; //number of items in Deque
private int numAdded; //keeps track of total number of items added
//inner class for Node Object implementation of Deque
private class Node
{
private Node before; //link backward
private Node next; //link forward
private Item item; //Item stored in Node
//constructor for Node
Node(Item item)
{
this.item = item;
}
}
//construct an empty deque
public Deque()
{
//initialize size variable
size = 0;
}
//returns true if deque is empty
public boolean isEmpty()
{
return size == 0;
}
//returns number of elements in deque
public int size()
{
return size;
}
//adds an element to the beginning of deque
public void addFirst(Item item)
{
//makes sure item is not null object
checkNull(item);
//creates a temporary reference to Node in first
//so that it isn't lost
Node temp = first;
//create a new Node to store the new item at beginning
//of deque
first = new Node(item);
//set next pointer to refer to temp
first.next = temp;
//if it's the first item inserted, make last point to it
if (size() == 0)
last = first;
// otherwise set a pointer back to the new item from what
// was previously first
else
first.next.before = first;
size++;
numAdded++;
}
// adds item to the back of the deque
public void addLast(Item item)
{
// throws an error if item is null
checkNull(item);
// if it's the first item, make both last and first point to it
if (size() == 0)
{
last = new Node(item);
first = last;
}
// make the last item point to the node being inserted
else
{
last.next = new Node(item);
// make the new node point back to the previous
last.next.before = last;
// update last
last = last.next;
}
size++;
numAdded++;
}
//checks that inputted item is not null
private void checkNull(Item item)
{
if (item == null)
throw new NullPointerException();
}
//checks that there is at least one element in the deque
private void checkEmpty()
{
if (size() == 0)
throw new java.util.NoSuchElementException();
}
//removes the first item from the deque
public Item removeFirst()
{
//throw error if deque is empty
checkEmpty();
//store item of first in a temporary object
Item temp = first.item;
// check if it's not the last item to avoid nullPointerExceptions
if (first.next != null)
{
//set the pointer back from second node to null (prevent loitering)
first.next.before = null;
//set first to point to what was originally the second
//node
first = first.next;
}
// if it's the last item to remove, set first and last to be null
else
{
first = null;
last = null;
}
//decrement size by one
size--;
//return the item stored in temp
return temp;
}
//removes the last element from the deque
public Item removeLast()
{
//throw error if deque is empty
checkEmpty();
//store item of last in a temporary object
Item temp = last.item;
// check if it's not the last item to avoid nullPointerExceptions
if(last.before != null)
{
//set pointer forward from second to last element to null
//to prevent loitering
last.before.next = null;
//update last
last = last.before;
}
// if it's the last item to remove, set first and last to be null
else
{
last = null;
first = null;
}
//decrement size by one
size--;
//return the item stored in temp
return temp;
}
// The Iterator code below was adapted from "Stack.java"
// on the booksite:
// http://algs4.cs.princeton.edu/13stacks/Stack.java.html
// return an iterator over items in order from front to end
public Iterator<Item> iterator()
{
return new ForwardIterator(first);
}
// iterates forward through deque
private class ForwardIterator implements Iterator<Item>
{
private Node temp; // current node
// constructs iterator and sets current to first
ForwardIterator(Node first)
{
temp = first;
}
// checks if there's a next item available
public boolean hasNext()
{
return temp != null;
}
// throws an error if user tries to remove an item during iteration
public void remove()
{
throw new UnsupportedOperationException();
}
// returns the next item
public Item next()
{
if (hasNext())
{
// stores item in the current node
Item item = temp.item;
// updates the current node to the next
temp = temp.next;
return item;
}
// throws an exception if it reaches the end
throw new NoSuchElementException();
}
}
// unit tests
public static void main(String[] args)
{
Deque<String> d = new Deque<String>();
d.addLast("hi");
d.addLast("Chris");
d.addFirst("What's");
for (String e: d)
StdOut.println(e);
StdOut.println(d.removeLast());
StdOut.println(d.removeFirst());
StdOut.println(d.removeFirst());
Integer trash = null;
Deque<Integer> b = new Deque<Integer>();
for(int i = 0; i < 5; i++) {
double k = Math.random();
if (k < 0.5)
b.addFirst(i);
else
trash = b.removeFirst();
}
for(Integer e: b)
StdOut.println(e);
}
}