Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

//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

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

1 Answer

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));
    }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...