java中数据结构(二)双链表总结

来源:互联网 发布:syslog 数据库 编辑:程序博客网 时间:2024/06/06 02:21

上面文章介绍了单链表的总结java中数据结构(一)单链表总结,双链表在单链表结构的基础上增加了一个指向前驱节点的元素地址,它的优点根据一个节点很容易就可以得到前后节点的数据元素,而不再向单链表一样每次从头节点开始查找才可以,缺点是相对单链表增加了内存开销。

双链表的数据结构图

下面的代码实现了双链表的增删改查:

双链表中的Node节点

public class Node<E> {//双链表节点    public E data;    public Node<E> prev;//前驱节点    public Node<E> next;//后继节点    public Node(E data, Node<E> prev, Node<E> next) {        this.data = data;        this.prev = prev;        this.next = next;    }    public Node(E data){        this(data,null,null);    }    public Node(){        this(null,null,null);    }}

双链表中的NodeLinkList链表

public class NodeLinkList<E> {//这是一个双链表    private int length;//链表的长度    private Node<E> head;//头部节点中的data是null    public NodeLinkList() {        length = 0;        head = new Node<>();//创建头部节点    }    public int size() {//获取链表的长度(这里需要注意的是要从head.next开始算起)        return length;    }    public boolean isEmpty() {//判断链表是不是空        return head.next == null;    }    public void clearAll() {//清除链表中的所有数据        head.next = null;    }    public boolean add(E data) {//在链表最后添加新节点        Node<E> p = this.head;        while (p.next != null) {// 遍历双链表            p = p.next;        }        Node<E> q = new Node<>(data);// 创建要插入的结点q        p.next = q;        q.prev = p;        this.length++;// 链表长度加1        return true;    }    public boolean add(int index, E data) {//在index索引处添加data数据的节点        if (index < 0 || data == null) {            return false;        }        Node p = new Node(data);//创建新的节点        Node q = head.next;        if (q == null || index == 0) {//如果是空链表或者索引是0            head.next = p;            p.prev = head;        } else {//如果不是空链表            if (index > length - 1) {//需要将新节点加入链表的尾部                add(data);            } else {//需要将新节点加入列表中间位置                int j = 0;                while (q != null && j < index) {                    q = q.next;//循环结束的tempNode是目前链表中索引为index的节点                    j++;                }                p.prev = q.prev;                p.next = q;                q.prev = p;                p.prev.next = q;            }        }        length++;        return true;    }    public boolean delete(int index) {//在指定索引位置删除节点        if (index < 0 || head.next == null || index > length) {            return false;        }        if (length == 1) {//只有一个节点即对尾节点的话就将列表置空            clearAll();        } else {//删除的是中间节点和第一个有效节点(head.next对应的节点)            int j = 0;            Node p = head.next;            while (p != null && j < index) {                p = p.next;//循环结束获取的是当前index节点                j++;            }            p.prev.next = p.next;            p.next.prev = p.prev;        }        length--;        return true;    }    public boolean update(int index, E newData) {        if (index < 0 || newData == null) {            return false;        }        int j = 0;        Node p = head.next;        while (p != null && j < index) {            p = p.next;            j++;        }        if (p != null) {            p.data = newData;        }        return true;    }    public Node select(int index) {        if (index < 0 || index > length) {            return null;        }        int j = 0;        Node p = head.next;        while (p != null && j < index) {            p = p.next;            j++;        }        return p;    }    public void print() {        Node p = head;        while (p != null) {            System.out.println(p.data);            p = p.next;        }    }