-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_using_queue.cpp
More file actions
73 lines (57 loc) · 1.61 KB
/
stack_using_queue.cpp
File metadata and controls
73 lines (57 loc) · 1.61 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
// https://leetcode.com/problems/implement-stack-using-queues/solution/
#include<iostream>
#include<queue>
using namespace std;
class MyStack {
public:
queue<int> q_real;
queue<int> q_hold;
MyStack() {
}
void push(int x) {
q_hold.push(x);
while(!q_real.empty()){
q_hold.push(q_real.front());
q_real.pop();
}
while(!q_hold.empty()){
q_real.push(q_hold.front());
q_hold.pop();
}
}
int pop() {
int temp = q_real.front();
q_real.pop();
return temp;
}
int top() {
return q_real.front();
}
bool empty() {
return q_real.empty();
}
};
/*
The mentioned above two approaches have one weakness, they use two queues.
This could be optimized as we use only one queue, instead of two.
Push
When we push an element into a queue,
it will be stored at back of the queue due to queue's properties.
But we need to implement a stack,
where last inserted element should be in the front of the queue, not at the back.
To achieve this we can invert the order of queue elements when pushing a new element.
We will add the element and then will keep on poping element and again pushing them
will size if equal to the size before insertion of this element
*/
void push(int x, queue<int> q){
int s = q.size();
q.push(x);
// by this we are just pushing the previous element
// after the currently added element
while(s>1){
int temp = q.front();
q.pop();
q.push(temp);
s--;
}
}