Skip to content

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。通俗的说:就是由一个个节点组成,这些节点逻辑上连续,物理上不连续

它不需要向数组那样提前占用内存空间才能存储,可以随时插入元素没有扩容操作。

Node节点介绍

Node类中e表示的就是元素本身,next指向下一个Node的内存地址,因此我们只需要知道头节点就能一步步找到所有元素。

java
public class LinkedList<E> {

    private class Node {
        private E e;
        private 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();
        }
    }
}

LinkedList基础框架搭建

java
public class LinkedList<E> {

    private class Node {
        private E e;
        private 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 dummyHead; // 头节点
    int size; // 元素数量

    public LinkedList() {
        this.dummyHead = new Node(null, null);
        this.size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
}

添加元素

java
public class LinkedList<E> {
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("您输入了一个非法索引");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    /**
     * 链首添加元素
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 链尾添加元素
     */
    public void addLast(E e) {
        add(size, e);
    }
}

获取元素

java
public class LinkedList<E> {
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("您输入了一个非法索引");
        }

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

设置元素

java
public void set(int index, E e) {
    if (index < 0 || index >= size) {
        throw new IllegalArgumentException("您输入了一个非法索引");
    }

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

是否包含某元素

java
public boolean contains(E e) {
    Node cur = dummyHead.next;
    while (cur != null) {
        if (cur.e.equals(e)) {
            return true;
        }
        cur = cur.next;
    }
    return false;
}

删除元素

java
public class LinkedList<E> {    
    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("您输入了一个非法索引");
        }

        Node prev = dummyHead.next;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;// 交给GC回收
        size--;
        return retNode.e;
    }

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

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

重写toString方法

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