Skip to content

Commit 42bbd05

Browse files
committed
用两个栈实现队列[优化版]
1 parent 0d87347 commit 42bbd05

3 files changed

Lines changed: 89 additions & 4 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cn.edu.tju.rico.queue;
2+
3+
import cn.edu.tju.rico.stack.LinkedStack;
4+
import cn.edu.tju.rico.stack.Node;
5+
6+
/**
7+
* Title: 使用两个栈模拟一个队列
8+
* Description: 其中一个栈专门用作入队(始终不执行出队操作),另一个栈专门用作出队(始终不执行入队操作)
9+
* 此种解法相对于StackQueue性能要高不少,避免了反复“倒”栈,仅在需要时才“倒”一次!!!
10+
* @author rico
11+
* @created 2017年5月19日 下午10:45:11
12+
*/
13+
public class OptimizationStackQueue<E> {
14+
15+
private LinkedStack<E> stack1; // 入队栈
16+
private LinkedStack<E> stack2; // 出队栈
17+
18+
public OptimizationStackQueue() {
19+
stack1 = new LinkedStack<E>();
20+
stack2 = new LinkedStack<E>();
21+
}
22+
23+
/**
24+
* @description 添加元素到队尾,直接对stack1执行压栈操作。
25+
* @author rico
26+
* @created 2017年5月19日 下午10:47:59
27+
* @param e
28+
*/
29+
public void put(E e) {
30+
stack1.push(e);
31+
}
32+
33+
/**
34+
* @description 删除队头并返回队头元素的值。先检查stack2是否为空:
35+
* 若为空,先将stack1中的元素倒回stack2(stack1元素不弹出!),再对stack2执行弹栈操作
36+
* 否则,则直接对stack2执行弹栈操作
37+
* @author rico
38+
* @created 2017年5月19日 下午10:48:32
39+
* @return
40+
*/
41+
public E pop() {
42+
if (stack2.isEmpty()) {
43+
Node<E> temp = stack1.peek(); // Stack1始终不执行弹栈操作,peek:获取元素但不删除
44+
while (temp != null) {
45+
stack2.push(temp.getData());
46+
temp = temp.getNext();
47+
}
48+
}
49+
return stack2.pop().getData();
50+
}
51+
52+
@Override
53+
public String toString() {
54+
String str = new StringBuilder(stack1.toString()).reverse().toString();
55+
if(stack2.isEmpty()){
56+
return str;
57+
}
58+
return str.substring(str.indexOf(stack2.peek().getData().toString()));
59+
}
60+
}

src/cn/edu/tju/rico/stack/Node.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public T getData() {
3131
return data;
3232
}
3333

34-
public void setData(T data) {
35-
this.data = data;
36-
}
37-
3834
@Override
3935
public String toString() {
4036
return data.toString();
4137
}
4238

39+
public Node<T> getNext() {
40+
return next;
41+
}
42+
4343
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.edu.tju.rico.test;
2+
3+
import cn.edu.tju.rico.queue.OptimizationStackQueue;
4+
5+
public class OptimizationStackQueueTest {
6+
public static void main(String[] args) {
7+
OptimizationStackQueue<Integer> queue = new OptimizationStackQueue<Integer>();
8+
queue.put(1);
9+
queue.put(3);
10+
queue.put(5);
11+
queue.put(2);
12+
queue.put(8);
13+
queue.put(6);
14+
System.out.println(queue);
15+
System.out.println("\n------------------\n");
16+
17+
queue.pop();
18+
System.out.println(queue);
19+
System.out.println("\n------------------\n");
20+
21+
queue.put(4);
22+
System.out.println(queue);
23+
System.out.println("\n------------------\n");
24+
}
25+
}

0 commit comments

Comments
 (0)