-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueues.java
More file actions
340 lines (285 loc) · 10.2 KB
/
Copy pathQueues.java
File metadata and controls
340 lines (285 loc) · 10.2 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
import java.util.*;
import javax.lang.model.element.Element;
public class Queues {
// // static class Node{
// // int data;
// // Node next;
// // Node(int data){
// // this.data = data;
// // this.next = null;
// // }
// // }
// // static class Queue{
// // // // static int arr[]; // Queue using array
// // // // static int size;
// // // // static int rear;
// // // // static int front; // for Circular array queue
// // // // Queue(int n){
// // // // arr = new int[n];
// // // // size = n;
// // // // rear = -1;
// // // // front = -1;
// // // // }
// // // static Node head = null;
// // // static Node tail = null;
// // // // Is empty
// // // public static boolean isEmpty(){
// // // // return rear == -1 && front == -1; // for arrays
// // // return head == null && tail == null; // for linked list
// // // }
// // // // is full
// // // // public static boolean isFull(){
// // // // return (rear + 1) % size == front; // For circular linked list
// // // // }
// // // // Add function
// // // public static void add(int data){ // O(1)
// // // // if (isFull()){
// // // // System.out.println("Queue is full");
// // // // return;
// // // // }
// // // // For arrays
// // // // if (front == -1){
// // // // front = 0;
// // // // }
// // // // // rear++;
// // // // rear = (rear + 1) % size; // for circular array
// // // // arr[rear] = data;
// // // // For linked list
// // // Node newNode = new Node(data);
// // // if (head == null){
// // // head = tail = newNode;
// // // }
// // // tail.next = newNode;
// // // tail = newNode;
// // // }
// // // // Remove function
// // // public static int remove(){ // O(n)
// // // if (isEmpty()){
// // // System.out.println("The Queue is already empty");
// // // return -1;
// // // }
// // // // For arrays
// // // // int result = arr[front];
// // // // // rear--;
// // // // // Last element delete
// // // // if (rear == front){
// // // // rear = front = -1;
// // // // }else{
// // // // front = (front + 1) % size;
// // // // }
// // // // return result;
// // // // For linkedlist
// // // int front = head.data;
// // // if(tail == head){
// // // head = tail = null;
// // // }else{
// // // head = head.next;
// // // }
// // // return front;
// // // }
// // // // Peek function
// // // public static int peek(){
// // // if (isEmpty()){
// // // System.out.println("The Queue is already empty");
// // // return -1;
// // // }
// // // // return arr[front]; // In case of arrays
// // // return head.data; // In case of linked list
// // // }
// // // Queue using two stacks
// // static Stack<Integer> s1 = new Stack<>();
// // static Stack<Integer> s2 = new Stack<>();
// // public static boolean isEmpty(){
// // return s1.isEmpty();
// // }
// // // add
// // public static void add(int data){
// // while(!s1.isEmpty()){
// // s2.push(s1.pop());
// // }
// // s1.push(data);
// // while(!s2.isEmpty()){
// // s1.push(s2.pop());
// // }
// // }
// // // Remove
// // public static int remove(){
// // while(isEmpty()){
// // System.out.println("Queue is empty");
// // return -1;
// // }
// // return s1.pop();
// // }
// // // peek
// // public static int peek(){
// // while(isEmpty()){
// // System.out.println("Queue is empty");
// // return -1;
// // }
// // return s1.peek();
// // }
// // }
// // static class Stack{
// // static Queue<Integer> q1 = new LinkedList<>();
// // static Queue<Integer> q2 = new LinkedList<>();
// // public static boolean isEmpty(){
// // return q1.isEmpty() && q2.isEmpty();
// // }
// // public static void push(int data){
// // if(!q1.isEmpty()){
// // q1.add(data);
// // }else{
// // q2.add(data);
// // }
// // }
// // public static int pop(){
// // if (isEmpty()){
// // System.out.println("Queue is Empty");
// // return -1;
// // }
// // int top = -1;
// // if (!q1.isEmpty()){ // Case 1 q1 k andr saare elements h
// // while(!q1.isEmpty()){
// // top = q1.remove();
// // if (q1.isEmpty()){
// // break;
// // }
// // q2.add(top);
// // }
// // }else{ // Case 2 q2 k andr saare elements h
// // while(!q2.isEmpty()){
// // top = q2.remove();
// // if (q2.isEmpty()){
// // break;
// // }
// // q1.add(top);
// // }
// // }
// // return top;
// // }
// // public static int peek(){
// // if (isEmpty()){
// // System.out.println("Queue is Empty");
// // return -1;
// // }
// // int top = -1;
// // if (!q1.isEmpty()){ // Case 1 q1 k andr saare elements h
// // while(!q1.isEmpty()){
// // top = q1.remove();
// // q2.add(top);
// // }
// // }else{ // Case 2 q2 k andr saare elements h
// // while(!q2.isEmpty()){
// // top = q2.remove();
// // q1.add(top);
// // }
// // }
// // return top;
// // }
// // }
// public static void nonRepeating(String str){
// int freq[] = new int[26];
// Queue<Character> q = new LinkedList<>();
// for (int i = 0; i < str.length(); i++){
// char ch = str.charAt(i);
// q.add(ch);
// freq[ch - 'a']++;
// while(!q.isEmpty() && freq[q.peek() - 'a'] > 1){
// q.remove();
// }
// if(q.isEmpty()){
// System.out.print(-1 + " ");
// }else{
// System.out.print(q.peek() + " ");
// }
// }
// System.out.println();
// }
// public static void interLeave(Queue<Integer> q){
// Queue<Integer> firstHalf = new LinkedList<>();
// int size = q.size();
// for (int i = 0; i < size/2; i++){
// firstHalf.add(q.remove());
// }
// while(!firstHalf.isEmpty()){
// q.add(firstHalf.remove());
// q.add(q.remove());
// }
// }
// public static void reverse(Queue<Integer> q){
// Stack<Integer> s = new Stack<>();
// while(!q.isEmpty()){
// s.push(q.remove());
// }
// while(!s.isEmpty()){
// q.add(s.pop());
// }
// }
static class Stack{
Deque<Integer> deque = new LinkedList<>();
// Push
public void push(int data){
deque.addLast(data);
}
// pop
public int pop(){
return deque.removeLast();
}
// peek
public int peek(){
return deque.getLast();
}
}
public static void main(String[] args) {
// Queue q = new Queue();
// Queue<Integer> q = new LinkedList<>(); // Through JCF // ArrayDeque can also be used instead of LinkedList
// q.add(1);
// q.add(2);
// q.add(3);
// // System.out.println(q.remove());
// q.add(4);
// // System.out.println(q.remove());
// q.add(5);
// while (!q.isEmpty()){
// System.out.println(q.peek());
// q.remove();
// }
// Stack s = new Stack();
// s.push(1);
// s.push(2);
// s.push(3);
// while(!s.isEmpty()){
// System.out.println(s.peek());
// s.pop();
// }
// String str = "aabccxb";
// nonRepeating(str);
// Queue<Integer> q = new LinkedList<>();
// q.add(1);
// q.add(2);
// q.add(3);
// q.add(4);
// q.add(5);
// // interLeave(q);
// reverse(q);
// while(!q.isEmpty()){
// System.out.print(q.remove() + " ");
// }
// System.out.println();
// Deque<Integer> dq = new LinkedList<>();
// dq.addFirst(1);
// dq.addFirst(2);
// dq.addLast(3);
// dq.addLast(4);
// System.out.println(dq);
// dq.remove();
// System.out.println(dq);
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}