forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQueue.java
More file actions
40 lines (35 loc) · 798 Bytes
/
MyQueue.java
File metadata and controls
40 lines (35 loc) · 798 Bytes
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
import java.util.Stack;
class MyQueue {
Stack<Integer> first;
Stack<Integer> second;
public MyQueue() {
first =new Stack();
second = new Stack();
}
public void push(int x) {
first.push(x);
}
public int pop() {
while(!first.isEmpty()){
second.push(first.pop());
}
int rem=second.pop();
while(!second.isEmpty()){
first.push(second.pop());
}
return rem;
}
public int peek() {
while(!first.isEmpty()){
second.push(first.pop());
}
int rem=second.peek();
while(!second.isEmpty()){
first.push(second.pop());
}
return rem;
}
public boolean empty() {
return first.isEmpty();
}
}