• About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment
Cuitutorial
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment

Data Structure

Home » Blog » Circular Link list Data Structure

Circular Link list Data Structure

  • Posted by saqib
  • Categories Data Structure
  • Date November 11, 2022
  • Comments 0 comment

Circular Link List complete code and application

Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.

Basic Operations

Following are the important operations supported by a circular list.

  • insert
  • delete
  • display

For the working detail diagram of these operation click here

Single Circular Link list

Single circular link list
circular link list

Doubly Ring

Doubly Ring data structure can be regarded as Circular Doubly Link List. In Circular Doubly Link List, instead of making last node’s next pointer  to NULL, We assign or point it to head node. Similarly, first (head) node’s previous pointer  is pointed to last node in list . so that it sounds as Doubly Ring data structure.

circular doubly link list
doubly circular link list

Application of Circular Linked List

  • Personal Computers, where multiple applications are running. All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the linked list until all the applications are completed.
  • Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on moving forward as a player’s chance ends.
  • Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two pointers, FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer is required.

C++ code for Single circular link list

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

struct node
{
int info;
struct node *next;
}*p;

class circular_llist
{
public:
void create_node(int value);
void add_after(int value, int position);
void delete_element(int value);
void display_list();
circular_llist()
{
p = NULL;
}
};

int main()
{
int choice, element, position;
circular_llist cl;
while (1)
{
cout << “1.Create Node” << endl;
cout << “2.Add after” << endl;
cout << “3.Delete” << endl;
cout << “4.Display” << endl;
cout << “5.suit” << endl;
cout << “Enter your choice : “;
cin >> choice;
switch (choice)
{
case 1:
cout << “Enter the element: “;
cin >> element;
cl.create_node(element);
cout << endl;
break;
case 2:
cout << “Enter the element: “;
cin >> element;
cout << “Insert element after position: “;
cin >> position;
cl.add_after(element, position);
cout << endl;
break;
case 3:
if (p == NULL)
{
cout << “List is empty, nothing to delete” << endl;
break;
}
cout << “Enter the element for deletion: “;
cin >> element;
cl.delete_element(element);
cout << endl;
break;
case 4:
cl.display_list();
break;
case 5:
exit(1);
break;

default:
cout << “Wrong choice” << endl;
}
}
return 0;
}
//Insert Operation
void circular_llist::create_node(int value)
{
node *t;
t = new node;
t->info = value;
if (p == NULL)
{
p = t;
t->next = p;
}
else
{
t->next = p->next;
p->next = t;
p = t;
}
}

void circular_llist::add_after(int value, int pos)
{
if (p == NULL)
{
cout << “First Create the list.” << endl;
return;
}
node *t, *s;
s = p->next;
for (int i = 0; i < pos – 1; i++)
{
s = s->next;
if (s == p->next)
{
cout << “There are less than “;
cout << pos << ” in the list” << endl;
return;
}
}
t = new node;
t->next = s->next;
t->info = value;
s->next = t;

if (s == p)
{
p = t;
}
}

//delete operation
void circular_llist::delete_element(int value)
{
node *t, *s;
s = p->next;

if (p->next == p && p->info == value)
{
t = p;
p = NULL;
free(t);
return;
}
if (s->info == value)
{
t = s;
p->next = s->next;
free(t);
return;
}
while (s->next != p)
{

if (s->next->info == value)
{
t = s->next;
s->next = t->next;
free(t);
cout << “Element ” << value;
cout << ” deleted from the list” << endl;
return;
}
s = s->next;
}

if (s->next->info == value)
{
t = s->next;
s->next = p->next;
free(t);
p = s;
return;
}
cout << “Element ” << value << ” not found in the list” << endl;
}

//Display List Operation
void circular_llist::display_list()
{
node *s;
if (p == NULL)
{
cout << “List is empty” << endl;
return;
}
s = p->next;
cout << “Circular Link List: = “;
while (s != p)
{
cout << s->info << “->”;
s = s->next;
}
cout << s->info << endl;
cout << endl;
}

 

Related links 

Single link list                 Stack              AVL Trees             Binary search          Counting Sort

Doubly link list               Queue              Graphs                  Bubble Sort               Radix sort

Circular link list              Binary search tree       Hashing         Insertion Sort         Bucket Sort

Josephus Problem          Tree Traversal              Heaps                Quick Sort              Merge Sort

 

At Cui tutorial, courses, past papers and final year projects

#tutorial #cui #pastpaper #courses

 

  • Share:
author avatar
saqib

Previous post

Single link list Data structure
November 11, 2022

Next post

Doubly Link List
November 11, 2022

You may also like

Counting Sort
6 December, 2022

Counting sort is a type of Linear Time Sort Counting Sort was invented by H.H.Seward in 1954 All the sorting algorithms introduced so far share an                              …

Insertion Sort
6 December, 2022

Insertion Sort using loop and recursive function complete code On the ith pass we “insert” the ith element A[i] into its rightful place among A[1],A[2],…A[i-1] which were placed in sorted order. After this insertion A[1],A[2],…A[i] are in sorted order. Time …

Bubble Sort using C++
6 December, 2022

Bubble sort using loop and bubble sort in link list One of the simplest sorting methods. The basic idea is to move required value (smallest or highest ) to the top. Compare adjacent values (all elements) Swap values if required …

Leave A Reply Cancel reply

You must be logged in to post a comment.

admin@cuitutorial.com
Facebook-f Twitter Youtube Linkedin Instagram Stack-overflow Pinterest Github Quora Whatsapp
Courses
  • All Courses
  • Past Paper
  • Final year projects
  • Interview Questions
  • Contact
Important Pages
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
Links
  • University Events
  • Team
Education & learning platform for All Computer science subjects
Final year projects
Past Paper
Interview questions
Programming, C/C++, Asp.net/MVC. Android, MySql, Jquery, Ajax, javascript, Php, Html5, Bootstrap4.
NTS, GAT, PPSC, FPSC

Copyright © 2021 | Cuitutorial