Java數據結構-02單鏈表

一.鏈式存儲: node

    ①簡述:線性表的鏈式存儲結構的特色是用一組任意的存儲單元存儲線性表的數據元素,這組存儲單元能夠是連續的,也能夠是不連續的。存儲單元由兩部分組成,數據源和指針,數據源放數據,指針指向下個存儲單元(如圖)。LinkedList採用的就是鏈式存儲線性表。數組

    

 

    ②特色:數據是非連續的,鏈表的運輸必須從頭指針開始;ui

    ③與順序表的區別:this

      鏈式線性表刪除和插入效率高,查詢效率低
      順序表查詢效率高,刪除和插入效率低。spa

    ④單鏈表相關操做:設計

      A.插入操做3d

        

 

 

       B.刪除操做指針

        

 

    ⑤代碼實現: code

  1 package com.atguigu.test03;
  2 
  3 public class SingleLinkedList {
  4     //這裏不須要數組,不須要其餘的複雜的結構,我只要記錄單向鏈表的「頭」結點
  5     private Node first;//first中記錄的是第一個結點的地址
  6     private int total;//這裏我記錄total是爲了後面處理的方便,例如:當用戶獲取鏈表有效元素的個數時,不用現數,而是直接返回total等
  7     
  8     
  9     private class Node{
 10         Object data;//由於數據能夠是任意類型的對象,因此設計爲Object
 11         Node next;//由於next中記錄的下一個結點的地址,所以類型是結點類型
 12         
 13         Node(Object data, Node next){
 14             this.data = data;
 15             this.next = next;
 16         }
 17     }
 18     
 19     public void add(Object obj){
 20     
 21         Node newNode = new Node(obj, null);
 22         
 23         //①當前新結點是第一個結點
 24         if(first == null){
 25             //說明newNode是第一個
 26             first = newNode;
 27         }else{
 28             //②先找到目前的最後一個,把新結點連接到它的next中
 29             Node node = first;
 30             while(node.next != null){
 31                 node = node.next;
 32             }
 33             //退出循環時node指向最後一個結點
 34             
 35             //把新結點連接到它的next中
 36             node.next = newNode;
 37         }
 38         
 39         total++;
 40     }
 41     
 42     public int size(){
 43         return total;
 44     }
 45     
 46     public Object[] getAll(){
 47         //(1)建立一個數組,長度爲total
 48         Object[] all = new Object[total];
 49         
 50         //(2)把單向鏈表的每個結點中的data,拿過來放到all數組中
 51         Node node = first;
 52         for (int i = 0; i < total; i++) {
 53 //            all[i] = 結點.data;
 54             all[i] = node.data;
 55             //而後node指向下一個
 56             node = node.next;
 57         }
 58         
 59         //(3)返回數組
 60         return all;
 61     }
 62     
 63     public void remove(Object obj){
 64         if(obj == null){
 65             //(1)先考慮是不是第一個
 66             if(first!=null){//鏈表非空
 67                 
 68                 //要刪除的結點正好是第一個結點
 69                 if(first.data == null){
 70                     //讓第一個結點指向它的下一個
 71                     first = first.next;
 72                     total--;
 73                     return;
 74                 }
 75                 
 76                 //要刪除的不是第一個結點
 77                 Node node = first.next;//第二個結點
 78                 Node last = first;
 79                 while(node.next!=null){//這裏不包括最後一個,由於node.next==null,不進入循環,而node.next==null是最後一個
 80                     if(node.data == null){
 81                         last.next = node.next;
 82                         total--;
 83                         return;
 84                     }
 85                     last = node;
 86                     node = node.next;
 87                 }
 88                 
 89                 //單獨判斷最後一個是不是要刪除的結點
 90                 if(node.data == null){
 91                     //要刪除的是最後一個結點
 92                     last.next = null;
 93                     total--;
 94                     return;
 95                 }
 96             }
 97         }else{
 98             //(1)先考慮是不是第一個
 99             if(first!=null){//鏈表非空
100                 
101                 //要刪除的結點正好是第一個結點
102                 if(obj.equals(first.data)){
103                     //讓第一個結點指向它的下一個
104                     first = first.next;
105                     total--;
106                     return;
107                 }
108                 
109                 //要刪除的不是第一個結點
110                 Node node = first.next;//第二個結點
111                 Node last = first;
112                 while(node.next!=null){//這裏不包括最後一個,由於node.next==null,不進入循環,而node.next==null是最後一個
113                     if(obj.equals(node.data)){
114                         last.next = node.next;
115                         total--;
116                         return;
117                     }
118                     last = node;
119                     node = node.next;
120                 }
121                 
122                 //單獨判斷最後一個是不是要刪除的結點
123                 if(obj.equals(node.data)){
124                     //要刪除的是最後一個結點
125                     last.next = null;
126                     total--;
127                     return;
128                 }
129             }
130         }
131     }
132 
133     public int indexOf(Object obj){
134         if(obj == null){
135             Node node = first;
136             for (int i = 0; i < total; i++) {
137                 if(node.data == null){
138                     return i;
139                 }
140                 node = node.next;
141             }
142         }else{
143             Node node = first;
144             for (int i = 0; i < total; i++) {
145                 if(obj.equals(node.data)){
146                     return i;
147                 }
148                 node = node.next;
149             }
150         }
151         return -1;
152     }
153 }
相關文章
相關標籤/搜索