學習數據結構Day4

鏈表

 

之前看過了動態數組,棧和隊列,雖然我們把第一個叫做動態數組,但是,他們的底層實質上還是靜態數組。靠

 

resize來實現動態數組。而鏈表是真正的數據結構

 

  • 鏈表需要一個節點。
  • 數據存儲在鏈表中

 

相當於是一串火車,將數據放在車廂中,兩個車廂之間還需要一個個節點來相互串聯。

 

優點:實現了真正的動態。

 

缺點:無法進行隨機訪問

 

public class LinkedList<E> {

    private class Node {

        public E e;
        public Node next;

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

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

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

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

    private Node head;
    private int size;

    public LinkedList(Node head, int size) {
        head = null;
        this.size = 0;
    }

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

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

    //鏈表添加新的元素
    public void addFirst(E e) {
//        Node newNode = new Node((E) node);
//        newNode.next = head;
//        head = newNode;

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