-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack using Two queue.cpp
More file actions
108 lines (97 loc) · 1.69 KB
/
Stack using Two queue.cpp
File metadata and controls
108 lines (97 loc) · 1.69 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Construct a stack using two queues (q1, q2), you need to simulate the stack operations by using queue operations. You are required to complete the three methods push() which takes an integer 'x' as input denoting the element to be pushed into the stack, pop() which poped out from the stack and display() which display the resultant stack(-1 if the stack is empty).
Input Format
Enter the sequence of stack operation, 1 for push(), 2 for pop() and 3 for display().
Constraints
Maximum size of stack in 20
Output Format
Display result according to sequence of operations
Sample Input 0
1
2
1
3
2
1
4
2
1
5
3
Sample Output 0
2 5
Sample Input 1
1
2
2
3
Sample Output 1
-1
*/
#include<iostream>
#include<queue>
using namespace std;
int *arr;
queue<int> q1;
queue<int> q2;
void push(int x)
{
q2.push(x);
while(!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
swap(q1,q2);
}
void pop()
{
q1.pop();
}
void display()
{
if(q1.empty())
{
cout<<"-1"<<" ";
}
else
{
q2=q1;
arr = new int[q1.size()];
int j=0;
while(!q2.empty())
{
arr[j]=q2.front();
q2.pop();
j++;
}
for (int i=q1.size()-1;i>=0;i--)
{
cout<<arr[i]<<" ";
}
}
}
int main()
{
int choice;
do
{
cin>>choice;
switch(choice)
{
case 1:
int x;
cin>>x;
push(x);
break;
case 2:
pop();
break;
case 3:
display();
default:
exit(0);
}
}
while(true);
}