-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_using_queue.cpp
67 lines (56 loc) · 1.27 KB
/
stack_using_queue.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
#include <iostream>
#include <queue>
#include <vector>
#include <cstdlib>
using namespace std;
// Implement stack using two queues
class Stack
{
queue<int> q1, q2;
public:
// Insert an item into the stack
void push(int data)
{
// Move all elements from the first queue to the second queue
while (!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
// Push the given item into the first queue
q1.push(data);
// Move all elements back to the first queue from the second queue
while (!q2.empty())
{
q1.push(q2.front());
q2.pop();
}
}
// Remove the top item from the stack
int pop()
{
// if the first queue is empty
if (q1.empty())
{
cout << "Underflow!!";
exit(0);
}
// return the front item from the first queue
int front = q1.front();
q1.pop();
return front;
}
};
int main()
{
vector<int> keys = { 1, 2, 3, 4, 5 };
// insert the above keys into the stack
Stack s;
for (int key: keys) {
s.push(key);
}
for (int i = 0; i <= keys.size(); i++) {
cout << s.pop() << endl;
}
return 0;
}