# Deletion in Circular Linked List in C

## C Program for Circular Linked List Deletion

In the Deletion in Circular Linked List in C  we can delete the node in the following ways :

• Deletion from the Beginning
• Deletion from the End
• Deletion from the specific Position

## Deletion in Circular Linked List in C

In this topic we want to discuss about delete the any node in circular linked list So, In this scenario is also have three ways to delete the node in linked list like doubly or singly linked list.

• Deletion at beginning
• Deletion at middle
• Deletion at end

1. It is not easy to reverse the linked list.
2. If proper care is not taken, then the problem of infinite loop can occur.

3. If we are at a node, then we can go to any node. But in linear linked list it is not possible to go to previous node. Easy implementation of some operations is possible

### Deletion at beginning

Let’s have a look at the code for deletion at the beginning –
```void deleteBegin(struct Node **head)
{

// if there are no nodes in Linked List can't delete
{
printf ("Linked List Empty, nothing to delete");
return;
}

// if only 1 node in CLL
{
return;
}

// traverse till last node in CLL
curr = curr->next;

// assign last node's next to 2nd node in CLL

// move head to next node
free (temp);
}
```

### Deletion at end

Let’s have a look at the code for deletion at the end –
```void deleteEnd (struct Node **head)
{
struct Node *previous;

// if there are no nodes in Linked List can't delete
{
printf ("Linked List Empty, nothing to delete");
return;
}

// if Linked List has only 1 node
{
return;
}

// else traverse to the last node
{
// store previous link node as we need to change its next val
previous = temp;
temp = temp->next;
}
// Curr assign 2nd last node's next to head
// delete the last node
free (temp);
// 2nd last now becomes the last node
}
```

### Deletion in between (After a given node)

Let’s have a look at the code for deletion after a given node –
```void deletePos (struct Node **head, int n)
{

if (n < 1 || size < n)
{
printf ("Can't delete, %d is not a valid position\n", n);
}

else if (n == 1)

else if (n == size)

else
{
struct Node *previous;

// traverse to the nth node
while (--n)
{
// store previous link node as we need to change its next val
previous = temp;
temp = temp->next;
}

// change previous node's next node to nth node's next node
previous->next = temp->next;
// delete this nth node
free (temp);
}
}
```

#### Code for Deletion in circular

Run
```#include<stdio.h>
#include<stdlib.h>
// structure for Circular Linked List
struct Node
{
int data;
struct Node *next;
};

{

// if there are no nodes in Linked List can't delete
{
printf ("Linked List Empty, nothing to delete");
return;
}

// if only 1 node in CLL
{
return;
}

// traverse till last node in CLL
curr = curr->next;

// assign last node's next to 2nd node in CLL

// move head to next node
free (tempNode);
}

{
struct Node *previous;

// if there are no nodes in Linked List can't delete
{
printf ("Linked List Empty, nothing to delete");
return;
}

// if Linked List has only 1 node
{
return;
}

// else traverse to the last node
{
// 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
// delete the last node
free (tempNode);
// 2nd last now becomes the last node
}

void deletePos (struct Node **head, int n)
{

if (n < 1 || size < n)
{
printf ("Can't delete, %d is not a valid position\n", n);
}

else if (n == 1)

else if (n == size)

else
{
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;

{
return;
}

{
curr = curr->next;
}

curr->next = newNode;
}

{
int size = 0;

if (temp == NULL)
return size;

do
{
size++;
temp = temp->next;

}

return size;
}

{
// if there are no node in CLL
return;

//need to take care of circular structure of CLL
do
{
printf ("%d ", temp->data);
temp = temp->next;

}
printf ("\n");
}

int main ()
{

// first node will be null at creation

return 0;
}
```

#### Output

```16 15 14 13 12 11 10
15 14 13 12 11 10
15 14 13 12 11
15 14 12 11```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java