Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Circular linked list in Data structure

YASH PAL, 22 May 202028 May 2024

In Data Structure A circular linked list is a list in which there is no None link. and the link of the last node refers to the first node. each node has a successor and all the nodes form a circle.

Circular linked list | Data structures and Algorithms

to perform the operations on a circular linked list we generally and will need to store the reference of the last node of the list. because if we store the reference of the first node then we don’t perform operations such as insertion, deletion, etc in constant time. and we need to traverse the whole linked list.

so to perform all the operations in constant time we always store the reference of the last node.

Circular linked list | Data structures and Algorithms

we also use the circular linked list to implement the other data structure like the implementation of a queue. because the insertions and deletions in the queue take constant time using a circular linked list.

Python program to Implement Circular Linked list

class Node(object):

    def __init__(self, value):
        self.info = value
        self.link = None

class CircularLinkedList(object):

    def __init__(self):
        self.last = None
    def display_list(self):

        if self.last == None:
            print("List is empty\n")
            return        p = self.last.link

        while True:
            print(p.info, " ", end='')
            p = p.link
            if p == self.last.link:
                break        print()

    def insert_in_beginning(self, data):
        temp = Node(data)
        temp.link = self.last.link
        self.last.link = temp

    def insert_in_empty_list(self, data):
        temp = Node(data)
        self.last = temp
        self.last.link = self.last

    def insert_at_end(self,data):
        temp = Node(data)
        temp.link = self.last.link
        self.last.link = temp
        self.last = temp

    def create_list(self):
        n = int(input("Enter the number of nodes : "))
        if n == 0:
            return        data = int(input("Enter the element to be inserted : "))
        self.insert_in_empty_list(data)

        for i in range(n-1):
            data = int(input("Enter the element to be inserted : "))
            self.insert_at_end(data)

    def insert_after(self, data, x):
        p = self.last.link

        while True:
            if p.info == x:
                break            p = p.link
            if p == self.last.link:
                break
        if p == self.last.link and p.info != x:
            print(x, " not present in the list")
        else:
            temp = Node(data)
            temp.link = p.link
            p.link = temp
            if p == self.last:
                self.last = temp

    def delete_first_node(self):
        if self.last is None:
            return        if self.last.link == self.last:
            self.last = None            return        self.last.link = self.last.link.link

    def delete_last_node(self):
        if self.last is None:
            return        if self.last.link == self.last:
            self.last = None            return
        p = self.last.link
        while p.link != self.last:
            p = p.link
        p.link = self.last.link
        self.last = p

    def delete_node(self,x):

        if self.last is None:
            return        if self.last.link == self.last and self.last.info == x:
            self.last = None            return        if self.last.link.info == x:
            self.last.link == self.last.link.link
            return
        p = self.last.link
        while p.link != self.last.link:
            if p.link.info == x:
                break            p = p.link

        if p.link == self.last.link:
            print(x, " not found in the list")
        else:
            p.link = p.link.link
            if self.last.info == x:
                self.last = p

#################################################

list = CircularLinkedList()


list.create_list()

while True:
    print("1. Display list")
    print("2. Insert in empty list")
    print("3. Insert a node in the beginning of the list")
    print("4. Insert a node at the end of the list")
    print("5. Insert a node after a specified node")
    print("6. Delete first node")
    print("7. Delete last node")
    print("8. Delete any node")
    print("9. Quite")

    option = int(input("Enter your choice : "))

    if option == 1:
        list.display_list()
    elif option == 2:
        data = int(input("Enter the element to be inserted : "))
        list.insert_in_empty_list(data)
    elif option == 3:
        data = int(input("Enter the element to be inserted : "))
        list.insert_in_beginning(data)
    elif option == 4:
        data = int(input("Enter the element to be inserted : "))
        list.insert_at_end(data)
    elif option == 5:
        data = int(input("Enter the element to be inserted : "))
        x = int(input("Enter the element after which to insert : "))
        list.insert_after(data,x)
    elif option == 6:
        list.delete_first_node()
    elif option == 7:
        list.delete_last_node()
    elif option == 8:
        data = int(input("Enter the element to be deleted : "))
        list.delete_node(data)
    elif option == 9:
        break    else:
        print("Wrong option")
    print()
Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes