-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_9CQueue.java
More file actions
47 lines (41 loc) · 1.35 KB
/
Copy path_9CQueue.java
File metadata and controls
47 lines (41 loc) · 1.35 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
package offer;
import java.util.Deque;
import java.util.LinkedList;
/**
* @author : CodeWater
* @create :2022-03-12-16:05
* @Function Description :
* 剑指 Offer 09. 用两个栈实现队列
*/
class _9CQueue {
//Deque双端队列;虽然题目说是用两个栈模拟队列;但是实现的时候用队列来处理模拟栈实现队列效果
Deque<Integer> stack1;
Deque<Integer> stack2;
public _9CQueue() {
//多态;第一个栈用来插入,第二个用来删除
stack1 = new LinkedList<Integer>();
stack2 = new LinkedList<Integer>();
}
// 插入
public void appendTail(int value) {
stack1.push(value);
}
// 删除;当第二个栈不空就弹出;为空但第一个栈不空就按次序弹到第二个。弹完栈2在出一个;两个栈都空则-1
public int deleteHead() {
// 如果第二个栈为空
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
// 第一个栈的元素入第二个栈后,栈2还是空,-1无元素
if (stack2.isEmpty()) {
return -1;
} else {
// 栈2 不空就弹出
//pop弹出元素并且返回值
int deleteItem = stack2.pop();
return deleteItem;
}
}
}