How can I initialize a Doubly Linked List and then add the first element in java?

Stephen Burns

I'm currently working on creating a doubly linked list, but I'm struggling to do so because the constructor requires the previous element and the next element. However, checking the list just results in two null elements, the head and the tail. The constructor for a node is

    public Node(Node prev, Node next, String link) {
        this.prev = prev;
        this.next = next;
        this.link = link;
    }

The constructor for the empty list that I have is

public DoublyLinkedList() {
    head = tail = null;
}

My code for adding an element is

public void addElement(String link) {
    Node n = new Node(tail.prev, tail, link);
    if (head == null) {
        head = n;
        head.next = n;
    }
    tail.prev = n;
    tail = n;
}

I know that the reason I'm resulting in null is because tail == null when I pass it into the constructor. However, I don't know how to update the value of tail before creating a new Node. I tried constructing the empty list with

public DoublyLinkedList() {
    head = tail = null;
    head.prev = null;
    head.next = tail;
    tail.next = null;
    tail.prev = head;
}

But that isn't showing the elements as being added either.

David Villarreal

Am going to assume that addElement adds an element to the end of the list if that is the case try this instead

Node n = new Node(tail, null, link); // The new tail
if (head == null) {
    head = n;
    tail = n;
}else{
    tail.next = n;
    tail = n;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can someone please explain how can I remove first and last nodes in my doubly linked list?

How can I remove the first and last Node from doubly linked list?

First and Last Element of a Doubly Linked List

The first element of a ( custom ) doubly linked list is repeated

How can I add a node at the beginning of a doubly linked list in-between two dummy nodes?

How do I add spaces between elements in a doubly linked list when concentrating them into a string in Java?

How can I write a heapsort for a doubly linked list? c++

How can I print a doubly-linked list?

How can I get the index of a specific node in a doubly linked list?

How to create a doubly linked list add method?

How do I filter a doubly linked list?

How to properly initialize a big structure with doubly linked list?

How to append a node in a doubly linked list in Java?

How could I make this singly linked list into a doubly linked list?

Add element to ordered doubly linked list using recursive

How do I insert a new node before first node of a doubly-linked list?

How Can I add linked list to vector?

How can i initialize a linked list node with a single init call?

I have problem in the 'removeElement(element)' in the doubly linked list

How can I add a element in Linked List ascending order if the list not empty

Error when trying to add into doubly linked list Java

Counting how many of that "element" appear in a doubly linked list

How to insert an Element in a doubly linked list without repetion?

Convert Binary Tree to doubly linked list. How can I avoid using global variable here?

How can I find if a number in a doubly linked list is between max and min?

How can I add new element with given index into Linked-List?

Java Iterator Doubly Linked list

How add node before doubly linked list c++

How to add a new node at the start of a doubly linked list?