二、自己動手實現------------“鏈表”

參考文章:

https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&mid=2247484086&idx=1&sn=24127ceb5e0fed7f832f82579c4fbc19&chksm=ebd743b7dca0caa1dce912d47251548225aa59b4b48742a963ac52b05e13ec923a738ebcb836#rd    Java實現單向鏈表

https://www.cnblogs.com/whgk/p/6589920.html      數據結構(一) 單鏈表的實現-JAVA

https://github.com/liuyubobobo/Play-with-Algorithms


溫馨提示:

        鏈表的基礎知識以及構成,參考上面的文章,下面只給出鏈表的實現,如果還不是很理解,就自己拿出紙和筆,多寫一寫,畫一畫,網上的視頻還有紙質資料,以及博客公衆號很多,都搜一搜,理解了原理,再來看代碼。代碼參考了很大一部分波波老師的代碼,有問題 call

 

以下是  “鏈表”  的實現代碼:

public class LinkedList<E> {

    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();
        }
    }

    private Node dummyHead;
    private int size;

    public LinkedList(){
        dummyHead = new Node();
        size = 0;
    }

    // 獲取鏈表中的元素個數
    public int getSize(){
        return size;
    }

    // 返回鏈表是否爲空
    public boolean isEmpty(){
        return size == 0;
    }

    // 在鏈表的index(0-based)位置添加新的元素e
    // 在鏈表中不是一個常用的操作,練習用:)
    public void add(int index, E e){

        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed. Illegal index.");

        Node prev = dummyHead;
        for(int i = 0 ; i < index ; i ++)
            prev = prev.next;

        prev.next = new Node(e, prev.next);
        size ++;
    }

    // 在鏈表頭添加新的元素e
    public void addFirst(E e){
        add(0, e);
    }

    // 在鏈表末尾添加新的元素e
    public void addLast(E e){
        add(size, e);
    }

    // 獲得鏈表的第index(0-based)個位置的元素
    // 在鏈表中不是一個常用的操作,練習用:)
    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);
    }

    // 修改鏈表的第index(0-based)個位置的元素爲e
    // 在鏈表中不是一個常用的操作,練習用:)
    public void set(int index, E e){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Update failed. Illegal index.");

        Node cur = dummyHead.next;
        for(int i = 0 ; i < index ; i ++)
            cur = cur.next;
        cur.e = e;
    }

    // 查找鏈表中是否有元素e
    public boolean contains(E e){
        Node cur = dummyHead.next;
        while(cur != null){
            if(cur.e.equals(e))
                return true;
            cur = cur.next;
        }
        return false;
    }

    // 從鏈表中刪除index(0-based)位置的元素, 返回刪除的元素
    // 在鏈表中不是一個常用的操作,練習用:)
    public E remove(int index){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Remove failed. Index is illegal.");

        // E ret = findNode(index).e; // 兩次遍歷

        Node prev = dummyHead;
        for(int i = 0 ; i < index ; i ++)
            prev = prev.next;

        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size --;

        return retNode.e;
    }

    // 從鏈表中刪除第一個元素, 返回刪除的元素
    public E removeFirst(){
        return remove(0);
    }

    // 從鏈表中刪除最後一個元素, 返回刪除的元素
    public E removeLast(){
        return remove(size - 1);
    }

    // 從鏈表中刪除元素e
    public void removeElement(E e){

        Node prev = dummyHead;
        while(prev.next != null){
            if(prev.next.e.equals(e))
                break;
            prev = prev.next;
        }

        if(prev.next != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            size --;
        }
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();

        Node cur = dummyHead.next;
        while(cur != null){
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL");

        return res.toString();
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

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