forked from intrinsi/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueusingstack.cpp
64 lines (54 loc) · 1.21 KB
/
queueusingstack.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
#include <iostream>
#include <stack>
#include <cstdlib>
using namespace std;
// Implement a queue using two stacks
class Queue
{
stack<int> s1, s2;
public:
// Add an item to the queue
void enqueue(int data)
{
// Move all elements from the first stack to the second stack
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
// push item into the first stack
s1.push(data);
// Move all elements back to the first stack from the second stack
while (!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
}
// Remove an item from the queue
int dequeue()
{
// if the first stack is empty
if (s1.empty())
{
cout << "Underflow!!";
exit(0);
}
// return the top item from the first stack
int top = s1.top();
s1.pop();
return top;
}
};
int main()
{
int keys[] = { 1, 2, 3, 4, 5 };
Queue q;
// insert above keys
for (int key: keys) {
q.enqueue(key);
}
cout << q.dequeue() << endl; // print 1
cout << q.dequeue() << endl; // print 2
return 0;
}