實現一個簡單的LinkedList——高淇JAVA300講筆記之LinkedList,linkedlistjava300

來源:互聯網
上載者:User

實現一個簡單的LinkedList——高淇JAVA300講筆記之LinkedList,linkedlistjava300

    老高的這部分代碼其實寫的比較粗糙,比如頭結點和尾節點都考慮的不是很全面,不過敲一遍用來理解LinkedList的原理還是不錯的。實現了add()、get()、size()、remove()等方法。

    LinkedList也是有序,可重複的。底層實現是鏈表,線程不安全,效率高。查詢慢,修改、插入、刪除快。

    在寫的時候在紙上畫幾個節點會比較好理解一點。

    

    先是Node類,用來表示一個節點:

 1 package cn.bjsxt.collection; 2  3 //用來表示一個節點 4 public class Node { 5     Node previous;  //上一個節點 6     Object obj;  //該節點的對象 7     Node next;  //下一個節點 8      9     //無參構造器10     public Node() {11     }12     13     //有參構造器14     public Node(Node previous, Object obj, Node next) {15         super();16         this.previous = previous;17         this.obj = obj;18         this.next = next;19     }20 21     /**22      * 以下是getter和setter方法23      * @return24      */25     public Node getPrevious() {26         return previous;27     }28 29     public void setPrevious(Node previous) {30         this.previous = previous;31     }32 33     public Object getObj() {34         return obj;35     }36 37     public void setObj(Object obj) {38         this.obj = obj;39     }40 41     public Node getNext() {42         return next;43     }44 45     public void setNext(Node next) {46         this.next = next;47     }48 49 }

 

    然後是我們自己寫的LinkedList類:

  1 package cn.bjsxt.collection;  2   3 public class SxtLinkedList {  4     private Node first;  //前端節點  5     private Node last;  //尾節點  6       7     private int size;  //LinkedList的大小  8       9     //增加一個節點     10     public void add(Object obj) { 11         Node n = new Node(); 12          13         //如果沒有第一個節點 14         if(first == null) { 15             n.setPrevious(null); 16             n.setObj(obj); 17             n.setNext(null); 18              19             first = n; 20             last = n;  //這個節點既是第一個也是最後一個 21         } else { 22             //直接往last節點後增加新的節點 23             n.setPrevious(last);  //把最後一個節點設為新節點的上一個節點 24             n.setObj(obj); 25             n.setNext(null); 26              27             last.setNext(n);  //新節點設為當前最後一個節點的下一個節點 28              29             last = n;  //新節點成為最後一個節點 30      31         } 32         size++; 33     } 34      35   //在指定位置插入一個新對象 36     public void add(int index, Object obj) { 37         rangeCheck(index);  //檢查數組下標是否越界 38         Node temp = node(index);  //指定位置上的節點 39          40         Node newNode = new Node(); 41         newNode.obj = obj; 42          43         if(temp != null) { 44             Node up = temp.previous;  //原節點的上一個節點 45             up.next = newNode;  //新節點設為上一個節點的下一個節點 46             newNode.previous = up;  //上一個節點設為新節點的上一個節點 47              48             newNode.next = temp;  //原節點設為新節點的下一個節點 49             temp.previous = newNode;  //新節點設為原節點的上一個節點 50              51             size++; 52         } 53     } 54      55     //返回鏈表的大小 56     public int size() { 57         return size; 58     } 59      60     //數組下標越界檢查 61     private void rangeCheck(int index) { 62         if(index<0 || index>=size) { 63             try { 64                 throw new Exception(); 65             } catch (Exception e) { 66                 e.printStackTrace(); 67             } 68         } 69     } 70      71     //返回指定位置的元素 72     public Object get(int index) { 73         rangeCheck(index);  //檢查數組下標是否越界 74          75         Node temp = node(index); 76         if(temp != null) { 77             return temp.obj; 78         } 79         return null; 80     } 81      82     //返回指定位置的節點Node對象 83     public Node node(int index) { 84         Node temp = null; 85         if(first != null) { 86             temp = first; 87             for(int i=0;i<index;i++) { 88                 temp = temp.next; 89             } 90         } 91         return temp; 92     } 93      94      95     //刪除指定位置上的節點 96     public void remove(int index) { 97         Node temp = node(index); 98          99         if(temp != null) {100             Node up = temp.previous;101             Node down = temp.next;102             up.next = down;103             down.previous = up;104             size--;105         }106         107         108     }109     110     public static void main(String[] args) {111         SxtLinkedList list = new SxtLinkedList();112         list.add("aaa");113         list.add("bbb");114         list.add(1,"BBBB");115         System.out.println(list.size());116         System.out.println(list.get(2));117 //        list.remove(1);118         System.out.println(list.get(1));119     }120     121     122 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.