Linked List Implementation in Java

1. Introduction

A linked list is a data structure made up of nodes that together represent a sequence. Each node contains a value and a reference (or link) to the next node in the sequence. 

Linked lists are fundamental data structures that are particularly useful when dynamic memory allocation is required or when insertions and deletions are more frequent than random access to elements.

2. Implementation Steps

1. Define a Node class to represent individual elements of the linked list.

2. Define a LinkedList class which will:

a. Have a head reference pointing to the start of the list.

b. Provide methods like add(), delete(), display(), and isEmpty().

3. Implement the methods to manage the linked list.

4. Create an instance of the linked list class and perform operations to demonstrate its functionality.

3. Implementation in Java

class Node<T> {
    T data;
    Node<T> next;
    Node(T data) {
        this.data = data;
        this.next = null;
    }
}
class LinkedList<T> {
    private Node<T> head;
    public LinkedList() {
        head = null;
    }
    // Check if the list is empty
    public boolean isEmpty() {
        return head == null;
    }
    // Add element to the end of the list
    public void add(T data) {
        Node<T> newNode = new Node<>(data);
        if (isEmpty()) {
            head = newNode;
            return;
        }
        Node<T> current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }
    // Delete the first occurrence of the given element
    public boolean delete(T data) {
        if (isEmpty()) return false;
        if (head.data.equals(data)) {
            head = head.next;
            return true;
        }
        Node<T> current = head;
        while (current.next != null && !current.next.data.equals(data)) {
            current = current.next;
        }
        if (current.next == null) return false;
        current.next = current.next.next;
        return true;
    }
    // Display the list
    public void display() {
        Node<T> current = head;
        while (current != null) {
            System.out.print(current.data + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(10);
        list.add(20);
        list.add(30);
        list.display();  // Outputs: 10 -> 20 -> 30 -> null
        list.delete(20);
        list.display();  // Outputs: 10 -> 30 -> null
    }
}

Output:

10 -> 20 -> 30 -> null
10 -> 30 -> null

Explanation:

1. A generic Node class is defined to represent each element of the linked list. It has two members: data which stores the value and next which points to the next Node in the list.

2. The LinkedList class maintains a reference, head, pointing to the start of the list.

3. The isEmpty() method checks whether the list is empty by verifying if the head is null.

4. The add() method appends a new element to the end of the list. If the list is empty, it sets the head to the new node; otherwise, it traverses to the end and adds the new node.

5. The delete() method removes the first occurrence of the given element. It returns true if the deletion was successful and false otherwise.

6. The display() method prints out the linked list, showcasing its structure.

7. In the main() method, we instantiate a LinkedList of Integers, add some values, display the list, perform a deletion, and then display the list again to demonstrate its functionality.


Comments