-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyStack.java
36 lines (31 loc) · 822 Bytes
/
MyStack.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
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;
public class MyStack {
Deque<Integer> queue = new ArrayDeque<Integer>();
// Push element x onto stack.
public void push(int x) {
queue.addFirst(x);
}
// Removes the element on top of the stack.
public void pop() {
queue.removeFirst();
}
// Get the top element.
public int top() {
int e = queue.getFirst();
return e;
}
// Return whether the stack is empty.
public boolean empty() {
return queue.isEmpty();
}
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
System.out.println(stack.top());
stack.pop();
System.out.println(stack.top());
}
}