Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

Double Linked List

//I have a single linked list that looks like this

node head = new Node ();
head.info = 3;
head.next = new Node ();
head.next.info = 6 ();
head.next.next = new Node ();
head.next.next.info = 9;
head.next.next.next = null;

//How would I write a double linked list?

class Double Node
{
    info;
    doubleNode prev;
    doubleNode next;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the part for creating a double linked list and adding nodes to it. You can update it by adding remove and insert functions as you want.

class Node{
    Node prev;
    Node next;
    int value;

    public Node(int value) {
        this.value = value;
    }
}

class DoubleLinkedList{
    Node head;
    Node tail;

    public DoubleLinkedList(Node head) {
        this.head = head;
        this.tail = head;
        head.next = null;
        head.prev = null;
    }

    public void addNode(Node node){
        tail.next = node;
        node.prev = tail;
        tail = node;
    }
}

public class Main {
    public static void main(String args[]){
        Node head= new Node(3);
        DoubleLinkedList list = new DoubleLinkedList(head);
        list.addNode(new Node(5));
        list.addNode(new Node(6));
        list.addNode(new Node(7));
        list.addNode(new Node(8));
    }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...