日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
數(shù)據(jù)結(jié)構(gòu)線性結(jié)構(gòu)篇—鏈表

[[399074]]

一、前言

在前面兩章我們講解了動(dòng)態(tài)數(shù)組、棧和隊(duì)列的講解,這些底層都是依托靜態(tài)數(shù)組,靠 resize解決固定容量問題的,之前雖然用戶看到的是動(dòng)態(tài)數(shù)組,但是依然使用的是靜態(tài)數(shù)組,他是依靠 resize 這個(gè)方法解決 固定容量問題 ,但是我們今天要講解的 鏈表 不一樣,鏈表是我們數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)的一個(gè)重點(diǎn),也有可能是一個(gè)難點(diǎn),為什么鏈表這么重要呢?因?yàn)樗亲詈?jiǎn)單的也是 真正的動(dòng)態(tài)數(shù)據(jù)結(jié)構(gòu)。

二、為什么鏈表很重要

  • 鏈表是一個(gè)真正的動(dòng)態(tài)數(shù)據(jù)結(jié)構(gòu)
  • 最簡(jiǎn)單的動(dòng)態(tài)數(shù)據(jù)結(jié)構(gòu)
  • 更深入的理解引用(或者指針)
  • 更深入的理解遞歸
  • 輔助組成其他數(shù)據(jù)結(jié)構(gòu)

更深入的理解引用(或者指針):和內(nèi)存相關(guān),雖然在 java 中大家不用手動(dòng)的管理內(nèi)存,但是對(duì) 鏈表 這種數(shù)據(jù)結(jié)構(gòu),更加深入的理解,可以幫助大家對(duì)引用、指針、甚至計(jì)算機(jī)系統(tǒng)中和內(nèi)存管理相關(guān)的很多話題,有更加深入的認(rèn)識(shí)。

更深入的理解遞歸:鏈表 本來也是有他非常清晰的遞歸結(jié)構(gòu)的,、由于 鏈表 這種數(shù)據(jù)結(jié)構(gòu)是 數(shù)據(jù)結(jié)構(gòu),我們可以更加 深入理解遞歸,對(duì)于遞歸這種深入理解是不可獲取的。

鏈表 本身也是具有功能性:輔助組成其他數(shù)據(jù)結(jié)構(gòu)(hashMap 、棧和隊(duì)列)

三、什么是鏈表

鏈表 是一種數(shù)據(jù)結(jié)構(gòu),在內(nèi)存中通過 節(jié)點(diǎn)記錄內(nèi)存地址 而相互鏈接形成一條鏈的儲(chǔ)存方式。相比數(shù)組而言,鏈表在內(nèi)存中不需要連續(xù)的區(qū)域,只需要每一個(gè)節(jié)點(diǎn)都能夠 記錄下一個(gè)節(jié)點(diǎn) 的 內(nèi)存地址 ,通過 引用 進(jìn)行查找,這樣的特點(diǎn)也就造就了 鏈表 增刪操作時(shí)間消耗很小,而查找遍歷時(shí)間消耗很大的特點(diǎn)。

我們?nèi)粘T?Java 中使用的 LinkedList 即為 雙向鏈表。而在鏈表是由其基本組成單元節(jié)點(diǎn) (Node) 來實(shí)現(xiàn)的。我們?cè)谌粘V幸姷降逆湵泶蟛糠侄际?單鏈表和雙鏈表

單元節(jié)點(diǎn) (Node):

 
 
 
 
  1. class Node{ 
  2.   E e; 
  3.   Node next; 

e 就是鏈表元素

next 指的是當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)

對(duì)于 鏈表 來說它就像我們的火車一樣,每一個(gè)節(jié)點(diǎn)其實(shí)就是一節(jié)車廂,我們?cè)谲噹写鎯?chǔ)真正的數(shù)據(jù),而車廂和車廂之間還要進(jìn)行連接,讓我們數(shù)據(jù)是整合在一起的,用戶可以方便的在所有的數(shù)據(jù)上進(jìn)行查詢或其他操作,那么 數(shù)據(jù)和數(shù)據(jù)連接 就是由這個(gè) next 來完成的

當(dāng)然 鏈表 不能無窮無盡,如果一個(gè)節(jié)點(diǎn)的 next 是 Null 了,就說明這個(gè)節(jié)點(diǎn)是最后一個(gè)節(jié)點(diǎn)了,這就是 鏈表

如下圖所示(單鏈表):

鏈表的優(yōu)點(diǎn):真正的動(dòng)態(tài),不需要處理固定容量的問題鏈表的缺點(diǎn):?jiǎn)适Я穗S機(jī)訪問的能力

在數(shù)組中:每一個(gè)索引,直接從數(shù)組中拿出索引對(duì)應(yīng)的元素,這是因?yàn)閺牡讓訖C(jī)制上,數(shù)組所開辟的空間,在內(nèi)存里是連續(xù)分布的,所以我們可以直接可以去找這個(gè)數(shù)組的偏移,直接計(jì)算出這個(gè)數(shù)據(jù)所存儲(chǔ)的內(nèi)存地址,可以直接使用。

鏈表:而鏈表是靠 Next 一層一層連接的,需要借助這個(gè) Next 一點(diǎn)一點(diǎn)的去找我們需要取出來的元素。

四、創(chuàng)建我們自己的鏈表

4.1 鏈表基本結(jié)構(gòu)

 
 
 
 
  1. /** 
  2.  * 底層鏈表的內(nèi)部類 
  3.  * @param  
  4.  */ 
  5. public class LinkedList { 
  6.  
  7.     //設(shè)計(jì)私有的內(nèi)部類,對(duì)于用戶來說不需要知道鏈表底層實(shí)現(xiàn), 
  8.     // 不需要知道node這個(gè)節(jié)點(diǎn),對(duì)用戶屏蔽編碼實(shí)現(xiàn)的底層實(shí)現(xiàn) 
  9.     private class Node{ 
  10.         public E e; 
  11.         public Node next;//public 可以在LinkedList隨意操作 
  12.  
  13.         public Node(E e,Node next){ 
  14.             this.e = e; 
  15.             this.next = next; 
  16.         } 
  17.  
  18.         public Node(E e){ 
  19.             this(e,null); 
  20.         } 
  21.  
  22.         public Node(){ 
  23.             this(null,null); 
  24.         } 
  25.  
  26.         @Override 
  27.         public String toString() { 
  28.             return e.toString(); 
  29.         } 
  30.     } 

內(nèi)部類Node:設(shè)計(jì)私有的內(nèi)部類,對(duì)于用戶來說不需要知道鏈表底層實(shí)現(xiàn),不需要知道node這個(gè)節(jié)點(diǎn),對(duì)用戶屏蔽編碼實(shí)現(xiàn)的底層實(shí)現(xiàn)e:元素next:指向Node的一個(gè)引用

4.2 添加元素

之前我們講的是如何在數(shù)組中添加元素,我們?cè)跀?shù)組尾添加元素是非常方便的,因?yàn)閷?duì)于數(shù)組來說是順序排放的,有意思的是對(duì)于鏈表來說,恰恰相反,在鏈表頭添加元素是非常方便的,其實(shí)這樣非常好理解,對(duì)于數(shù)組來說我們有 size 這個(gè)變量,它直接指向了數(shù)組中最后一個(gè)元素下一個(gè)位置,也就是下一個(gè)待添加元素的位置,所以直接添加就非常容易,因?yàn)橛?size 這個(gè)變量,在跟蹤數(shù)組的尾巴,而對(duì)于鏈表來說我們?cè)O(shè)立了鏈表的一個(gè)頭 head ,而沒有變量來跟蹤鏈表的尾巴,所以我們?cè)阪湵眍^添加元素是非常方便的,最關(guān)鍵的就是 node.next = head 和 head = node,如下圖所示:

4.2.1 鏈表頭添加元素

