Java数据结构之链表的基本操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yhflyl/article/details/86539600

链表 Linked List

数据存储在“节点”中

在这里插入图片描述

优点

真正的动态,不需要处理固定容量的问题

缺点

丧失了随机访问的能力

数组和链表的对比

数组最好用于索引有语意。

最大优点:支持快速查询

链表不适合用于索引有语意的情况

最大优点:动态

代码实现

public class LinkedList<E> {

    private Node head;
    private int size;

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

    /**
     * 初始化
     */
    public LinkedList() {
        head = null;
        size = 0;
    }
    
	/**
     * 获取长度
     */
    public int getSize() {
        return size;
    }

    /**
     * 判空
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在链表头部添加数据
     * @param e
     */
    public void addFirst(E e) {
        // 方法一
//        Node node = new Node(e);
//        node.next = head;
//        head = node;
        // 方法二: 简化方法
        head = new Node(e, head);
        size++;
    }

    /**
     * 添加
     * 不是一个常用的操作
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if(index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed, Illegal index.");
        }else if(index == 0) {
            addFirst(e);
        }else{
            Node prev = head;
            for(int i = 0; i < index - 1; i++) prev = prev.next;
//            Node node = new Node(e);
//            node.next = prev.next;
//            prev.next = node;
            prev.next = new Node(e, prev.next);
            size++;
        }
    }

    /**
     * 在链表末尾添加元素
     * @param e
     */
    public void addList(E e) {
        add(size, e);
    }
}

引入虚拟头节点

在这里插入图片描述

public class LinkedList<E> {

    private Node dummyHead;
    private int size;

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

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

    public int getSize() {
        return size;
    }

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

    /**
     * 添加
     * 不是一个常用的操作
     * @param index
     * @param 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++;
    }

    /**
     * 在链表头部添加数据
     * @param e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 在链表末尾添加元素
     * @param e
     */
    public void addList(E e) {
        add(size, e);
    }
}

完整代码

public class LinkedList<E> {

    private Node dummyHead;
    private int size;

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

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

    public int getSize() {
        return size;
    }

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


    /**
     * 添加
     * 不是一个常用的操作
     * @param index
     * @param 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++;
    }

    /**
     * 在链表头部添加数据
     * @param e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 在链表末尾添加元素
     * @param e
     */
    public void addList(E e) {
        add(size, e);
    }

    /**
     * 获取索引对应的值
     * @param index
     * @return
     */
    public E get(int index) {
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        Node cur = dummyHead.next;
        for(int i = 0; i < index; i++) cur = cur.next;
        return cur.e;
    }

    /**
     * 获取第一个元素
     * @return
     */
    public E getFirst() {
        return get(0);
    }

    /**
     * 获取最后一个元素
     * @return
     */
    public E getLast() {
        return get(size-1);
    }

    /**
     * 修改第index位置的元素
     * @param index
     * @param e
     */
    public void set(int index, E e) {
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        Node cur = dummyHead.next;
        for(int i = 0; i < index; i++) cur = cur.next;
        cur.e = e;
    }

    /**
     * 遍历链表中的元素
     * @param e
     * @return
     */
    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while(cur != null) {
            if(cur.e.equals(e))
                return true;
            cur = cur.next;
        }
        return false;
    }


    /**
     * 删除指定位置的元素
     * @param index
     * @return
     */
    public E remove(int index) {
        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;
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }


    /**
     * 遍历链表
     * @return
     */
    @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();
    }

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yhflyl/article/details/86539600