實現一個簡單的棧(底層鏈表)

棧的特色html

  1. 先進後出(FILO)或者  後進先出(LIFO)
  2. 增刪元素皆是在棧頂操做
  3. 一次只能刪除一個數據項:當前棧頂元素
  4. 只容許訪問一個數據項:當前棧頂元素

 

所需條件數組

  1. 一個鏈結點類Link,裏面包含了須要存儲的數據data和一個Link類型的引用變量next
  2. 一個封裝了鏈表操做元素的行爲類MyLinkList,裏面包含了一個Link類型的引用變量first和若干個方法

 

分析實現測試

 肯定棧具備的功能:入棧push()、出棧pop()、查看棧頂元素getTop()、判空isEmpty()、判長length()、清空clear()this


 

代碼實現spa

1. Link類code

 1 public class Link {
 2 
 3     public Object data;
 4     public Link next;
 5 
 6     public Link(Object data) {
 7         this.data = data;
 8         this.next = null;
 9     }
10 
11     public void displayLink() {
12         System.out.print(data + "\t");
13     }
14 }

 

2. MyLinkList類htm

 1 public class MyListList {
 2 
 3     private Link first;
 4 
 5     /**
 6      * 判斷鏈表是否爲空
 7      * @return
 8      */
 9     public boolean isEmpty() {
10         return (first == null);
11     }
12 
13     /**
14      * 在頭結點處插入元素
15      * @param data
16      */
17     public void insertFirst(Object data) {
18         Link newLink = new Link(data);
19         newLink.next = first;
20         first = newLink;
21     }
22 
23     /**
24      * 刪除頭結點
25      * @return
26      */
27     public Object deleteFirst() {
28         if (isEmpty()) {
29             return null;
30         }
31         Link temp = first;
32         first = first.next;
33         return temp.data;
34     }
35 
36     /**
37      * 得到頭結點存儲的數據
38      * @return
39      */
40     public Object getFirstData() {
41         return first.data;
42     }
43     
44     /**
45      * 得到鏈表的長度
46      * @return
47      */
48     public int length() {
49         Link current = first;
50         int count = 0;
51         while (current != null) {
52             count++;
53             current = current.next;
54         }
55         return count;
56     }
57 
58     /**
59      * 清空鏈表
60      */
61     public void clearList() {
62         Link current = first;
63         while (current != null) {
64             deleteFirst();
65             current = current.next;
66         }
67     }
68 }

 

3. 鏈棧類blog

 1 public class LinkStack {
 2 
 3     private MyListList myListList;
 4 
 5     public LinkStack() {
 6         this.myListList = new MyListList();
 7     }
 8 
 9     public boolean isEmpty(){
10         return myListList.isEmpty();
11     }
12 
13     public void push(Object data) {
14         myListList.insertFirst(data);
15     }
16 
17     public Object pop() {
18         return myListList.deleteFirst();
19     }
20 
21     public void clear() {
22         myListList.clearList();
23     }
24 
25     public int length() {
26         return myListList.length();
27     }
28 
29     public Object getTop() {
30         return myListList.getFirstData();
31     }
32 }

 

4. 測試get

 1 public class LinkStackTest {
 2 
 3     public static void main(String[] args) {
 4 
 5         LinkStack linkStack = new LinkStack();
 6 
 7         System.out.println( "棧是否爲空? " + linkStack.isEmpty());
 8 
 9         linkStack.push(10);
10         linkStack.push(20);
11         linkStack.push(30);
12         linkStack.push(0);
13 
14         System.out.println("棧長: " + linkStack.length());
15 
16         System.out.println("棧頂元素: " + linkStack.getTop());
17 
18         // 清空棧
19         linkStack.clear();
20 
21         System.out.println( "棧是否爲空? " + linkStack.isEmpty());
22 
23         linkStack.push(2);
24         linkStack.push(1);
25         linkStack.push(6);
26         linkStack.push(3);
27         linkStack.push(5);
28         // 取出棧元素,並打印
29         while(!linkStack.isEmpty()){
30             Object pop = linkStack.pop();
31             System.out.print(pop + "\t");
32         }
33         System.out.println();
34     }
35 }

 

5. 結果class

棧是否爲空? true
棧長: 4
棧頂元素: 0
棧是否爲空? true
5    3    6    1    2    

 


 

總結

  1. 使用鏈表做爲底層實現,能夠任意擴容,這一點比用數組實現好太多;
  2. 插入、查找、刪除所需時間都是O(1),由於都是對棧頂元素操做。

 

對比連接:使用數組實現棧

相關文章
相關標籤/搜索