-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueUsing2Stacks.cpp
52 lines (44 loc) · 945 Bytes
/
QueueUsing2Stacks.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
#include<stack>
#include<iostream>
using namespace std;
//Implementation of a Queue Using Two Stacks
stack<int> st1, st2;
void enqueue(int n) {
while (!st1.empty()) {
st2.push(st1.top());
st1.pop();
}
st1.push(n);
while (!st2.empty()) {
st1.push(st2.top());
st2.pop();
}
}
int dequeue() {
if (st1.empty()) {
cout << "empty queue";
return 0;
}
int res = st1.top();
cout << res << " is popped off the queue "<< endl;
st1.pop();
return res;
}
int main()
{
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
enqueue(6);
enqueue(7);
cout << dequeue() << '\n';
cout << dequeue() << '\n';
cout << dequeue() << '\n';
cout << dequeue() << '\n';
cout << dequeue() << '\n';
cout << dequeue() << '\n';
cout << dequeue() << '\n';
return 0;
}