-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQueue.java
More file actions
52 lines (46 loc) · 1.33 KB
/
MyQueue.java
File metadata and controls
52 lines (46 loc) · 1.33 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
package basic.QueueAndStack;
import org.junit.Assert;
import org.junit.Test;
import java.util.Stack;
/**
* 用栈实现队列,解法类似于用队列实现栈,都是用两个设定的数据结构在push时做一次数据的转换。
*/
public class MyQueue {
private Stack<Integer> stack =new Stack<Integer>();
private Stack<Integer> temp =new Stack<Integer>();
@Test
public void test() {
MyQueue myQueue=new MyQueue();
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
Assert.assertEquals(1,myQueue.peek());
Assert.assertEquals(1,myQueue.pop());
Assert.assertEquals(2,myQueue.pop());
Assert.assertEquals(3,myQueue.peek());
}
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
while (!stack.isEmpty()) {
temp.add(stack.pop());
}
stack.add(x);
while (!temp.isEmpty()) {
stack.add(temp.pop());
}
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
return stack.pop();
}
/** Get the front element. */
public int peek() {
return stack.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack.isEmpty();
}
}