代碼實(shí)現(xiàn):

 
 
 
 
  1. //在鏈表頭中添加元素e 
  2.   public void addFirst(E e){ 
  3.   //方式一 
  4.   //        Node node = new Node(e); 
  5.   //        node.next = head; 
  6.   //        head = node; 
  7.   //方式二 
  8.       head = new Node(e,head); 
  9.       size ++; 
  10.   } 

4.2.2 鏈表中間添加元素

我們需要在索引為2的地方添加元素 666,我們只需要找到 元素666要 插入之前的節(jié)點(diǎn)(1) ,我們管它叫 prev,然后把 之前節(jié)點(diǎn)的(1) next 指向 666,然后在將 666的這個(gè) 節(jié)點(diǎn)指向之前節(jié)點(diǎn)(1) 的 之后的節(jié)點(diǎn)(2) ,就完成了整個(gè)插入了,其中關(guān)鍵代碼就是 node.next=prev.next和prev.next=node;,其中關(guān)鍵:我們要找到添加節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn) 。

代碼實(shí)現(xiàn):

 
 
 
 
  1.  //在鏈表的index(0-based)位置添加新的元素e 
  2.     public void add(int index,E e){ 
  3.         if(index < 0 || index > size) 
  4.             throw new IllegalArgumentException("Add failed. Illegal index."); 
  5.  
  6.         if(index == 0) 
  7.             addFirst(e); 
  8.         else{ 
  9.             Node prev = head; 
  10.             for (int i = 0; i < index - 1; i++) {//將prev 放入下一個(gè)節(jié)點(diǎn),直到移動(dòng)到index - 1 
  11.                 prev = prev.next; 
  12.  
  13.                 //方式一 
  14. //                Node node = new Node(e); 
  15. //                node.next = prev.next; 
  16. //                prev.next = node; 
  17.  
  18.                 //方式二 
  19.                 prev.next = new Node(e,prev.next); 
  20.                 size++; 
  21.             } 
  22.         } 
  23.     } 
  24.  
  25.  //在鏈表末尾添加新的元素e 
  26.     public void addLast(E e){ 
  27.         add(size,e); 
  28.     } 

4.2.3 添加操作時(shí)間復(fù)雜度

4.3 為鏈表設(shè)計(jì)虛擬頭結(jié)點(diǎn)

上面我們介紹了鏈表的添加操作,那么我們?cè)谔砑拥臅r(shí)候遇到了一個(gè)問題,就是在鏈表任意一個(gè)地方的時(shí)候,添加一個(gè)元素,在鏈表頭添加一個(gè)元素,和在鏈表其他地方添加元素,邏輯上會(huì)有差別,為什么在鏈表頭添加元素會(huì)比較特殊呢,因?yàn)槲覀冊(cè)阪湵硖砑釉氐倪^程,要找到待添加的 之前的一個(gè)節(jié)點(diǎn),但是由于對(duì)于鏈表頭沒有之前的一個(gè)節(jié)點(diǎn),不過我們可以自己創(chuàng)建一個(gè)頭結(jié)點(diǎn),這個(gè)頭節(jié)點(diǎn)就是 虛擬頭結(jié)點(diǎn),這個(gè)節(jié)點(diǎn)對(duì)于用戶來說是不存在, 用戶也不會(huì)感知到這個(gè)節(jié)點(diǎn)的存在,我們是屏蔽了這個(gè)節(jié)點(diǎn)的存在,如下圖所示:

