DS - Unit - 1 - Circular LL Notes
DS - Unit - 1 - Circular LL Notes
In single linked list, every node points to its next node in the sequence and the last node points
NULL. But in circular linked list, every node points to its next node in the sequence but the last
node points to the first node in the list.
Circular linked list is a sequence of elements in which every element has link to its next element
in the sequence and the last element has a link to the first element in the sequence. That means
circular linked list is similar to the single linked list except that the last node points to the first
node in the list Example.
Note that there are no NULL values in the NEXT part of any of the nodes of list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
void displayList();
int main()
int numnodes;
scanf("%d", &numnodes);
createList(numnodes);
displayList();
return 0;
/**
*/
void createList(int n)
int i, data;
head=firstnode;
scanf("%d", &data);
firstnode->data = data;
firstnode->next = NULL;
lastnode = firstnode;
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
lastnode->next = newNode;
lastnode = newNode;
// when Linked List is created with n nodes then to make it circular Link // the
last node with first node
lastnode->next=head;
/**
*/
void displayList()
if(head == NULL)
printf("List is empty.\n");
else
temp = head;
do {
temp = temp->next;
}while(temp!= head);
OUTPUT
Linked List is
21 22 23 24
Step 4. To add new node in the beginning set the next pointer of newnode as
Pseudo code or function to insert newnode in the start of circular linked list is
given here
void insertAtBeginning(int value)
newNode->data = value;
lastnode->next =newNode; //Connect last node with newNode which is now Firstnode
head=newNode
}
Case 2- Insert newnode at the end of circular Linked List
Step 4. To add new node in the end of circular linked list set the next pointer of
lastnode with newnode and set next pointer of newnode with head to connect it
with first node of linked list and make the newnode as last node
// Pseudocode or Function to insert newnode at the End of Circular Linked List is given
//below
newnode->data = value;
newnode->next=firstnode;
lastnode->next = newnode;
lastnode = newnode;
Step 4 – Enter the data value of the node after which you want to insert the
newnode.
Step 5 - Define two node pointer temp1 and temp2 and initialize with them with
head and move temp1 and temp2 until the temp2 represents that node after which
you have to insert new node and temp1 refers to node next to temp2.
Step 6 – Set the next pointer of temp2 as newnode and next pointer of nenode with
temp1.
/* Pseudo Code or Function to insert new node after a given node in CLL is given below
newnode->next = NULL;
temp1 = head;
temp2 = head;
/*traverse the linkedlist till the temp2 refer to the node after which you want to insert new //
node*/
do
temp2=temp1;
temp1 = temp1->next;
}while(temp2->data!=num1);
temp2->next = newnode;
newnode->next=temp1 ;
}
Deletion Operation in Circular Linked List
Step 2 – If Linked List is empty the display the message linked list is empty
deletion is not possible
Step 3- If Linked List is not empty the to delete the first node of Circular Linked
list do the following
3.1 – Declare a node pointer temp and initialize it with head ( first node)
3.2 – Set the head pointer as head->next and set next field of lastnode as head.
void deletefrombegining()
if(head==NULL)
else
struct node*temp=head;
head=head->next;
lastnode->next=head;
free(temp);
}
Case 2 – Deleting Last node or from end of Circular Linked List
Step 2 – If Linked List is empty the display the message linked list is empty
deletion is not possible
Step 3- If Linked List is not empty the to delete the last node of Circular Linked
list do the following
3.1 -- Define two node pointer temp1 and temp2 and initialize with them with
head and move temp1 and temp2 until the temp2 refers to the second last node and
temp1 refers to the last node of circular linked list.
if(head==NULL)
else
struct node*temp1,*temp2;
temp1=head;
temp2=head;
do
{
temp2=temp1;
temp1=temp1->next;
}while(temp1->next!=head);
free(temp1);
Step 2 – If Linked List is empty the display the message linked list is empty
deletion is not possible
Step 3- If Linked List is not empty the to delete a node after a given node in
Circular Linked list do the following
3.1 – Enter the data filed value of that node after which you have to delete the node
in Circular Linked List
3.2-- Define two node pointer temp1 and temp2 and initialize with them with head
and move temp1 and temp2 until the temp1 refers to the node to be deleted and
temp2 refers to that node after which we have to delete the bode.
if(head==NULL)
else
temp1 = head;
temp2 = head;
// traverse the linkedlist till the temp2 refer to the node after which you want to insert new
node
while (temp2->data!=num)
temp2=temp1;
temp1 = temp1->next;
temp2->next = temp1->next;
free(temp1);