forked from imsushant12/GFG-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_using_two_queues.cpp
84 lines (59 loc) · 1.33 KB
/
Stack_using_two_queues.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
/*
Problem Statement:
------------------
Implement a Stack using two queues q1 and q2.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3 the
stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
Example 2:
Input:
push(2)
pop()
pop()
push(3)
Output: 2 -1
Your Task:
Since this is a function problem, you don't need to take inputs. You are required to complete the two methods push() which takes an integer 'x'
as input denoting the element to be pushed into the stack and pop() which returns the integer poped out from the stack(-1 if the stack is empty).
Expected Time Complexity: O(1) for push() and O(N) for pop() (or vice-versa).
Expected Auxiliary Space: O(1) for both push() and pop().
*/
// Link --> https://practice.geeksforgeeks.org/problems/stack-using-two-queues/1
// Code:
void QueueStack ::push(int num)
{
while(!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
q1.push(num);
while(!q2.empty())
{
q1.push(q2.front());
q2.pop();
}
}
int QueueStack ::pop()
{
int answer = -1;
if(!q1.empty())
{
answer = q1.front();
q1.pop();
return answer;
}
return answer;
}