代碼實(shí)現(xiàn):

 
 
 
 
  1. private Node dummyHead; 
  2.    int size; 
  3.  
  4.    public LinkedList(){ 
  5.        dummyHead = new Node(null,null); 
  6.        size = 0; 
  7.    } 
  8.  
  9.  
  10.    //獲取鏈表中的元素個(gè)數(shù) 
  11.    public int getSize(){ 
  12.        return size; 
  13.    } 
  14.  
  15.    //返回鏈表是否為空 
  16.    public boolean isEmpty(){ 
  17.        return size == 0; 
  18.    } 
  19.    //在鏈表的index(0-based)位置添加新的元素e 
  20.    public void add(int index,E e){ 
  21.  
  22.        if(index < 0 || index > size) 
  23.            throw new IllegalArgumentException("Add failed. Illegal index."); 
  24.  
  25.        Node prev = dummyHead; 
  26.        for (int i = 0; i < index; i++) 
  27.            prev = prev.next; 
  28.  
  29.        prev.next = new Node(e,prev.next); 
  30.        size ++; 
  31.    } 
  32.  
  33.  //在鏈表頭中添加元素e 
  34.    public void addFirst(E e){ 
  35.        add(0,e); 
  36.    } 
  37.  
  38.    //在鏈表末尾添加新的元素e 
  39.    public void addLast(E e){ 
  40.        add(size,e); 
  41.    } 

4.4 鏈表元素 get、set、是否存在操作

 
 
 
 
  1. //在鏈表的index(0-based)位置添加新的元素e 
  2.    public E get(int index){ 
  3.        if(index < 0 || index > size) 
  4.            throw new IllegalArgumentException("Get failed. Illegal index."); 
  5.  
  6.        Node cur = dummyHead.next; 
  7.        for (int i = 0; i < index; i++) 
  8.            cur = cur.next; 
  9.  
  10.        return cur.e; 
  11.    } 
  12.  
  13.    //獲得鏈表的第一個(gè)元素 
  14.    public E getFirst(){ 
  15.        return get(0); 
  16.    } 
  17.  
  18.    //獲取鏈表的最后一個(gè)元素 
  19.    public E getLast(){ 
  20.        return get(size - 1); 
  21.    } 
  22.  
  23.    //在鏈表的index(0-based)位置添加新的元素e 
  24.    public void set(int index,E e){ 
  25.        if(index < 0 || index > size) 
  26.            throw new IllegalArgumentException("Set failed. Illegal index."); 
  27.  
  28.        Node cur = dummyHead.next; 
  29.        for (int i = 0; i < index; i++) 
  30.            cur = cur.next; 
  31.  
  32.        cur.e = e; 
  33.    } 
  34.  
  35.    //查找鏈表中是否有元素e 
  36.    public boolean contains(E e){ 
  37.        Node cur = dummyHead.next; 
  38.        while (cur != null){ 
  39.            if(cur.e.equals(e)) 
  40.                return  true; 
  41.            cur = cur.next; 
  42.        } 
  43.        return false; 
  44.    } 

4.5.1 修改和查找操作時(shí)間復(fù)雜度

4.5 刪除鏈表元素

加入我們想要?jiǎng)h除索引為 (2) 位置的元素,我們需要找到 待刪除節(jié)點(diǎn)之前的一個(gè)位置,也就是(1) ,我們用 prev 表示,找到這個(gè)節(jié)點(diǎn)之后,那么 (2) 就是我們需要?jiǎng)h除的索引了 我們叫 delNode,如下圖所示:

代碼實(shí)現(xiàn):

 
 
 
 
  1. //從鏈表中刪除Index(0-based)位置的元素,返回刪除的元素 
  2.     public E remove(int index){ 
  3.         if(index < 0 || index > size) 
  4.             throw new IllegalArgumentException("Remove failed. Illegal index."); 
  5.  
  6.         Node prev = dummyHead; 
  7.         for (int i = 0; i < index; i++) 
  8.             prev = prev.next; 
  9.  
  10.         Node retNode = prev.next; 
  11.         prev.next = retNode.next; 
  12.         retNode.next = null; 
  13.  
  14.         size --; 
  15.  
  16.         return  retNode.e; 
  17.  
  18.     } 
  19.  
  20.     //從鏈表中刪除第一個(gè)位置的元素 
  21.     public E removeFirst(){ 
  22.         return remove(0); 
  23.     } 
  24.  
  25.     //從鏈表中刪除最后一個(gè)位置的元素 
  26.     public E removeLast(){ 
  27.         return remove(size - 1); 
  28.     } 

4.5.1 刪除操作時(shí)間復(fù)雜度

