-
Notifications
You must be signed in to change notification settings - Fork 7
/
50_Queue_Using_Stacks.cpp
59 lines (58 loc) · 1.03 KB
/
50_Queue_Using_Stacks.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
#include<iostream>
#include<stack>
#include"38_Stack_LinkedList.cpp"
using namespace std;
stack<int> s1,s2;
// Dequeue is expensive
void dequeue(){
if(!s1.empty() && s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s2.pop();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
}
else{
cout<<"Queue is empty"<<endl;
}
}
void enqueue(int val){
s1.push(val);
}
void display(){
if(!s1.empty() && s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
while(!s2.empty()){
cout<<s2.top()<<" ";;
s1.push(s2.top());
s2.pop();
}
cout<<endl;
}
else{
cout<<"Queue is empty"<<endl;
}
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
display();
dequeue();
display();
dequeue();
display();
dequeue();
display();
return 0;
}