forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQUEUES_USING_A_TWO_STACKS.CPP
79 lines (69 loc) · 1.28 KB
/
QUEUES_USING_A_TWO_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// using classes
// optimisation needed
#include <iostream>
#include <stack>
using namespace std;
class myqueue{
stack<int> st1;
stack<int> st2;
public:
void poping()
{
if(st1.empty())
cout<<"THE STACK IS EMPTY!"<<endl;
else {
while(!st1.empty())
{
st2.push(st1.top());
st1.pop();
}
st2.pop();
while(!st2.empty())
{
st1.push(st2.top());
st2.pop();
}
}
}
void pushing(int ele)
{
st1.push(ele);
}
void print()
{
// we need to push all the elements from st1 to st2 and then print elements of st2 so as to maintain
// the order of queue
if(st1.empty())
cout<<"THE STACK IS EMPTY!"<<endl;
else {
while(!st1.empty())
{
st2.push(st1.top());
st1.pop();
}
while(!st2.empty())
{
cout<<st2.top()<<" ";
st2.pop();
}
}
}
};
int main()
{
myqueue mq;
mq.pushing(1);
mq.pushing(2);
mq.pushing(3);
mq.pushing(4);
mq.pushing(5);
mq.pushing(6);
mq.poping();
mq.poping();
mq.poping();
mq.pushing(7);
mq.pushing(8);
mq.pushing(9);
mq.print();
return 0;
}