-
Notifications
You must be signed in to change notification settings - Fork 0
/
L232 - ImplementQueueUsingStacks.java
65 lines (51 loc) · 1.43 KB
/
L232 - ImplementQueueUsingStacks.java
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
class MyQueue {
Stack<Integer> s1;
Stack<Integer> s2;
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
public void push(int x) {
//approach 1 where we add in constant time and remove in 'n' time
s1.push(x);
}
public int pop() {
while( !s1.isEmpty()){
int item =s1.pop();
s2.push(item);
}
//now items are transfered to s2
//we only need to remove the top element frim s2 (pop from Q)
int removed = s2.pop();
//now transfer back the remainng elements from s2 to s1;
while(!s2.isEmpty()){
int item = s2.pop();
s1.push(item);
}
return removed;
}
public int peek() {
//similar to pop but we just need to see the 1st (element that'll be reomved first from the Q')
while( !s1.isEmpty()){
int item =s1.pop();
s2.push(item);
}
int peeked = s2.peek();
while(!s2.isEmpty()){
int item = s2.pop();
s1.push(item);
}
return peeked;
}
public boolean empty() {
return s1.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/