简体   繁体   中英

How to create a doubly linked list add method?

It is a doubly linked list of type Double. My other methods won't run with the tester because they say "this.head" or "this.current" is null so I'm assuming something is wrong with the add method since that is used first. It is throwing Any help is appreciated!

{
    private class ListNode
    {
        double data;
        ListNode prev;
        ListNode next;
    
        public ListNode(Double aData, ListNode foreWards, ListNode backWards)
        {
            data = aData;
            next = foreWards;
            prev = backWards;
        }
    }
    private ListNode head;
    private ListNode current;
    private ListNode previous;
    private ListNode tail;
    public DoubleDoubleLL()
    {
        head = current = previous = null;
    }
public void add(Double aData)
    {
        ListNode newNode = new ListNode(aData, null, null);
        if(head == null)
        {
            head = current = tail = newNode;
            tail = current = newNode;
            return;
        }
        else
        {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
            tail.next = null;
            ListNode temp = head;
            temp = temp.next;
            temp.next = newNode;
            temp.prev = newNode;
        }
    }
}

First of all it is normal to have next or prev == null in case of the first and the last records. This how we indicate the start and the end of the list.

As for your add method, yes it is wrong:)

See the example here, I added a main method with some printouts so you can see how it works.

public class DoubleLinkedList {
    private ListNode head;
    private ListNode tail;
    private int size = 0;

    private class ListNode {
        double data;
        ListNode prev;
        ListNode next;

        public ListNode(double data, ListNode prev, ListNode next) {
            this.data = data;
            this.prev = prev;
            this.next = next;

        }
        public String toString(ListNode node) {
            return node == null ? "NIL" : ""+node.hashCode();
        }
        public String toString() {
            return String.format("ListNode [%d] [data: %f, next: %s, prev: %s]", this.hashCode(), this.data, this.toString(this.next),this.toString(this.prev));
        }
    }

    public DoubleLinkedList() {
    }

    public void add(double data) {
        this.size++;
        ListNode node = new ListNode(data, this.tail, null);
        if (this.tail != null) {
            this.tail.next = node;
        }
        this.tail = node;
        if (this.head == null) {
            this.head = node;
        }
    }


    public String toString(ListNode node) {
        return node == null ? "NIL" : ""+node.hashCode();
    }
    public String toString() {
        return String.format("DoubleLinkedList [%d] [size: %d, head: %s, tail: %s] (%s)", this.hashCode(), this.size, toString(this.head), toString(this.tail), this.printList());
    }

    private String printList() {
        StringBuilder sb = new StringBuilder();
        ListNode next = this.head;
        while (next != null) {
            sb.append("\n\t").append(next.toString()).append(",");
            next = next.next;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        DoubleLinkedList list = new DoubleLinkedList();
        System.out.println(list.toString());

        list.add(11);
        System.out.println(list.toString());
        list.add(12);
        System.out.println(list.toString());
        // adding two at once
        list.add(13);
        list.add(14);
        System.out.println(list.toString());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM