-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDLLDeque.java
241 lines (199 loc) · 6.46 KB
/
DLLDeque.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
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
/*****************************************************
* class DLLQueue
* uses singly-linked nodes to implement a QUEUE
* (a collection with FIFO property)
*
* -------------------------------
* end | ---> Q U E U E ---> | front
* -------------------------------
*
* Linkages oriented against queue flow to facilitate
* O(1) en/dequeue.
*
* N <- N <- ... <- N <- N
* _end -^ ^- _front
*
******************************************************/
//import java.util.NoSuchElementException;
//import java.lang.RuntimeException;
public class DLLDeque<T> implements Deque<T>
{
private DLLNode<T> _first, _last; //Constructor(value,previous,next)
private int _size;
// default constructor creates an empty queue
public DLLDeque()
{
_first = _last = new DLLNode<T>(null,null,null);
_size = 0;
}
/*****NEW INTERFACE METHODS*****/
//adds a value to the beginning of the deque
//if the deque was previously empty, sets both first and last to this new node
//if not then it sets first to the new node
public void addFirst (T val) {
if (isEmpty()) {
_first = new DLLNode(val, null, null);
_last = _first;
_size++;
return;
} else {
_first.setPrev(new DLLNode<T>(val, null, _first));
_first = _first.getPrev();
_size++;
return;
}
}
//adds a value to the ending of the deque
//if the deque was previously empty, sets both first and last to this new node
//if not then it sets last to the new node
public void addLast(T val) {
if (isEmpty()) {
_last = new DLLNode(val, null, null);
_first = _last;
_size++;
return;
} else {
_last.setNext(new DLLNode<T>(val, _last, null));
_last = _last.getNext();
_size++;
return;
}
}
//removes a value from the beginning of the deque
//if the deque is empty, it throws an exception
//if not then it sets first to the following node
public T removeFirst() {
if (isEmpty()){
//throw exception when someone tries to remove from an empty queue
throw new IndexOutOfBoundsException("There is nothing to remove.");
}
T rtn = _first.getCargo();
_first = _first.getNext();
_first.setPrev(null);
_size--;
return rtn;
}
//removes a value from the ending of the deque
//if the deque is empty, it throws an exception
//if not then it sets last to the previous node
public T removeLast() {
if (isEmpty()) {
//throw exception when someone tries to remove from an empty queue
throw new IndexOutOfBoundsException("There is nothing to remove.");
}
T rtn = _last.getCargo();
_last = _last.getPrev();
_last.setNext(null);
_size--;
return rtn;
}
//returns the first value of the deque
public T peekFirst() {
return _first.getCargo();
}
//returns the last value of the deque
public T peekLast() {
return _last.getCargo();
}
//returns the size of the deque using a variable that is incremented when necessary throughout the class
public int size() {
return _size;
}
/*****NEW INTERFACE METHODS*****/
/*********PREVIOUS METHODS***********^
// means of adding a thing to the collection
public void enqueue( T enQVal )
{
//special case: when enqueuing to an empty list,
//make _front && _end point to same node
if ( isEmpty() ) {
_front = _end = new DLLNode<T>( enQVal, null );
}
else {
// Q: Why is this a bad idea:
// _end = new LLNode<T>( enQVal, null );
_end.setNext( new DLLNode<T>( enQVal, null ) );
_end = _end.getNext();
}
}//O(1)
// means of removing a thing from the collection
// remove and return thing at front of queue
// assume _queue ! empty
public T dequeue()
{
T foo = _front.getValue();
_front = _front.getNext();
//check for emptiness
if ( _front == null )
_end = null;
return foo;
}//O(1)
// means of peeking at thing next in line for removal
public T peekFront()
{
return _front.getValue();
}//O(1)
^***************PREVIOUS METHODS************/
/***********PREVIOUS METHODS TO BE USED******/
//returns whether or not the size is 0
public boolean isEmpty()
{
return _size == 0;
}//O(1)
// print each node, separated by spaces
public String toString()
{
String foo = "";
DLLNode<T> tmp = _first;
while ( tmp != null ) {
foo += tmp.getCargo() + " ";
tmp = tmp.getNext();
}
return foo;
}//O(n)
/***********PREVIOUS METHODS TO BE USED******/
public static void main( String[] args )
{
DLLDeque<String> DLLDequelJ = new DLLDeque<String>();
System.out.println("\nsize");
System.out.println(DLLDequelJ.size());
System.out.println("\nnow enqueuing thrice...");
DLLDequelJ.addFirst("James");
DLLDequelJ.addFirst("Todd");
DLLDequelJ.addFirst("Smith");
System.out.println( DLLDequelJ ); //for testing toString()...
System.out.println("\npeeking first");
System.out.println(DLLDequelJ.peekFirst());
System.out.println("\npeeking last");
System.out.println(DLLDequelJ.peekLast());
System.out.println("\nnow enqueuing thrice...");
DLLDequelJ.addLast("James");
DLLDequelJ.addLast("Todd");
DLLDequelJ.addLast("Smith");
System.out.println( DLLDequelJ ); //for testing toString()...
System.out.println("\npeeking first");
System.out.println(DLLDequelJ.peekFirst());
System.out.println("\npeeking last");
System.out.println(DLLDequelJ.peekLast());
System.out.println("\nsize");
System.out.println(DLLDequelJ.size());
System.out.println("\nnow testing toString()...");
System.out.println( DLLDequelJ ); //for testing toString()...
System.out.println("\nnow dequeuing thrice...");
System.out.println( DLLDequelJ.removeFirst() );
System.out.println( DLLDequelJ.removeFirst() );
System.out.println( DLLDequelJ.removeFirst() );
System.out.println("\nnow testing toString()...");
System.out.println( DLLDequelJ ); //for testing toString()...
System.out.println("\nnow dequeuing thrice...");
System.out.println( DLLDequelJ.removeLast() );
System.out.println( DLLDequelJ.removeLast() );
System.out.println( DLLDequelJ.removeLast() );
System.out.println("\nsize");
System.out.println(DLLDequelJ.size());
System.out.println("\nDequeuing from empty queue should yield error...");
System.out.println( DLLDequelJ.removeFirst() );
/*v~~~~~~~~~~~~~~MAKE MORE~~~~~~~~~~~~~~v
^~~~~~~~~~~~~~~~AWESOME~~~~~~~~~~~~~~~^*/
}//end main
}//end class DLLQueue