Languages
[Edit]
EN

Java - insert node as last one to double linked list (custom implementation)

1 points
Created by:
Root-ssh
175450

1. Overview

In this post we will see how to implement algorithm to correctly add node as last one to double linked list. Below we have 2 implementations, first one with node based on Integer, second one with generic Node and usage example based on String.

  • To create new Node we call constructor with next node set as null and previous node set as newly created one.
  • Then we have short logic which will handle links (previous and next) between our nodes.

We can use the logic to reverse the process of adding the node as last one and implement logic for appending the node as the first one. In previous article we covered the algorithm of inserting node at the beginning of linked list (adding node as first one).

2. Insert node - code example

Below we have java implementation with Node based on Integer as item of double linked list with logic to insert node as last one. This implementation is without generics to have this example as simple as possible. Generic implementation of this linked list is in next part of this post.

package com.dirask;

public class DoubleLinkedList {
    Node first;
    Node last;
    int size;

    public void addLast(Integer item) {
        Node tmpLast = last;
        Node newNode = new Node(tmpLast, item, null);
        last = newNode; // assign new node as last
        if (tmpLast == null) {
            first = newNode;
        } else {
            // take care that each node has next link
            tmpLast.next = newNode;
        }
        size++;
    }

    @Override
    public String toString() {
        if (first != null) {
            return first.printForward();
        }
        return "List is empty";
    }

    private static class Node {
        Node prev;
        Integer item;
        Node next;

        public Node(Node prev, Integer item, Node next) {
            this.prev = prev;
            this.item = item;
            this.next = next;
        }

        public String printForward() {
            if (next != null) {
                return item + " -> " + next.printForward();
            }
            return String.valueOf(item);
        }
    }

    public static void main(String[] args) {

        DoubleLinkedList list = new DoubleLinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);

        // list: 1 -> 2 -> 3
        System.out.println("list: " + list.toString());
        // size: 3
        System.out.println("size: " + list.size);
    }
}

Output:

list: 1 -> 2 -> 3
size: 3
Java - insert node as last one to double linked list - screenshot from intellij idea in debugging session
Debug session - Insert node to linked list

3. Insert node - generic implementation

Below we have java generic implementation of double linked list with add last method which will add node to our list.

package com.dirask;

public class DoubleLinkedList<E> {
    Node<E> first;
    Node<E> last;
    int size;

    public void addLast(E item) {
        Node<E> tmpLast = last;
        Node<E> newNode = new Node<>(tmpLast, item, null);
        last = newNode; // assign new node as last
        if (tmpLast == null) {
            first = newNode;
        } else {
            // take care that each node has next link
            tmpLast.next = newNode;
        }
        size++;
    }

    @Override
    public String toString() {
        if (first != null) {
            return first.printForward();
        }
        return "List is empty";
    }

    private static class Node<E> {
        Node<E> prev;
        E item;
        Node<E> next;

        public Node(Node<E> prev, E item, Node<E> next) {
            this.prev = prev;
            this.item = item;
            this.next = next;
        }

        public String printForward() {
            if (next != null) {
                return item + " -> " + next.printForward();
            }
            return String.valueOf(item);
        }
    }

    public static void main(String[] args) {

        DoubleLinkedList<String> list = new DoubleLinkedList<>();
        list.addLast("1");
        list.addLast("2");
        list.addLast("3");

        // list: 1 -> 2 -> 3
        System.out.println("list: " + list.toString());
        // size: 3
        System.out.println("size: " + list.size);
    }
}

Output:

list: 1 -> 2 -> 3
size: 3
Java - insert node as last one to double linked list - screenshot from intellij idea in debugging session - generic code
Debug session - Insert node to linked list - generic implementation

References

References (Harvard CS classes - Data Structures)

Alternative titles

  1. Add node as last one to linked list in java
  2. Java - append node as last one to the linked list
  3. How do I add node to the double linked list as the last one in java?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join