-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.cpp
458 lines (395 loc) · 8.94 KB
/
Day12.cpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <bits/stdc++.h>
using namespace std;
//Stack using queue
/*There are two methods to do this
1. Masking push method costly
2. Making pop method costly
*/
//Pop is costly
//Linked list implementation of queues
//Dynamic queue
O(1) only for adding element not O(n)
#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
class queue{
node* front;
node* back;
public:
queue(){
front=NULL;
back=NULL;
}
void push(int x){
node* n= new node(x);
if(front==NULL){
back=n;
front=n;
}
back->next=n;
}
void pop(){
if(front==NULL){
cout<<"Queue underflow"<<endl;
return;
}
node* todelete=front;
front=front->next;
delete todelete;
}
int peek(){
if(front==NULL){
cout<<"No"<<endl;
return;
}
return front->data;
}
bool empty(){
if(front==NULL) return true;
return false;
}
};
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
class queue{
node* front;
node* back;
}
class Deque
{
int *arr;
int front;
int rear;
int size;
public:
// Initialize your data structure.
Deque(int n)
{
size = n;
arr = new int[n];
front = -1;
rear = -1;
}
// Pushes 'X' in the front of the deque. Returns true if it gets pushed into the deque, and false otherwise.
bool pushFront(int x)
{
//check full or not
if( isFull() ) {
return false;
}
else if(isEmpty()) {
front = rear = 0;
}
else if(front == 0 && rear != size-1) {
front = size-1;
}
else
{
front--;
}
arr[front] = x;
return true;
}
// Pushes 'X' in the back of the deque. Returns true if it gets pushed into the deque, and false otherwise.
bool pushRear(int x)
{
if( isFull() ) {
return false;
}
else if(isEmpty()) {
front = rear = 0;
}
else if(rear == size-1 && front != 0) {
rear = 0;
}
else
{
rear++;
}
arr[rear] = x;
return true;
}
// Pops an element from the front of the deque. Returns -1 if the deque is empty, otherwise returns the popped element.
int popFront()
{
if(isEmpty()){//to check queue is empty
//cout << "Queue is Empty " << endl;
return -1;
}
int ans = arr[front];
arr[front] = -1;
if(front == rear) { //single element is present
front = rear = -1;
}
else if(front == size - 1) {
front = 0; //to maintain cyclic nature
}
else
{//normal flow
front++;
}
return ans;
}
// Pops an element from the back of the deque. Returns -1 if the deque is empty, otherwise returns the popped element.
int popRear()
{
if(isEmpty()){//to check queue is empty
//cout << "Queue is Empty " << endl;
return -1;
}
int ans = arr[rear];
arr[rear] = -1;
if(front == rear) { //single element is present
front = rear = -1;
}
else if(rear == 0) {
rear = size-1; //to maintain cyclic nature
}
else
{//normal flow
rear--;
}
return ans;
}
// Returns the first element of the deque. If the deque is empty, it returns -1.
int getFront()
{
if(isEmpty()){
return -1;
}
return arr[front];
}
// Returns the last element of the deque. If the deque is empty, it returns -1.
int getRear()
{
if(isEmpty()){
return -1;
}
return arr[rear];
}
// Returns true if the deque is empty. Otherwise returns false.
bool isEmpty()
{
if(front == -1)
return true;
else
return false;
}
// Returns true if the deque is full. Otherwise returns false.
bool isFull()
{
if( (front == 0 && rear == size-1) || (front != 0 && rear == (front-1)%(size-1) ) ) {
return true;
}
else
{
return false;
}
}
};//Implemenation of dequeue
class Mystack{
int N;
queue<int> q1;
queue<int> q2;
public:
Mystack(){
N=0;
}
int size(){
return N;
}
void pop(){
if(q1.empty()){
return;
}
while(q1.size()!=1){
q2.push(q1.front());
q1.pop();
}
q1.pop();
N--;
//Exchaning q1 and q2 using temporary queue
// swap(q1,q2);
queue<int> temp;
temp=q1;
q1=q2;
q2=temp;
}
void push(int val){
q1.push(val);
N++;
}
int top(){
if(q1.empty()){
return -1;
}
while(q1.size()!=1){
q2.push(q1.front());
q1.pop();
}
int ans= q1.front();
q2.push(ans);
queue<int> temp=q1;
q1=q2;
q2=temp;
return ans;
}
};
//Queues using stacks
//Approach 1
class Myqueue1{
stack<int> s1;
stack<int> s2;
public:
void push(int x){
s1.push(x);
}
int pop1(){
if(s1.empty() && s2.empty()){
cout<<"Queue is empty\n";
return -1;
}
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
int topVal=s2.top();
s2.pop();
return topVal;
}
int pop2(){
if(s1.empty()){
cout<<"Queue is empty\n";
return -1;
}
int x= s1.top();
s1.pop();
if(s1.empty()){
return x;
}
int item=pop2();
s1.push(x);
return item;
}
bool empty(){
if(s1.empty()){
return true;
}
return false;
}
};
int main(){
Mystack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
cout<<st.size()<<endl;
/*Myqueue1 q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);*/
/*cout<<q.pop2()<<endl;
cout<<q.pop2()<<endl;
cout<<q.pop2()<<endl;
cout<<q.pop2()<<endl;
cout<<q.pop2()<<endl;*/
/*cout<<q.pop1()<<endl;
q.push(7);
cout<<q.pop1()<<endl;
cout<<q.pop1()<<endl;
cout<<q.pop1()<<endl;
cout<<q.pop1()<<endl;
cout<<q.pop1()<<endl;
cout<<q.empty()<<endl;*/
return 0;
}
//Approach 2- Using function call stack
/*Dequeue:
1. If stack1 is empty then print error
2. If stack1 has only one element then return it
3. Recursively pop everthing from stack1, store the popped item in a variable res, push the res back to stack1 and return res
*/
//Queue: Stores a list of items in which an item can be inserted at one end and removed from the other end only-FIFO
/*Operations enqueue(x),dequeue(),peek() : prints the front element,empty(),front,back pointers*/
//Array implementation of queues
#define n 20
class Myqueue{
int *arr;
int front;
int back;
public:
Myqueue(){
arr= new int[n];
front=-1;
back=-1;
}
void push(int x){
if(back==n-1){
cout<<"Queue Overflow"<<endl;
return;
}
back++;
arr[back]=x;
if(front==-1){
front++;
}
}
void pop(){
if(front==-1 || front>back){
cout<<"No elements in queue"<<endl;
return;
}
front++;
}
int peek(){
if(front==-1 || front>back){
cout<<"No elements in queue"<<endl;
return -1;
}
return arr[front];
}
bool empty(){
if(front==-1 || front>back){
cout<<"No elements in queue"<<endl;
return true;
}
return false;
}
};
/*int main(){
Myqueue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
cout<<q.peek()<<endl;
q.pop();
cout<<q.peek()<<endl;
cout<<q.empty()<<endl;
return 0;
}*/