-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQueues.java
More file actions
374 lines (370 loc) · 9.22 KB
/
MyQueues.java
File metadata and controls
374 lines (370 loc) · 9.22 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
import java.util.*;
public class MyQueues {
public static void printQ(Queue<Integer> q) {
Queue<Integer> q2=new LinkedList<>();
while(!q.isEmpty()){
System.out.print(q.peek()+" ");
q2.add(q.remove());
}
System.out.println();
while(!q2.isEmpty()){
q.add(q2.remove());
}
}
public static void printQEfficient(Queue<Integer> q) {
int arr[]=new int[q.size()];
for(int i=0;i<arr.length;i++){
System.out.print(q.peek()+" ");
arr[i]=q.remove();
}
System.out.println();
for(int x:arr){
q.add(x);
}
}
public static void printReverse(Queue<Integer> q) {
int arr[]=new int[q.size()];
for(int i=0;i<arr.length;i++){
arr[i]=q.remove();
}
for(int i=arr.length-1;i>=0;i--){
System.out.print(arr[i]+" ");
}
System.out.println();
for(int x:arr){
q.add(x);
}
}
public static void reverseQ(Queue<Integer> q){
Stack<Integer> stk=new Stack<>();
while(!q.isEmpty()){
stk.push(q.poll());
}
while(!stk.isEmpty()){
q.add(stk.pop());
}
System.out.println(q);
}
public static void reverseFirstKElements(Queue<Integer> q,int k) {
Stack<Integer> stk=new Stack<>();
for(int i=1;i<=k;i++)
stk.push(q.remove());
while(!stk.isEmpty())
q.add(stk.pop());
for(int i=1;i<=q.size()-k;i++){
q.add(q.remove());
}
System.out.println(q);
}
public static void reorderQueue(Queue<Integer> q) {
//interleave 1st and 2nd half of queue together(assuming even size queue)
Stack<Integer> stk=new Stack<>();
int n=q.size();
for(int i=1;i<=n/2;i++){
stk.push(q.remove());
}
while (!stk.isEmpty()) {
q.add(stk.pop());
}
for(int i=1;i<=n/2;i++){
stk.push(q.remove());
}
while (!stk.isEmpty()) {
q.add(stk.pop());
q.add(q.remove());
}
reverseQ(q);
}
public static void main(String[] args) throws Exception{
ArrayList<Integer> al=new ArrayList<>();
for(int i=1;i<=10;i++)
al.add(i);
System.out.println(al);
al.remove(2);
System.out.println(al);
}
}
class dequeLL{
Node head=null,tail=null;
int size=0;
void addLast(int val){
Node t=new Node(val);
if(size==0)head=tail=t;
else{
tail.next=t;
tail=t;
}
size++;
}
void addFirst(int val){
Node t=new Node(val);
if(size==0)head=tail=t;
else{
t.next=head;
head=t;
}
size++;
}
int popFirst()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
size--;
int val=head.val;
head=head.next;
return val;
}
int popLast()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
size--;
int val=tail.val;
for(Node t=head;t!=null;t=t.next){
if(t.next==tail){
t.next=null;
tail=t;
}
}
return val;
}
int peekFirst()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
return head.val;
}
int peekLast()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
return tail.val;
}
public String toString(){
StringBuilder sb=new StringBuilder();
for(Node t=head;t!=null;t=t.next){
sb.append(t.val+" ");
}
return sb.toString();
}
}
class dequeueArray{
int q[]=new int[10];
int f=-1,r=-1,size=0;
void addLast(int val)throws Exception{
if(size==q.length)throw new Exception("Queue is full!");
if(size==0)f=0;
q[++r]=val;
size++;
}
void addFirst(int val)throws Exception{
if(size==q.length||f==0)throw new Exception("Queue is full!");
if(size==0){
f=r=0;
q[0]=val;
}else q[--f]=val;
size++;
}
int popFirst()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
size--;
return q[f++];
}
int popLast()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
size--;
return q[r--];
}
int peekFirst()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
return q[f];
}
int peekLast()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
return q[r];
}
boolean isEmpty(){
return size==0;
}
public String toString(){
StringBuilder sb=new StringBuilder();
for(int i=f;i<=r;i++){
sb.append(q[i]+" ");
}
return sb.toString();
}
}
class circularQLL{
Node head=null,tail=null;
int size=0;
void add(int data){
Node t=new Node(data);
if(size==0)head=t;
else tail.next=t;
t.next=head;
tail=t;
size++;
}
int pop()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
size--;
int data=head.val;
head=head.next;
return data;
}
int peek()throws Exception{
if(size==0)throw new Exception("Queue is Empty!");
return head.val;
}
boolean isEmpty(){
return size==0;
}
public String toString() {
StringBuilder sb=new StringBuilder();
if(isEmpty())return "Queue is Empty!";
for(Node t=head;t.next!=head;t=t.next)
sb.append(t.val+" ");
return sb.toString();
}
}
class circularQueue{
int q[]=new int[10];
int size=0,f=-1,r=-1;
void add(int val)throws Exception{
if(size==0){
q[++r]=val;
f++;
}else if(size==q.length)throw new Exception("queue is full!");
else if(r==q.length-1){
q[0]=val;
r=0;
}
else q[++r]=val;
size++;
}
int pop() throws Exception{
if(size==0)throw new Exception("Queue is already empty");
else{
int val=q[f];
f=(f==q.length-1)?0:++f;
size--;
return val;
}
}
int peek()throws Exception{
if(size==0)throw new Exception("Queue is already empty");
return q[f];
}
boolean isEmpty(){
return size==0;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
if(size==0)return "Queue is Empty!";
if(f<=r){
for(int i=f;i<=r;i++){
sb.append(q[i]+" ");
}
}else{
//f to n
for(int i=f;i<q.length;i++){
sb.append(q[i]+" ");
}
//0 to r
for(int i=0;i<=r;i++){
sb.append(q[i]+" ");
}
}
return sb.toString();
}
}
class Node{
int val;
Node next=null;
public Node(int val){
this.val=val;
}
}
class queueLinkedList{
Node head=null,tail=null;
int size=0;
void add(int val){
Node t=new Node(val);
if(size==0)head=t;
else tail.next=t;
tail=t;
size++;
}
int pop(){
if(isEmpty()){
System.out.println("queue is aready empty hence -1 is returned");
return -1;
}
int pop=head.val;
head=head.next;
size--;
return pop;
}
int peek(){
if(isEmpty()){
System.out.println("queue is aready empty hence -1 is returned");
return -1;
}
return head.val;
}
boolean isEmpty(){
return size==0;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
for(Node t=head;t!=null;t=t.next){
sb.append(t.val+" ");
}
return sb.toString();
}
}
class queueArray{
int q[]=new int[10];
int front=-1,rear=-1,size=0;
void add(int element) {//push
if(rear==q.length-1){
System.out.println("queue is full");
return;
}
if(front==-1)front++;
q[++rear]=element;
size++;
}
int pop() {//pop
if(front==-1){
System.out.println("queue is already empty hence -1 is returned");
return -1;
}
size--;
return q[front++];
}
int popOptimized(){
if(front==-1){
System.out.println("queue is already empty hence -1 is returned");
return -1;
}
int pop=q[front];
for(int i=front;i<rear;i++){
q[i]=q[i+1];
} rear--;
size--;
return pop;
}
int peek(){
if(front==-1){
System.out.println("queue is already empty hence -1 is returned");
return -1;
}
return q[front];
}
boolean isEmpty(){
return size==0;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
for(int i=front;i<=rear;i++){
sb.append(q[i]+" ");
}
return sb.toString();
}
}