Circular Linked List Insertion and Deletion Program in C
February 18, 2023
C Program for Circular Linked List Insertion and Deletion
On this page, we will look at the following –
Insertion/Deletion at Start
Insertion/Deletion at the End
Insertion/Deletion at a Position
What is a Circular Linked List
A Circular Linked List is almost very similar to a singly linked list. With just one difference that the last node of the circular linked list is connected to the first node in the list.
While in a singly linked list the last node is connected to a null node.
Following are some terminologies of a Circular Linked List –
Head node
Node
Next Pointer
Data
Some variations also support a tail node that represents the last node in a circular linked List
Possible Positions to insert / delete
In the programs we will look at we will do insertions/deletions at the following positions –
#include<stdio.h>
#include<stdlib.h>
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 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;
}
int getCurrSize (struct Node *node)
{
int size = 0;
while (node != NULL)
{
node = node->next;
size++;
}
return size;
}
void insertPosition (int data, int pos, struct Node **head)
//function to insert element at specific position
{
struct Node *newnode, *curNode;
int i;
if (*head == NULL)
{
printf ("List is empty");
}
if (pos == 1)
{
insertStart (head, data);
return;
}
else
{
newnode = (struct Node *) malloc (sizeof (struct Node));
newnode->data = data;
curNode = *head;
while (--pos > 1)
{
curNode = curNode->next;
}
newnode->next = curNode->next;
curNode->next = 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("Insert at beginning: ");
insertStart (&head, 2);
insertStart (&head, 1);
display (head);
printf("Insert at End: ");
insertLast (&head, 30);
insertLast (&head, 40);
display (head);
printf("Insert at Specific Position: ");
insertPosition (5, 3, &head);
display (head);
return 0;
}
Output
Insert at beginning: 1 2
Insert at End: 1 2 30 40
Insert at Specific Position: 1 2 5 30 40
#include<stdio.h>
#include<stdlib.h>
// structure for Circular Linked List
struct Node
{
int data;
struct Node *next;
};
int calcSize (struct Node *head);
void deleteBegin (struct Node **head)
{
struct Node *tempNode = *head;
// if there are no nodes in Linked List can't delete
if (*head == NULL)
{
printf ("Linked List Empty, nothing to delete");
return;
}
// if only 1 node in CLL
if (tempNode->next == *head)
{
*head = NULL;
return;
}
struct Node *curr = *head;
// traverse till last node in CLL
while (curr->next != *head)
curr = curr->next;
// assign last node's next to 2nd node in CLL
curr->next = (*head)->next;
// move head to next node
*head = (*head)->next;
free (tempNode);
}
void deleteEnd (struct Node **head)
{
struct Node *tempNode = *head;
struct Node *previous;
// if there are no nodes in Linked List can't delete
if (*head == NULL)
{
printf ("Linked List Empty, nothing to delete");
return;
}
// if Linked List has only 1 node
if (tempNode->next == *head)
{
*head = NULL;
return;
}
// else traverse to the last node
while (tempNode->next != *head)
{
// store previous link node as we need to change its next val
previous = tempNode;
tempNode = tempNode->next;
}
// Curr assign 2nd last node's next to head
previous->next = *head;
// delete the last node
free (tempNode);
// 2nd last now becomes the last node
}
void deletePos (struct Node **head, int n)
{
int size = calcSize (*head);
if (n < 1 || size < n)
{
printf ("Can't delete, %d is not a valid position\n", n);
}
else if (n == 1)
deleteBegin (head);
else if (n == size)
deleteEnd (head);
else
{
struct Node *tempNode = *head;
struct Node *previous; // traverse to the nth node
while (--n)
{ // store previous link node as we need to change its next val
previous = tempNode;
tempNode = tempNode->next;
}
// change previous node's next node to nth node's next node
previous->next = tempNode->next;
// delete this nth node
free (tempNode);
}
}
void insert (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
if (*head == NULL)
{
*head = newNode;
(*head)->next = *head;
return;
}
struct Node *curr = *head;
while (curr->next != *head)
{
curr = curr->next;
}
curr->next = newNode;
newNode->next = *head;
*head = newNode;
}
int calcSize (struct Node *head)
{
int size = 0;
struct Node *temp = head;
if (temp == NULL)
return size;
do
{
size++;
temp = temp->next;
}
while (temp != head);
return size;
}
void display (struct Node *head)
{
// if there are no node in CLL
if (head == NULL)
return;
struct Node *temp = head;
//need to take care of circular structure of CLL
do
{
printf ("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
printf ("\n");
}
int main ()
{
// first node will be null at creation
struct Node *head = NULL;
insert (&head, 10);
insert (&head, 11);
insert (&head, 12);
insert (&head, 13);
insert (&head, 14);
insert (&head, 15);
insert (&head, 16);
display (head);
deleteBegin (&head);
display (head);
deleteEnd (&head);
display (head);
deletePos (&head, 3);
display (head);
return 0;
}
Login/Signup to comment