(本文参考此博客

一.双向链表的结构:

双向链表.png

二.双向链表的优点:

1.插入节点和删除节点简单。
2.可双向遍历。

三.双向链表的java实现:

class DoubleLinkedList{
    DoubleLinkedList pre;
    DoubleLinkedList next;
    int val;
    static DoubleLinkedList head;
    static DoubleLinkedList tail;
    public DoubleLinkedList(DoubleLinkedList pre, DoubleLinkedList next, int val) {
        this.pre = pre;
        this.next = next;
        this.val = val;
    }

    public DoubleLinkedList getPre() {
        return pre;
    }

    public void setPre(DoubleLinkedList pre) {
        this.pre = pre;
    }

    public DoubleLinkedList getNext() {
        return next;
    }

    public void setNext(DoubleLinkedList next) {
        this.next = next;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }
    //获取链表长度
    public static int getLength(){
        int length=0;
        for(DoubleLinkedList d=head;d!=null;d=d.next){
            length++;
        }
        return length;
    }
    //插入节点
    public static void add(int val, int index) {
        DoubleLinkedList node=new DoubleLinkedList(null,null,val);
        if(index==0){
            node.next=head;
            node.pre=null;
            head=node;
        }else if(index==getLength()){
            tail.next=node;
            node.pre=tail;
            node.next=null;
        }else {
            int tmp=0;
            for(DoubleLinkedList d=head;d!=null;d=d.next){
                tmp++;
                if(tmp==index){
                    //此处顺序有讲究
                    node.next=d.next;
                    d.next.pre=node;
                    d.next=node;
                    node.pre=d;
                    break;
                }
            }
        }
    }
    //删除节点
    public static void delete(int index) {
        if(index==0){
            head=head.next;
        }else if(index==getLength()-1){
            tail=tail.pre;
            tail.next=null;
        }else if(index>=getLength()){
            System.err.println("index超出链表长度");
            System.exit(0);
        }else {
            int tmp=0;
            for(DoubleLinkedList d=head;d!=null;d=d.next){
                tmp++;
                if(tmp==index){
                    d.next=d.next.next;
                    d.next.pre=d;
                    break;
                }
            }
        }
    }
    public static void main(String[] args) {
        DoubleLinkedList node1 = new DoubleLinkedList(null, null,1);
        DoubleLinkedList node2 = new DoubleLinkedList(node1, null,2);
        DoubleLinkedList node3 = new DoubleLinkedList(node2, null,3);
        DoubleLinkedList node4 = new DoubleLinkedList(node3, null,4);
        node3.setNext(node4);
        node2.setNext(node3);
        node1.setNext(node2);
        head = node1;
        tail = node4;
        System.out.print("当前链表:");
        for (DoubleLinkedList n = head; n != null; n = n.next) {
            System.out.print(n.val + " ");
        }
        System.out.println();
        System.out.println("链表长度:" + getLength());
        add(1, 4);
        System.out.print("插入后链表:");
        for (DoubleLinkedList n = head; n != null; n = n.next) {
            System.out.print(n.val + " ");
        }
        System.out.println();
        delete(0);
        System.out.print("删除后链表:");
        for (DoubleLinkedList n = head; n != null; n = n.next) {
            System.out.print(n.val + " ");
        }
    }
}

吃不完的土豆番茄
59 声望10 粉丝