A Circular Linked List in C is a collection of elements connected together with the help of pointers. Such that each node contains a data value and addresses to the next node in the link. Where the last link of the circular Linked list has the address of the first node.
Why circular linked list?
As we know that singly linked list and doubly linked list are already present in there in data structure and algorithm then what is the need of circular linked list?
When we want to access the things in a loop repeatedly. Circular Linked List is ideal so that, whenever we reach the last node we can restart operations again by directly reaching the first node from the last node itself.
Structure of Circular linked list
Circular linked can be of both singly and doubly types. So they can be defined in a program by using the following set of code.
#include
#include
struct Node
{
int data;
struct Node *next;
};
void insertStart (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
// if its the first node being entered
if (*head == NULL)
{
*head = newNode;
(*head)->next = *head;
return;
}
// if LL already as >=1 node
struct Node *curr = *head;
// traverse till last node in LL
while (curr->next != *head)
{
curr = curr->next;
}
// assign LL's last node's next as this new node
curr->next = newNode;
// assign newNode's next as current head
newNode->next = *head;
// change head to this new node
*head = newNode;
}
void display (struct Node *head)
{
// if there are no node in LL
if (head == NULL)
return;
struct Node *temp = head;
//need to take care of circular structure of LL
do
{
printf ("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
printf ("\n");
}
int main ()
{
struct Node *head = NULL;
printf ("Linked List: ");
insertStart (&head, 6);
insertStart (&head, 5);
insertStart (&head, 4);
insertStart (&head, 3);
insertStart (&head, 2);
insertStart (&head, 1);
display (head);
return 0;
}
Output
Linked List: 1 2 3 4 5 6
Code For Insertion At The End In Circular Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertLast (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
// if its the first node being entered
if (*head == NULL)
{
*head = newNode;
(*head)->next = *head;
return;
}
// if LL already as >=1 node
struct Node *curr = *head;
// traverse till last node in LL
while (curr->next != *head)
{
curr = curr->next;
}
// assign LL's current last node's next as this new node
curr->next = newNode;
// assign this new node's next as current head of LL
newNode->next = *head;
}
void display (struct Node *head)
{
// if there are no node in LL
if (head == NULL)
return;
struct Node *temp = head;
//need to take care of circular structure of LL
do
{
printf ("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
printf ("\n");
}
int main ()
{
struct Node *head = NULL;
printf("Linked List: ");
insertLast (&head, 0);
insertLast (&head, 10);
insertLast (&head, 20);
insertLast (&head, 30);
insertLast (&head, 40);
display (head);
return 0;
}
Output
Linked List: 0 10 20 30 40
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment