-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path24_3_queue_using_stack_2.cpp
98 lines (83 loc) · 1.42 KB
/
24_3_queue_using_stack_2.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
#include "bits/stdc++.h"
using namespace std;
/*
Approach 2:
Using Function Call Stack
enQueue(x):
Push x to stack1.
deQueue(x):
1.If stack1 is empty then print error.
2.If stack1 has only one element then return it.
3.Recursively pop everything from the stack1, store the popped item in a variable res, push the res back to stack1 and return res.
*/
class que{
stack<int> s1;
public:
void push(int x){
s1.push(x);
}
int pop(){
if(s1.empty()){
cout<<"Queue is empty\n";
return -1;
}
int x=s1.top();
s1.pop();
if(s1.empty()){
return x;
}
int item =pop();
s1.push(x);
return item;
}
/*
st=1234
x=4
st=123
item=pop()
st=123
x=3
st=12
item=pop()
st=12
x=2
st=1
item=pop()
st=1
x=1
st=NONE
return 1
item=1
st=2
return 1
item=1
st=23
return 1
item=1
st=234
return 1
*/
bool empty(){
if(s1.empty()){
return true;
}
return false;
}
};
int32_t main()
{
que q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout<<q.pop()<<"\n";
q.push(5);
cout<<q.pop()<<"\n";
cout<<q.pop()<<"\n";
cout<<q.pop()<<"\n";
cout<<q.pop()<<"\n";
cout<<q.pop()<<"\n";
cout<<q.empty()<<endl;
return 0;
}