美文网首页数据结构与算法
线性表的链式存储--双向链表

线性表的链式存储--双向链表

作者: 暮想sun | 来源:发表于2019-12-17 11:06 被阅读0次

双向链表:

单链表的每个结点中,再设置一个指向其前驱结点的指针域。
存在两个指针域,一个指向直接后继,另一个人指向直接前驱。


结点数据结构:

    private static class LinkedNode<E> {

        //前驱结点
        private LinkedNode<E> pre;

        //后继结点
        private LinkedNode<E> next;

        //数据
        private E item;

        public LinkedNode(LinkedNode<E> pre, LinkedNode<E> next, E item) {
            this.pre = pre;
            this.next = next;
            this.item = item;
        }
    }

参数定义:

    //第一结点
    LinkedNode<E> first;

    //最后一个结点
    LinkedNode<E> last;

    //链表数据量
    private int size;

元素个数:

    public int size() {
        return size;
    }

判断是否为空:

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

获取元素下标:

    public int indexOf(Object o) {
        int index = 0;

        //循环判断
        if (o == null) {
            for (LinkedNode<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    return index;
                }
                index++;
            }
        } else {
            for (LinkedNode<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    return index;
                }
                index++;
            }

        }
        return -1;
    }

判断是否包含某个元素:

    public Boolean contains(Object o) {
        return indexOf(o) != -1;
    }

获取指定下标数据:

    public E get(int index) {

        //判断下标是否越界
        if (index < 0 || index > size) {
            throw new RuntimeException("下标不正确");
        }

        //减少循环次数,从头向后,或者从后向前判断
        if (index < (size >> 1)) {
            LinkedNode<E> x = first;
            for (int i = 0; i < index; i++) {
                x = x.next;
            }

            return x.item;

        } else {

            LinkedNode<E> x = last;
            for (int i = size - 1; i > index; i--) {
                x = x.pre;
            }

            return x.item;

        }

    }

插入--头插法:

    public void addFirst(E e) {

        LinkedNode<E> linkedNode = first;
        LinkedNode<E> newNode = new LinkedNode<>(null, first, e);

        first = newNode;
        if (linkedNode == null) {
            last = newNode;
        } else {
            linkedNode.pre = newNode;
        }

        size++;

    }

插入--尾插法:

 private void addLast(E e) {
        LinkedNode<E> linkedNode = last;
        LinkedNode<E> newNode = new LinkedNode<>(last, null, e);
        last = newNode;
        if (linkedNode == null) {
            first = newNode;
        } else {
            linkedNode.next = newNode;
        }

        size++;
    }

删除:

    public Boolean remove(Object o) {

        if (o == null) {
            for (LinkedNode<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (LinkedNode<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }

        }

        return false;
    }

    private void unlink(LinkedNode<E> linkedNode) {

        //前驱是空,说明为第一个元素
        LinkedNode<E> pre = linkedNode.pre;
        LinkedNode<E> next = linkedNode.next;
        if (pre == null) {
            first = next;
        } else {
            pre.next = next;
            linkedNode.pre = null;
        }

        //猴急为空,说明为最后一个结点
        if (next == null) {
            last = pre;
        } else {
            next.pre = pre;
            linkedNode.next = null;

        }

        linkedNode.item = null;
        size--;
    }

清空:

    public void clear() {

        for (LinkedNode<E> x = first; x != null; ) {

            LinkedNode<E> next = x.next;
            x.item = null;
            x.pre = null;
            x.next = null;

            x = next;
        }
        first = last = null;
        size = 0;

    }

相关文章

  • 线性表(三)——双向链表的表示和实现

    在上篇文章中我们分析讨论了线性表的链式存储结构。链式存储结构表示的线性表主要分为单链表、单循环链表和双向循环链表三...

  • 数据结构-单向链表

    一、线性表 线性表可以分为: 顺序表(数组) 链表(单向链表、双向链表、循环链表) 二、链表 链表是一种链式存储的...

  • 数据结构——线性表

    线性表分为顺序存储结构和链式存储结构(单链表,静态链表,循环链表,双向链表)。 单链表(**一种动态结构,所占空间...

  • 线性链表

    线性链表 线性表的顺序存储结构:顺序表线性表的链式存储结构:线性链表 线性表的链式存储所占存储空间大于顺序存储。 ...

  • 数据结构与算法之数组与链表

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 数据结构与算法之栈与队列

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 学习数据结构第一弹 线性表(5)

    循环链表与双向链表 循环链表: 循环链表也是一种线性表的链式存储结构,其实他和单链表很像,其特点是它是一个环,也就...

  • 线性表的链式存储--单链表

    Java之线性表的链式存储——单链表 我们都知道,线性表的存储结构分为两种,顺序存储结构和链式存储结构,线性表的分...

  • 线性表--链表(Linked)

    线性表的链式存储结构--链表(Linked) 链表(Linked)是用一组任意的存储单元存储线性表的数据元素,他们...

  • LeetCode题集整理- 链表篇

    1、链表基础 链式存储方式线性表线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素,这组存储单元...

网友评论

    本文标题:线性表的链式存储--双向链表

    本文链接:https://www.haomeiwen.com/subject/nncsnctx.html