Open In App

Circular Linked List Implementation in Java

Last Updated : 15 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Circular Linked List is a variation of the traditional linked list data structure. In the traditional linked list, the last node points to the null and it can indicating the end of the list. However, in the circular linked list, the last node points back to the first node and forms the circle or loop. This circular structure allows for the efficient traversal and manipulation of the elements in the list.

In this article, we will learn to implement a Circular Linked List in Java.

Organization of the Circular Linked List Data Structure

In the Circular Linked List, each node contains two fields and there are one is data and the other is a reference to the next node in the sequence. Unlike in the traditional linked list, the last node's next reference points back to the first node and it can create a circular structure.

Circular_Linked_List

Implementation

The Circular Linked List can implemented using the Node class to represent each element in the list and the LinkedList class to manage the list itself. The LinkedList class can contain the methods for adding, removing, and traversing in the list.

Basic Operations

  1. Insertion: Inserting the node at the beginning or the end of the list can be done on the Circular LinkedList in Java.
  2. Deletion: Deleting the node at the beginning or the end of the list of the Circular LinkedList.
  3. Traversal: Traversal can define the traversing of the entire list of the Circular LinkedList.

Operations Algorthims:

Operations

Algorithms

Complexity

1.Insert at the Begining

  1. Create the new node with given data.
  2. If list is empty, set the both head and tail to new node and make its the next pointer point to itself.
  3. Otherwise, set the new node next pointer to current head and update the head to new node and make the tail next pointer point to new head.

Time Complexity: O(1)
Space Complexity: O(1)

2. Insertion at the End

  1. Create the new node with given data.
  2. If list is empty, set the both head and tail to the new node and make its the next pointer point to itself.
  3. Otherwise, Set the current tail next pointer to new node and update the new node and make the new trail next pointer to head.

Time Complexity: O(1)
Space Complexity: O(1)

3. Deletion at the Beginging

  1. If list is empty, return
  2. If list has only one node then set the both head and tail to the null.
  3. Otherwise, update the head to its next node and make the tails next pointer point to new head.

Time Complexity: O(1)
Space Complexity: O(1)

4. Deletion ar the end

  1. If the list is empty, return
  2. if list has only one node then set the both head and tail to the null.
  3. Otherwise, traverse the list to the find the node before the tail and update the tail to this node and it can make the new tail next pointer point to the head.

Time Complexity: O(1)
Space Complexity: O(1)

Traversal

  1. If list is empty the print "List is Empty" and return.
  2. Start from the head and it can iterate through the list and printing the each node in list once.

Time Complexity: O(n)
Space Complexity: O(1)

Steps to Implementation of Cirular LinkedList

  1. Define the node class: Create the class to represents the each element(node) in the circular Linked List and each node can contains the data and the reference to the next node.
  2. Define the CircularLinkedList class: Create the class to manage the Circular Linked List and this class can contains the methods for the insertion, deletion, traversal and any other necessary operations.
  3. Implements the insertion methods: We can add the methods to the insert the nodes at the beginging and end of the list. Ensure that next pointer of the last node always points back to the head to maintains the circular structure.
  4. Implements the deletion methods: We can add the methods to delete the nodes from the begining and the end of list and it can update the head and tail pointers.
  5. Implements the traversal: We can add the method to the traverse the list and display its the elements of the list.
  6. Finally, We can write the main method to create the instance of CirularLinkedlist class and implements the performing the various operations such as the insertion, deletion and traversal of the Circular Linked List.

Example Program:

Java
// Java Program for Implementing
// Circular Linked List

// Class Node
class Node {
    int data;
    Node next;

    // Constructor to initialize a node with data
    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

// Class CircularLinkedList
class CircularLinkedList {
    private Node head;
    private Node tail;

    // Constructor CircularLinkedList
    public CircularLinkedList() {
        head = null;
        tail = null;
    }

    // Method to insert a new node with the given
      // data into the circular linked list
    public void insert(int data) {
        Node newNode = new Node(data);

        // If the list is empty, make the new node
          // the head and tail
        if (head == null) {
            head = newNode;
            tail = newNode;

            // Point to itself in a circular list
            newNode.next = head;
        } else {
            newNode.next = head;
            tail.next = newNode;
            tail = newNode;
        }
    }

    // Method to delete the node with the given
      // key from the circular linked list
    public void delete(int key) {
        if (head == null) {
            return;
        }

        Node curr = head;
        Node prev = null;
        while (curr.next != head) {
            if (curr.data == key) {
                // If the node to be deleted is the head node
                if (prev == null) {
                    Node last = head;
                    while (last.next != head) {
                        last = last.next;
                    }
                    head = curr.next;
                    last.next = head;
                    return;
                } else {
                    prev.next = curr.next;

                    // Update tail if the last node is deleted
                    if (curr == tail) {
                        tail = prev;
                    }
                    return;
                }
            }
            prev = curr;
            curr = curr.next;
        }

        // Check if the node to be deleted is the last node
        if (curr == head && curr.data == key) {
            prev.next = head;
          
               // Update tail if the last node is deleted
            tail = prev;
        }
    }

    // Method to display the elements
      // of the circular linked list
    public void printList() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
      
          System.out.print("Linked List Elements : ");

        Node itr = head;
        do {
            System.out.print(itr.data + " ");
            itr = itr.next;
        } while (itr != head);
        System.out.println();
    }
}

public class MyClass {
    public static void main(String[] args) {
        // Create a circular linked list
        CircularLinkedList list = new CircularLinkedList();

        // Insert elements into the list
        list.insert(1); 
        list.insert(2);
        list.insert(3);
        list.insert(4);

        // Print the list
          // Output: 1 2 3 4
        list.printList(); 

        // Delete an element from the list
        list.delete(2);

        // Print the updated list
          // Output: 1 3 4
        list.printList(); 
    }
}

Output
Linked List Elements : 1 2 3 4 
Linked List Elements : 1 3 4 

Complexity of the Above Method:

Time Complexity:

  • The overall time complexity for the most operations like insertion at begining, insertion at end, deletion at begining and traversal is O(1) or O(n) where the n is number of the element in list.
  • Insertion and deletion operations at the begining and end of list have the constant time complexity is O(1) as they can involves the fixed number of operations regardless of list size.
  • Deletion at end and traversal operations have the linear time complexity is O(n) as they can require the traversing the entire list which grows with the number of the elements in list.

Space Complexity:

  • Overall space complexity of the circular Linked List implementation is O(n) where n is the number of the elements in list.
  • This space complexity can required to stores the node of linked list.
  • The space complexity of the individual methods is constant O(1) as they can required only fixed amount of memory for the variables and temporary nodes of the list.

Applications of Circular Linked List

  1. It can applies in Circular Queues
  2. It can used to implementation of the Round-Robin scheduling algorithms
  3. It can used to developed the music playlist management

Next Article
Article Tags :
Practice Tags :

Similar Reads