11package 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 */
1013public 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}
0 commit comments