- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueAllInOne.java
More file actions
Latest commit
375 lines (331 loc) · 8.25 KB
/
Copy pathQueueAllInOne.java
File metadata and controls
375 lines (331 loc) · 8.25 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Array Based implementation of Queue.
*
* Implementation is based on Circular Array
*/
classQueue_Arr {
// Array to hold the stack
privateint[] arr;
// front pointer to array
privateintfront;
// rear pointer to array
privateintrear;
// total size of the array
privateinttotal_size;
publicQueue_Arr(intsize) {
arr = newint[size];
front = -1;
rear = -1;
total_size = size;
}
// Enqueue operation
publicvoidenqueue(intval) {
if(isFull()) return;
// IF empty, create a node, i.e, set front and rear to a index 0.
elseif(isEmpty()) front = rear = 0;
// if not empty, increment rear, modulus over total size is done to make it work even when the pointer moves to the frontend of array
elserear = (rear+1)%total_size;
// Set the current index to the given value
arr[rear] = val;
}
publicvoiddequeue() {
if(isEmpty()) return;
// if only element is present in the queue, then set front and rear to -1
elseif(front==rear) front = rear = -1;
// increment front
elsefront = (front +1)%total_size;
}
publicbooleanisFull() {
// Check if queue is full
return (rear+1)%total_size == front;
}
publicbooleanisEmpty() {
// Check if queue is empty
returnfront == -1 && rear==-1;
}
}
/*
* Linked list implementation of Queue, Relatively easier to implement than the array based implementation.
*/
classQueue_Link2 {
// Pointer to both the head and tail of the linkedlist
privateLink2head;
privateLink2tail;
privateintsize;
publicQueue_Link2() {
size = 0;
head = null;
tail = null;
}
// Enqueue
publicvoidenqueue(intval) {
Link2newNode = newLink2(val);
if(isFull()) return;
elseif(isEmpty()) head = newNode;
elsetail.next = newNode;
tail = newNode;
size++;
}
// Dequeue
publicvoiddequeue() {
if(isEmpty()) return;
elseif(head.next==tail) head = tail = null;
elsehead = head.next;
size--;
}
// number of elements in queue
publicintsize() {
returnsize;
}
//Check if queue is full
publicbooleanisFull() {
returnfalse;
}
// Check if queue is empty
publicbooleanisEmpty() {
returnhead == null;
}
}
/*
* Deque Array based implementation. Deque allows insertion and deletion from both ends.
*
* InsertFirst - Inserts from front
* DeleteFirst - Deletes from front
*
* InsertRear - Inserts from rear
* DeleteRear - Deletes from rear
*
* If insertion is done from one end and deletion is done from other end, then it will act as a Queue
* If insertion and deletion is done from same end, then it will act as a Stack
*
*/
classDeque {
// Array holding deque
privateint[] arr;
// points to front and back of the array resp
privateintfront;
privateintrear;
// Size of the deque
privateintmaxSize;
publicDeque(intsize) {
arr = newint[size];
maxSize = size;
// Initialized to -1
front = -1;
rear =-1;
}
// Inserting from the rear end
publicvoidinsertRear(intval) {
if(isFull()) return;
// If deque is empty, assign a valid position for both pointers to start inserting
elseif(isEmpty()) front = rear = 0;
// Else, just increment the rear, as we are inserting at rear.
elserear = (rear+1)%maxSize;
arr[rear] = val;
}
// Deleting from rear end
publicvoiddeleteRear() {
if(isEmpty()) return;
// If there is only element in the Deque, then deleting it will make front and rear point to invalid positions again
elseif(rear==front) front = rear = -1;
// Else just decrement the rear pointer, maxSize is added to deal with negative values
elserear = (rear+maxSize-1)%maxSize;
}
// Inserting from front end
publicvoidinsertFirst(intval) {
if(isFull()) return;
// If we are inserting at front end for the first time, then make it point to front of the array, that is maxSize. i treated maxsize-1 index as front end
elseif(front==-1 || front ==0) front = maxSize-1;
// else decrement the front pointer, maxSize is for dealing with negative values
elsefront = (front+maxSize-1)%maxSize;
arr[front] = val;
}
// Deleting from front end
publicvoiddeleteFirst() {
if(isEmpty()) return;
// if there is only element in the Deque, then deleting it will make front and rear point to invalid positions again
elseif( rear==front) front = rear = -1;
// else increment the front pointer
elsefront = (front+1)%maxSize;
}
// Checks if Deque is full
publicbooleanisFull() {
return (rear+1)%maxSize==front;
}
// Checks if Deque is empty
publicbooleanisEmpty() {
returnrear==-1 || front==-1;
}
}
/*
* Linked list based implementation of Deque.
*
*/
classDeque_Link {
// Pointers to start and end of the list
privateLink2head;
privateLink2tail;
//constructor
publicDeque_Link() {
head = null;
tail = null;
}
// Insertion at front
publicvoidinsertionFirst(intval) {
if(isFull()) return;
Link2newNode = newLink2(val);
newNode.next = head;
head = newNode;
}
// Deletes the first node
publicvoiddeleteFirst() {
if(isEmpty()) return;
head = head.next;
}
// Inserts after the last node
publicvoidinsertionRear( intval) {
if(isFull()) return;
Link2newNode = newLink2(val);
tail.next = newNode;
tail = newNode;
}
// Deletes the last node
publicvoiddeleteRear() {
if(isEmpty()) return;
tail = tail.prev;
tail.next = null;
}
// Checks if list is empty
publicbooleanisEmpty() { returnhead==null;}
// Checks if list is full, which is always false
publicbooleanisFull() { returnfalse; }
}
/*
* Priority Queue implementation based on array:
*
* Insertion takes O(N) time
* Deletion takes O(1) time,
* Inefficient compared to heap based implementation
*
*/
classPriority_Queue {
// Array holding priority queue
privateint[] arr;
// Size of the array
privateintsize;
// Current index of the array
privateintcur_index;
// Constructor
publicPriority_Queue(intsize) {
this.size = size;
arr = newint[size];
cur_index = 0;
}
// Sorted insertion, sorted in ascending order
publicvoidinsertion(intval) {
if(isFull()) return;
elseif(isEmpty()) arr[cur_index++] = val;
else {
inti =cur_index-1;
for(i=cur_index-1;i>=0;i--) {
if(arr[i]>val) {
arr[i+1] = arr[i];
}
else {
break;
}
}
arr[i+1] = val;
cur_index++;
}
}
// Deletes the highest element in the array
publicintdeletion() {
if(isEmpty()) return -1;
returnarr[cur_index--];
}
// Checks if the array is full
publicbooleanisFull() {
returncur_index == size;
}
// Checks if the array is empty
publicbooleanisEmpty() {
returncur_index==0;
}
}
// Main Class for Testing all other classes
publicclassQueueAllInOne {
publicstaticvoidmain(Stringargs[]) {
Dequedeq = newDeque(5);
//Queue testing, inserting from Rear, deleting from front
deq.insertRear(1);
deq.insertRear(2);
deq.insertRear(3);
deq.insertRear(4);
deq.insertRear(5);
System.out.println(deq.isFull());
deq.deleteRear();
deq.deleteRear();
deq.deleteRear();
deq.deleteRear();
deq.deleteRear();
System.out.println(deq.isEmpty());
//Stack Testing, inserting from rear, deleting from rear
deq.insertRear(1);
deq.insertRear(2);
deq.insertRear(3);
deq.insertRear(4);
deq.insertRear(5);
System.out.println(deq.isFull());
deq.deleteFirst();
deq.deleteFirst();
deq.deleteFirst();
deq.deleteFirst();
deq.deleteFirst();
System.out.println(deq.isEmpty());
//Stack Testing, opposite direction
deq.insertFirst(1);
deq.insertFirst(2);
deq.insertFirst(3);
deq.insertFirst(4);
deq.insertFirst(5);
System.out.println(deq.isFull());
deq.deleteRear();
deq.deleteRear();
deq.deleteRear();
deq.deleteRear();
deq.deleteRear();
System.out.println(deq.isEmpty());
//Queue Testing, opposite direction
deq.insertFirst(1);
deq.insertFirst(2);
deq.insertFirst(3);
deq.insertFirst(4);
deq.insertFirst(5);
System.out.println(deq.isFull());
deq.deleteFirst();
deq.deleteFirst();
deq.deleteFirst();
deq.deleteFirst();
deq.deleteFirst();
System.out.println(deq.isEmpty());
}
}
/*
* Utility Class
*
* Link2 - Definition of Node in a LinkedList
*
*/
classLink2 {
publicintdata;
publicLink2next;
publicLink2prev;
publicLink2(intval) {
data = val;
next = null;
}
publicvoiddisplay() {
System.out.print(data+" ");
}
}