forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_1.java
More file actions
89 lines (76 loc) · 2.04 KB
/
Exercise_1.java
File metadata and controls
89 lines (76 loc) · 2.04 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.Stack;
//we take 2 stacks, in stack to push the elements, out stack to pop/peek the elements
//TC : push : perfect O(1), pop, peek: Amortized O(1), empty: O(1)
//SC: O(1)
public class MyQueue {
private final Stack<Integer> in;
private final Stack<Integer> out;
/**
* Constructor to initialize {@link MyQueue} and the in and out stacks
*/
public MyQueue() {
this.in = new Stack<>();
this.out = new Stack<>();
}
/**
* Push the elements in the in stack
* @param x the value to be pushed
*/
public void push(int x) {
in.push(x);
}
/**
*
* @return pop the top element in the out stack aka the Queue
*/
public int pop() {
peek();
return out.pop();
}
/**
*
* @return peek the top element in the out stack aka the Queue
*/
public int peek() {
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
return out.peek();
}
/**
*
* @return true if both in stack and out stack are empty
*/
public boolean empty() {
return in.isEmpty() && out.isEmpty();
}
public static void main(String[] args) {
final MyQueue myQueue = new MyQueue();
myQueue.empty(); //return true
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
myQueue.push(4);
myQueue.empty(); //return false
myQueue.pop(); //return 1
myQueue.peek(); //return 2
myQueue.pop(); //return 2
myQueue.pop(); //return 3
myQueue.pop(); //return 4
myQueue.push(6);
myQueue.push(7);
myQueue.pop(); //return 6
myQueue.pop(); //return 7
myQueue.empty(); //return true
}
}
/**
* 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();
*/