JAVA數據結構之鏈表【有dummyNode】

LinkedList

Node結點

  • data域
  • 下個結點的指向

Node結點代碼

private class Node {
    public E e;
    public Node next;

    public Node(E e, Node next) {
        this.e = e;
        this.next = next;
    }

    public Node(E e) {
        this(e, null);
    }

    public Node() {
        this(null, null);
    }

    @Override
    public String toString() {
        return e.toString();
    }
}

鏈表結構表達

  • 鏈表中需要頭結點
    • Node head = new Node();
  • 需要size記錄擁有的元素個數
public class LinkedList<E> {
    private Node dummyHead;
    private int size;

    /***構造函數***/
    //只需要空參構造函數
    public LinkedList(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    /*******增*******/
    // 這因爲,頭結點和中間結點有差異,所以需要分別討論。此處可以優化

    public void add(int index, E e){
    //判斷index是否正確
        if(index < 0 || index>size){
            throw new IllegalArgumentException("Add failed.Illegal index");
        }

    //當不爲頭結點時,建立結點指針prev從頭結點開始遍歷到指定index位置
        Node prev = dummyHead;
        for(int i = 0;i<index-1;i++){
            prev = prev.next;
        }
        //在此處插入節點
        prev.next = new Node(e,prev.next);
        //size++
        size++;

    }

    public void addFirst(E e){
//        Node node = new Node(e);
//        node.next = head;
//        head = node;
//      簡化書寫
        head = new Node(e,head);
        size++;
    }
    public void addLast(E e){
        add(size, e);
    }
    /*********查*******/
    //獲取鏈表中的元素個數
    public int getSize(){
        return size;
    }
    //返回鏈表是否爲空
    public boolean isEmpty(){
        return size==0;
    }

    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Get failed.Illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    //獲得鏈表的第一個元素
    public E getFirst() {
        return get(0);
    }

    //獲得鏈表的最後一個元素
    public E getLast() {
        return get(size - 1);
    }

    /******改******/
    public void set(int index,E e){
        if(index < 0 || index>size){
            throw new IllegalArgumentException("Set failed.Illegal index");
        }
        //從頭結點,開始遍歷
        Node cur = dummyHead.next;
        for(int i = 0;i<index;i++){
            cur = cur.next;
        }
        cur.e = e;
    }
    /*****************刪******************/
    public E remove(int index){
        if (index < 0 || index>=size){
            throw new IllegalArgumentException("Removed failed.Illegal index");
        }
        //建立結點引用prev,遍歷到要刪除節點的頭一個節點
        Node prev = dummyHead;
        for(int i = 0;i<index;i++){
            prev = prev.next;
        }
        //建立刪除結點引用delNode,prev.next
        Node delNode = prev.next
        //prev的下一個結點跳過刪除結點,指向下下個結點
        prev.next = prev.next.next;
        //將要刪除的結點的引用賦值爲null
        delNode = null;
        size -- ;
        //返回刪除結點的數據域
        return delNode.e;
    }

    public E removeFirst(){
        remove(0);
    }

    public E removeLast(){
        remove(size - 1);
    }
}

這樣操作就可以不用考慮第一個結點,和中間結點的區別。
因爲在第一個結點之前加了一個dummyNode結點,所有需要操作的結點都是中間結點,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章