4.6 完整代碼

 
 
 
 
  1. /** 
  2.  * 底層鏈表的內(nèi)部類 
  3.  * @param  
  4.  */ 
  5. public class LinkedList { 
  6.  
  7.     private class Node{ 
  8.         public E e; 
  9.         public Node next;//public 可以在LinkedList隨意操作 
  10.  
  11.         public Node(E e,Node next){ 
  12.             this.e = e; 
  13.             this.next = next; 
  14.         } 
  15.  
  16.         public Node(E e){ 
  17.             this(e,null); 
  18.         } 
  19.  
  20.         public Node(){ 
  21.             this(null,null); 
  22.         } 
  23.  
  24.         @Override 
  25.         public String toString() { 
  26.             return e.toString(); 
  27.         } 
  28.     } 
  29.  
  30.  
  31.     private Node dummyHead; 
  32.     int size; 
  33.  
  34.     public LinkedList(){ 
  35.         dummyHead = new Node(null,null); 
  36.         size = 0; 
  37.     } 
  38.  
  39.  
  40.     //獲取鏈表中的元素個(gè)數(shù) 
  41.     public int getSize(){ 
  42.         return size; 
  43.     } 
  44.  
  45.     //返回鏈表是否為空 
  46.     public boolean isEmpty(){ 
  47.         return size == 0; 
  48.     } 
  49.  
  50.  
  51.  
  52.  
  53.     //在鏈表頭中添加元素e 
  54.     public void addFirst(E e){ 
  55. //方式一 
  56. //        Node node = new Node(e); 
  57. //        node.next = head; 
  58. //        head = node; 
  59. //方式二 
  60.         add(0,e); 
  61.     } 
  62.  
  63.     //在鏈表的index(0-based)位置添加新的元素e 
  64.     public void add(int index,E e){ 
  65.  
  66.         if(index < 0 || index > size) 
  67.             throw new IllegalArgumentException("Add failed. Illegal index."); 
  68.  
  69.         Node prev = dummyHead; 
  70.         for (int i = 0; i < index; i++) 
  71.             prev = prev.next; 
  72.  
  73.         prev.next = new Node(e,prev.next); 
  74.         size ++; 
  75.     } 
  76.  
  77.     //在鏈表末尾添加新的元素e 
  78.     public void addLast(E e){ 
  79.         add(size,e); 
  80.     } 
  81.  
  82.     //在鏈表的index(0-based)位置添加新的元素e 
  83.     public E get(int index){ 
  84.         if(index < 0 || index > size) 
  85.             throw new IllegalArgumentException("Get failed. Illegal index."); 
  86.  
  87.         Node cur = dummyHead.next; 
  88.         for (int i = 0; i < index; i++) 
  89.             cur = cur.next; 
  90.  
  91.         return cur.e; 
  92.     } 
  93.  
  94.     //獲得鏈表的第一個(gè)元素 
  95.     public E getFirst(){ 
  96.         return get(0); 
  97.     } 
  98.  
  99.     //獲取鏈表的最后一個(gè)元素 
  100.     public E getLast(){ 
  101.         return get(size - 1); 
  102.     } 
  103.  
  104.     //在鏈表的index(0-based)位置添加新的元素e 
  105.     public void set(int index,E e){ 
  106.         if(index < 0 || index > size) 
  107.             throw new IllegalArgumentException("Set failed. Illegal index."); 
  108.  
  109.         Node cur = dummyHead.next; 
  110.         for (int i = 0; i < index; i++) 
  111.             cur = cur.next; 
  112.  
  113.         cur.e = e; 
  114.     } 
  115.  
  116.     //查找鏈表中是否有元素e 
  117.     public boolean contains(E e){ 
  118.         Node cur = dummyHead.next; 
  119.         while (cur != null){ 
  120.             if(cur.e.equals(e)) 
  121.                 return  true; 
  122.             cur = cur.next; 
  123.         } 
  124.         return false; 
  125.     } 
  126.  
  127.  
  128.     //從鏈表中刪除Index(0-based)位置的元素,返回刪除的元素 
  129.     public E remove(int index){ 
  130.         if(index < 0 || index > size) 
  131.             throw new IllegalArgumentException("Remove failed. Illegal index."); 
  132.  
  133.         Node prev = dummyHead; 
  134.         for (int i = 0; i < index; i++) 
  135.             prev = prev.next; 
  136.  
  137.         Node retNode = prev.next; 
  138.         prev.next = retNode.next; 
  139.         retNode.next = null; 
  140.  
  141.         size --; 
  142.  
  143.         return  retNode.e; 
  144.  
  145.     } 
  146.  
  147.     //從鏈表中刪除第一個(gè)位置的元素 
  148.     public E removeFirst(){ 
  149.         return remove(0); 
  150.     } 
  151.  
  152.     //從鏈表中刪除最后一個(gè)位置的元素 
  153.     public E removeLast(){ 
  154.         return remove(size - 1); 
  155.     } 
  156.  
  157.     @Override 
  158.     public String toString() { 
  159.  
  160.         StringBuilder res = new StringBuilder(); 
  161.         for (Node cur = dummyHead.next;cur != null; cur= cur.next) 
  162.             res.append(cur + "->"); 
  163.  
  164.         res.append("Null"); 
  165.         return res.toString(); 
  166.     } 
  167.  

4.2.7 結(jié)果測(cè)試:

 
 
 
 
  1. public static void main(String[] args) { 
  2.         LinkedList linkedList = new LinkedList<>(); 
  3.         //添加元素 0-4 
  4.         for (int i = 0; i < 5 ; i++) { 
  5.             linkedList.addFirst(i); 
  6.             System.out.println(linkedList); 
  7.         } 
  8.  
  9.         //添加第二個(gè)元素添加 666 
  10.         linkedList.add(2,666); 
  11.         System.out.println(linkedList); 
  12.  
  13.         //刪除第二個(gè)元素 666 
  14.         linkedList.remove(2); 
  15.         System.out.println(linkedList); 
  16.  
  17.         //刪除第一個(gè)元素 
  18.         linkedList.removeFirst(); 
  19.         System.out.println(linkedList); 
  20.  
  21.         //刪除最后一個(gè)元素 
  22.         linkedList.removeLast(); 
  23.         System.out.println(linkedList); 
  24.     } 

打印結(jié)果:

 
 
 
 
  1. 0->Null 
  2. 1->0->Null 
  3. 2->1->0->Null 
  4. 3->2->1->0->Null 
  5. 4->3->2->1->0->Null 
  6. 4->3->666->2->1->0->Null 
  7. 4->3->2->1->0->Null 
  8. 3->2->1->0->Null 
  9. 3->2->1->Null 

四、鏈表時(shí)間復(fù)雜度分析

對(duì)于增加和刪除來說,如果是對(duì)鏈表頭進(jìn)行操作,那么就是 O(1) 級(jí)別的復(fù)雜度,對(duì)于查詢來說,也是一樣

五、鏈表應(yīng)用

5.1 使用棧實(shí)現(xiàn)鏈表

5.1.1 接口類:

 
 
 
 
  1. /** 
  2.  * @program: Data-Structures 
  3.  * @ClassName Stack 
  4.  * @description: 
  5.  * @author: lyy 
  6.  * @create: 2019-11-20 21:51 
  7.  * @Version 1.0 
  8.  **/ 
  9. public interface Stack { 
  10.  
  11.     int getSize(); 
  12.     boolean isEmpty(); 
  13.     void push(E e); 
  14.     E pop(); 
  15.     E peek(); 
  16.  

5.1.2 實(shí)現(xiàn)類:

 
 
 
 
  1. import com.lyy.datasty.Mystack.Stack; 
  2.  
  3. //鏈表?xiàng)?shí)現(xiàn) 
  4. public class LinkedListStack implements Stack { 
  5.  
  6.     private LinkedList1 list; 
  7.  
  8.     public LinkedListStack(){ 
  9.         list = new LinkedList1<>(); 
  10.     } 
  11.  
  12.  
  13.     @Override 
  14.     public int getSize() { 
  15.         return list.getSize(); 
  16.     } 
  17.  
  18.     @Override 
  19.     public boolean isEmpty() { 
  20.         return list.isEmpty(); 
  21.     } 
  22.  
  23.     @Override 
  24.     public void push(E e) { 
  25.         list.addFirst(e); 
  26.     } 
  27.  
  28.     @Override 
  29.     public E pop() { 
  30.         return list.removeFirst(); 
  31.     } 
  32.  
  33.     @Override 
  34.     public E peek() { 
  35.         return list.getFirst(); 
  36.     } 
  37.  
  38.     @Override 
  39.     public String toString() { 
  40.  
  41.         StringBuilder res = new StringBuilder(); 
  42.         res.append("Stack:top  "); 
  43.         res.append(list); 
  44.         return res.toString(); 
  45.     } 
  46.  
  47.    

5.1.3 運(yùn)行結(jié)果:

 
 
 
 
  1. public static void main(String[] args) { 
  2.         LinkedListStack stack = new LinkedListStack<>(); 
  3.         for (int i = 0; i < 5; i++) { 
  4.             stack.push(i); 
  5.             System.out.println(stack); 
  6.         } 
  7.         stack.pop(); 
  8.         System.out.println(stack); 
  9.  
  10.  
  11.     } 

5.1.4 結(jié)果打印:

 
 
 
 
  1. Stack:top  0->Null 
  2. Stack:top  1->0->Null 
  3. Stack:top  2->1->0->Null 
  4. Stack:top  3->2->1->0->Null 
  5. Stack:top  4->3->2->1->0->Null 
  6. Stack:top  3->2->1->0->Null 

5.2 使用鏈表實(shí)現(xiàn)隊(duì)列

5.2.1 接口類

 
 
 
 
  1. /** 
  2.  * @program: Data-Structures 
  3.  * @ClassName Queue 
  4.  * @description: 
  5.  * @author: lyy 
  6.  * @create: 2019-11-21 21:54 
  7.  * @Version 1.0 
  8.  **/ 
  9. public interface Queue { 
  10.  
  11.     int getSize(); 
  12.     boolean isEmpty(); 
  13.     void enqueue(E e); 
  14.     E dequeue(); 
  15.     E getFront(); 
  16.  
  17.  

5.2.2 實(shí)現(xiàn)類

 
 
 
 
  1. public class LinkedListQueue implements Queue
  2.  
  3.     //設(shè)計(jì)私有的內(nèi)部類,對(duì)于用戶來說不需要知道鏈表底層實(shí)現(xiàn), 
  4.     // 不需要知道node這個(gè)節(jié)點(diǎn),對(duì)用戶屏蔽編碼實(shí)現(xiàn)的底層實(shí)現(xiàn) 
  5.     private class Node{ 
  6.         public E e; 
  7.         public Node next;//public 可以在LinkedList隨意操作 
  8.  
  9.         public Node(E e, Node next){ 
  10.             this.e = e; 
  11.             this.next = next; 
  12.         } 
  13.  
  14.         public Node(E e){ 
  15.             this(e,null); 
  16.         } 
  17.  
  18.         public Node(){ 
  19.             this(null,null); 
  20.         } 
  21.  
  22.         @Override 
  23.         public String toString() { 
  24.             return e.toString(); 
  25.         } 
  26.     } 
  27.  
  28.     private Node head,tail; 
  29.     private int size; 
  30.  
  31.     public LinkedListQueue(){ 
  32.         head = null; 
  33.         tail = null; 
  34.         size = 0; 
  35.     } 
  36.  
  37.  
  38.     @Override 
  39.     public int getSize() { 
  40.         return&nb
    網(wǎng)站欄目:數(shù)據(jù)結(jié)構(gòu)線性結(jié)構(gòu)篇—鏈表
    鏈接URL:http://www.dlmjj.cn/article/djhcedo.html