美文网首页
Java算法之双端链表和双向链表

Java算法之双端链表和双向链表

作者: SunnyRivers | 来源:发表于2018-11-07 22:58 被阅读0次

双端链表

链表中保存着对最后一个链结点引用的链表。
可以方便的从尾结点插入数据。

代码

public class Node {
    // 数据域
    public long data;
    //指针域(保存下一个节点的引用)
    public Node next;

    public Node(long value) {
        this.data = value;
    }

    /**
     * 显示方法
     */
    public void display() {
        System.out.print(data + " ");
    }
}
/**
 * 双端链表
 */
public class DoubleEndLinkedList {
    // 头结点
    private Node first;
    //尾结点
    private Node last;

    public DoubleEndLinkedList() {
        first = null;
        last = null;
    }

    /**
     * 插入一个节点,在头结点后进行插入
     *
     * @param value
     */
    public void insertFirst(long value) {
        Node node = new Node(value);
        if (isEmpty()) {
            last = node;
        }
        node.next = first;
        first = node;
    }

    public void insertLast(long value) {
        Node node = new Node(value);
        if (isEmpty()) {
            first = node;
        } else {
            last.next = node;
        }
        last = node;
    }

    /**
     * 删除一个结点
     *
     * @return
     */
    public Node deleteFirst() {
        Node tmp = first;
        if (first.next == null) {
            last = null;
        }
        first = tmp.next;
        return tmp;
    }

    /**
     * 显示方法
     */
    public void display() {
        Node current = first;
        while (current != null) {
            current.display();
            current = current.next;
        }
    }

    /**
     * 查找方法
     *
     * @param value
     * @return
     */
    public Node find(long value) {
        Node current = first;
        while (current.data != value) {
            if (current.next == null) {
                return null;
            }
            current = current.next;
        }
        return current;
    }

    public Node delete(long value) {
        Node current = first;
        Node previous = first;
        while (current.data != value) {
            if (current.next == null) {
                return null;
            }
            previous = current;
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            previous.next = current.next;
        }
        return current;
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return first == null;
    }
}

测试

public class TestDoubleEndLinkedList {
    public static void main(String[] args) {
        DoubleEndLinkedList listFirst = new DoubleEndLinkedList();
        listFirst.insertFirst(0);
        listFirst.insertFirst(1);
        listFirst.insertFirst(2);
        listFirst.display();
        System.out.println();
        listFirst.deleteFirst();
        listFirst.deleteFirst();
        listFirst.display();
        System.out.println();
        System.out.println("-----");
        DoubleEndLinkedList listLast = new DoubleEndLinkedList();
        listLast.insertLast(3);
        listLast.insertLast(4);
        listLast.insertLast(5);
        listLast.display();
        System.out.println();
        listLast.deleteFirst();
        listLast.display();
    }
}

结果

···
2 1 0
0


3 4 5
4 5
···

双向链表

双端链表可以方便的从尾结点插入数据,但是不能从尾部删除数据,所以引入双向链表。
双向链表每个结点除了保存对下一个结点的引用,同时还保存者前一个结点的引用。

代码

public class Node {
    // 数据域
    public long data;
    //指针域(保存下一个节点的引用)
    public Node next;
    //指针域(保存前一个节点的引用)
    public Node previous;

    public Node(long value) {
        this.data = value;
    }

    /**
     * 显示方法
     */
    public void display() {
        System.out.print(data + " ");
    }
}
/**
 * 双向链表
 */
public class DoubleByLinkedList {
    // 头结点
    private Node first;
    //尾结点
    private Node last;

    public DoubleByLinkedList() {
        first = null;
        last = null;
    }

    /**
     * 插入一个节点,在头结点后进行插入
     *
     * @param value
     */
    public void insertFirst(long value) {
        Node node = new Node(value);
        if (isEmpty()) {
            last = node;
        } else {
            first.previous = node;
        }
        node.next = first;
        first = node;
    }

    public void insertLast(long value) {
        Node node = new Node(value);
        if (isEmpty()) {
            first = node;
        } else {
            last.next = node;
            node.previous = last;
        }
        last = node;
    }

    /**
     * 删除一个结点,在头结点后进行删除
     *
     * @return
     */
    public Node deleteFirst() {
        Node tmp = first;
        if (first.next == null) {
            last = null;
        } else {
            first.next.previous = null;
        }
        first = tmp.next;
        return tmp;
    }

    /**
     * 删除一个结点,从尾部进行删除
     *
     * @return
     */
    public Node deleteLast() {
        if (first.next == null) {
            first = null;
        } else {
            last.previous.next = null;
        }
        last = last.previous;
        return last;
    }

    /**
     * 显示方法
     */
    public void display() {
        Node current = first;
        while (current != null) {
            current.display();
            current = current.next;
        }
    }

    /**
     * 查找方法
     *
     * @param value
     * @return
     */
    public Node find(long value) {
        Node current = first;
        while (current.data != value) {
            if (current.next == null) {
                return null;
            }
            current = current.next;
        }
        return current;
    }

    public Node delete(long value) {
        Node current = first;
        while (current.data != value) {
            if (current.next == null) {
                return null;
            }
            current = current.next;
        }
        if (current == first) {
            first = first.next;
        } else {
            current.previous.next = current.next;
        }
        return current;
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return first == null;
    }
}

测试

public class TestDoubleByLinkedList {
    public static void main(String[] args) {
        DoubleByLinkedList dl = new DoubleByLinkedList();
        dl.insertLast(0);
        dl.insertLast(1);
        dl.insertLast(2);
        dl.display();
        System.out.println();
        while (!dl.isEmpty()){
            dl.deleteLast();
            System.out.println();
            dl.display();
        }
    }
}

结果

0 1 2 

0 1 
0 

相关文章

  • Java算法之双端链表和双向链表

    双端链表 链表中保存着对最后一个链结点引用的链表。可以方便的从尾结点插入数据。 代码 测试 结果 ···2 1 0...

  • 数据结构 | 其二 链表

    冰河winner - 数据结构之链表 2.1 单向链表 数据结构(一) 单链表的实现-JAVA 2.2 双端链表 ...

  • JAVA 集合之 LinkedList 底层实现和原理

    JAVA 集合之 LinkedList 底层实现和原理 概述 LinkedList底层是基于双向链表(双向链表的特...

  • Redis底层数据结构之双端链表

    前言 Redis的双端链表是基于双向链表实现的,但传统双向链表存在一些问题,比如获取链表长度需要遍历整个链表,时间...

  • 集合-LinkedList解析

    一、概要 Java中底层数据结构是链表、双端链表,Android中数据结构是双向循环链表 非线程安全数据结构,允许...

  • Java数据结构(四):线性表之双向链表

    java实现简单的双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直...

  • 数据结构和算法总结

    常用数据结构: 数组,栈,队列,链表(单向链表,双端链表,双向链表),哈希表(hash table),树(二叉树,...

  • Java中各队列小结

    typeClass存储结构数据组织算法备注非阻塞LinkedList双端链表双端链表非阻塞PriorityQueu...

  • 链表

    节点类 除双向链表外的节点类 双向链表的节点类 单链表 每个节点只指向下一个节点单链表的操作类 双端链表 每个节点...

  • 数据结构与算法系列-目录

    数据结构和算法目录表 线性结构 1.数组、单链表和双链表 2.Linux内核中双向链表的经典实现 栈 队列 树形结...

网友评论

      本文标题:Java算法之双端链表和双向链表

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