Skip to content

Commit 3441163

Browse files
committed
链式栈、顺序栈
1 parent 35c5dfe commit 3441163

3 files changed

Lines changed: 188 additions & 38 deletions

File tree

Lines changed: 110 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,76 @@
11
package cn.edu.tju.rico.stack;
22

3+
import java.util.Comparator;
4+
35
/**
4-
* Title: 自定义链式栈
5-
* Description: 链式栈的实现
6-
*
6+
* Title: 增强自定义链式栈 [通过维护一个栈来保证用O(1)的时间复杂度求栈中的最小元素 (空间换取时间)]
7+
* Description: 使用额外的栈结构存储栈中的最小元素
8+
* 如果当前入栈的元素比原来栈中的最小值还小,则将其保存到最小值栈中;否则,不做任何操作。
9+
* 如果当前出栈的元素正好是当前栈中的最小值,那么将最小值栈中的该最小值也一并弹出;否则,不做任何操作。
710
* @author rico
811
* @created 2017年4月6日 下午7:52:34
912
*/
1013
public class LinkedStack<E> {
11-
14+
1215
private Node<E> top; // 栈顶元素
1316
private int size; // 链式栈的大小
14-
17+
18+
/** 最小值栈 (@author: rico) */
19+
private LinkedStack<E> min;
20+
1521
// 构造函数
1622
public LinkedStack(){
17-
1823
}
19-
20-
// 栈是否为空
24+
25+
/**
26+
* @description 判断栈是否为空
27+
* @author rico
28+
* @created 2017年5月14日 上午10:54:44
29+
* @return
30+
*/
2131
public boolean isEmpty() {
2232
return size == 0;
2333
}
2434

25-
// 压栈
35+
/**
36+
* @description 压栈
37+
* @author rico
38+
* @param data
39+
*/
2640
public void push(E data) {
27-
Node<E> node = new Node<E>(data, top); // 新加入的元素指向栈顶元素
41+
Node<E> node = new Node<E>(data);
42+
// 新加入的元素指向栈顶元素
43+
node.next = top;
2844
top = node;
2945
size++;
3046
}
47+
48+
/**
49+
* @description 压栈操作,使用最小值栈
50+
* @author rico
51+
* @param data
52+
*/
53+
public void push(E data, Comparator<? super E> c) {
54+
push(data);
55+
if(min == null){
56+
min = new LinkedStack<E>();
57+
}
58+
if(min.peek() == null){
59+
min.push(data);
60+
}else if(c.compare(this.peek().data, min.peek().data) < 0){
61+
min.push(data);
62+
}
63+
}
3164

32-
//弹栈(弹出并删除栈顶元素)
33-
public Node<E> pop() throws Exception {
65+
/**
66+
* @description 弹出并删除栈顶元素
67+
* @author rico
68+
* @return
69+
* @throws Exception
70+
*/
71+
public Node<E> pop(){
3472
if (isEmpty()) {
35-
throw new Exception("栈为空...");
73+
return null;
3674
}
3775

3876
Node<E> node = top;
@@ -41,41 +79,75 @@ public Node<E> pop() throws Exception {
4179
size--;
4280
return node;
4381
}
82+
83+
/**
84+
* @description 弹出并删除栈顶元素,使用最小值栈
85+
* @author rico
86+
* @return
87+
* @throws Exception
88+
*/
89+
public Node<E> pop(Comparator<? super E> c){
90+
Node<E> temp = this.pop();
91+
if(temp != null && min.peek() != null){
92+
if(c.compare(temp.data, min.peek().data) == 0){
93+
min.pop();
94+
}
95+
}
96+
return temp;
97+
}
98+
99+
/**
100+
* @description 弹出栈顶元素(不执行删除操作)
101+
* @author rico
102+
* @return
103+
*/
104+
public Node<E> peek(){
105+
if (isEmpty()) {
106+
return null;
107+
}
108+
return top;
109+
}
44110

45-
// 打印栈
111+
/**
112+
* @description 获取当前栈中的最小值
113+
* @author rico
114+
* @return
115+
*/
116+
public Node<E> min() {
117+
if(min.peek() == null){
118+
return null;
119+
}else{
120+
return min.peek();
121+
}
122+
}
123+
124+
/**
125+
* @description 打印栈
126+
* @author rico
127+
*/
46128
public void print() {
47129
Node<E> index = top;
48130
while (index != null) {
49131
System.out.print(index.data + " ");
50132
index = index.next;
51133
}
134+
System.out.println();
52135
}
53-
54-
55-
// 私有内部类
56-
private class Node<T> {
57-
private Node<T> next;
58-
private T data;
59-
60-
public Node(T data, Node<T> node) {
61-
this.data = data;
62-
next = node;
63-
}
136+
137+
/**
138+
* @description 返回栈的大小
139+
* @author rico
140+
* @return
141+
*/
142+
public int size(){
143+
return size;
64144
}
65145

66-
//测试
67-
public static void main(String[] args) throws Exception {
68-
LinkedStack<String> stack = new LinkedStack<String>();
69-
stack.push("Rico");
70-
stack.push("TJU");
71-
stack.push("Livia");
72-
stack.push("NEU");
73-
74-
stack.print();
75-
76-
System.out.println();
146+
public LinkedStack<E> getMin() {
147+
return min;
148+
}
77149

78-
stack.pop();
79-
stack.print();
150+
public void setMin(LinkedStack<E> min) {
151+
this.min = min;
80152
}
81153
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.edu.tju.rico.stack;
2+
3+
/**
4+
* Title: 结点类
5+
* Description: 链表的基本元素
6+
*
7+
* @author rico
8+
* @created 2017年4月6日 下午9:55:58
9+
*/
10+
public class Node<T> {
11+
//包可见性
12+
Node<T> next;
13+
T data;
14+
15+
/**
16+
* 构造函数
17+
*
18+
* @description 构造一个新节点
19+
* @author rico
20+
* @created 2017年4月6日 下午9:56:56
21+
* @param data
22+
* 新元素数据
23+
* @param next
24+
* 新元素与链表结合节点
25+
*/
26+
public Node(T data) {
27+
this.data = data;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return data.toString();
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.edu.tju.rico.test;
2+
3+
import java.util.Comparator;
4+
import cn.edu.tju.rico.stack.LinkedStack;
5+
6+
public class LinkedStackTest {
7+
// 测试
8+
public static void main(String[] args) throws Exception {
9+
10+
LinkedStack<Integer> stack = new LinkedStack<Integer>();
11+
Comparator<Integer> c = new Comparator<Integer>() {
12+
@Override
13+
public int compare(Integer o1, Integer o2) {
14+
// TODO Auto-generated method stub
15+
if(o1 > o2)
16+
return 1;
17+
else if(o1 < o2)
18+
return -1;
19+
else
20+
return 0;
21+
}
22+
};
23+
stack.push(7,c);
24+
stack.push(6,c);
25+
stack.push(8,c);
26+
stack.push(5,c);
27+
stack.push(3,c);
28+
29+
System.out.println("原栈(最左边元素是栈顶元素):");
30+
stack.print();
31+
System.out.println();
32+
33+
System.out.println("弹出栈顶元素:");
34+
System.out.println(stack.peek());
35+
System.out.println("栈中的最小值:" + stack.min());
36+
System.out.println();
37+
38+
System.out.println("弹出并删除栈顶元素后的链表:");
39+
stack.pop(c);
40+
stack.print();
41+
System.out.println("栈中的最小值:" + stack.min());
42+
System.out.println();
43+
}
44+
}

0 commit comments

Comments
 (0)