HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Implementation of a doubly linked list in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javadoublyimplementationlistlinked

Problem

I'd like to improve this code.

DoublyLinkList.java

public class DoublyLinkList {

    private static class Node {
        private T data;
        private Node next;
        private Node prev;

        public Node(T data) {
            this.data = data;
        }

        public void displayNode() {
            System.out.print(data + " ");
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }

    public Node first = null;
    public Node last = null;

    public void addFirst(T data) {
        Node newNode = new Node(data);

        if (isEmpty()) {
            newNode.next = null;
            newNode.prev = null;
            first = newNode;
            last = newNode;

        } else {
            first.prev = newNode;
            newNode.next = first;
            newNode.prev = null;
            first = newNode;
        }
    }

    public boolean isEmpty() {
        return (first == null);
    }

    public void displayList() {
        Node current = first;
        while (current != null) {
            current.displayNode();
            current = current.next;
        }
        System.out.println();
    }

    public void removeFirst() {
        if (!isEmpty()) {
            Node temp = first;

            if (first.next == null) {
                first = null;
                last = null;
            } else {
                first = first.next;
                first.prev = null;
            }
            System.out.println(temp.toString() + " is popped from the list");
        }
    }

    public void removeLast() {
        Node temp = last;

        if (!isEmpty()) {

            if (first.next == null) {
                first = null;
                last = null;
            } else {
                last = last.prev;
                last.next = null;
            }
        }
        System.out.println(temp.toString() + " is popped from the list");
    }
}


DoublyLinkListDemo.java

```
publ

Solution

removeLast() assigns last to temp, which may be null. If the list is empty, you will call toString() on null.

Otherwise, it is a pretty well made list.

I would change the names of your functions/class.

  • DoublyLinkList becomes DoublyLinkedList



  • addFirst becomes pushFront



  • removeFirst becomes popFront



  • removeLast becomes popBack



  • displayList becomes display ( because we know a list is calling it )



  • displayNode becomes display



As general advise about naming, the words you are using to describe what is happening are good hints at what names the functions/class should be. For example, you use Doubly Linked List in the title of this post, and you use the word "popped" as output to the remove functions.

Context

StackExchange Code Review Q#63171, answer score: 4

Revisions (0)

No revisions yet.