forked from ShyrenMore/InterviewPrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_using_queue.cpp
74 lines (59 loc) · 1.32 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
68
69
70
71
72
73
74
/*
Consider a situation where we have library available for queue
and we need stack library
How do we implement the stack
We use two queues q1 and q2
whenever we push an item, it will go into q1 only
so top(), size(), pop() operations will be done using q1
we need to push in q1 in O(1) time
eg: q1 = {10, 5, 15}, q2={}
in q1 rear is 15, front is 10
push(20)
we should get q1 = {20, 10, 5, 15}
instead of q1 = {10, 5, 15, 20}
to do this, we put all els of q1 in q2
q1 = {}, q2 = {10, 5, 15}
now push the new item in q1
q1 = {20}, q2 = {10, 5, 15}
move everything back to q1
q1 = {20, 10, 5, 15} q2={}
top, size, pop are O(1)
only push is O(n)
you can modify the code where other operations are costly instead of push
*/
#include <iostream>
#include <queue>
using namespace std;
class Stack{
public:
queue<int> q1, q2;
int top()
{
return q1.front();
}
int size()
{
return q1.size();
}
int pop()
{
return q1.front();
}
void push(int x)
{
// copy q1 to q2
while(!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
// push new item in empty q1
q1.push(x);
// copy back to q1
while (!q2.empty())
{
q1.push(q2.front());
q2.pop();
}
}
};