-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathqueue_using_stacks.cpp
79 lines (58 loc) · 1.62 KB
/
queue_using_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
/* The basic difference in stack and queues is the order they are filled in practically.In Stacks,
they are inserted on top of previous element while In Queues they are inserted behind the last element.
The push() function is what we have to look out for. First we will empty out our primary stack s1 to another stack s2.
Then We push the element to be inserted x in s1(or s2).
Then just empty out the stack s2 back into s1. But the element x would be at the last place just like in a Queue.
The other Operations are the same as we have already implemented as a queue.*/
/*Complexity Analysis:
Time Complexity:
Push operation : O(1).
Same as pop operation in stack.
Pop operation : O(N).
The difference from above method is that in this method element is returned and all elements are restored back in a single call.
Auxiliary Space: O(N).
Use of stack for storing values.
*/
#include <bits/stdc++.h>
using namespace std;
struct Queue {
stack<int> s;
// Enqueue an item to the queue
void enQueue(int x)
{
s.push(x);
}
// Dequeue an item from the queue
int deQueue()
{
if (s.empty()) {
cout << "Q is empty";
exit(0);
}
// pop an item from the stack
int x = s.top();
s.pop();
// if stack becomes empty, return
// the popped item
if (s.empty())
return x;
// recursive call
int item = deQueue();
// push popped item back to the stack
s.push(x);
// return the result of deQueue() call
return item;
}
};
// Driver code
int main()
{
Queue q;
q.enQueue(10);
q.enQueue(20);
q.enQueue(30);
cout << q.deQueue() << '\n';
cout << q.deQueue() << '\n';
cout << q.deQueue() << '\n';
return 0;
}