0% found this document useful (0 votes)
2 views

Algorithm for Deleting a Node in a Circular Linked List

The document outlines an algorithm for deleting a node in a circular linked list, detailing steps for handling empty lists, single-node lists, and general cases for node deletion. It includes C code for inserting and deleting nodes, as well as displaying the list. Key points emphasize maintaining circular linkage and preventing memory leaks during deletion.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm for Deleting a Node in a Circular Linked List

The document outlines an algorithm for deleting a node in a circular linked list, detailing steps for handling empty lists, single-node lists, and general cases for node deletion. It includes C code for inserting and deleting nodes, as well as displaying the list. Key points emphasize maintaining circular linkage and preventing memory leaks during deletion.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Algorithm for Deleting a Node in a Circular Linked List

1. Check if the list is empty

o If head == NULL, print "List is empty" and return.

2. If the list contains only one node

o If head->next == head, free the node and set head = NULL.

3. If the node to be deleted is the head node

o Find the last node.

o Update the last node’s next pointer to point to head->next.

o Update head to head->next.

o Free the old head node.

4. Otherwise, traverse to find the node to be deleted

o Maintain a previous pointer.

o If the node is found, update the next pointer of the previous node to skip the node to
be deleted.

o Free the deleted node.

C Code for Deleting a Node in a Circular Linked List

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

// Function to insert at the end of Circular Linked List

void insertAtEnd(struct Node** head, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

if (*head == NULL) {

newNode->next = newNode;

*head = newNode;

} else {

struct Node* temp = *head;

while (temp->next != *head) {

temp = temp->next;

temp->next = newNode;

newNode->next = *head;

// Function to delete a node from a Circular Linked List

void deleteNode(struct Node** head, int key) {

if (*head == NULL) {

printf("List is empty.\n");

return;

struct Node *current = *head, *prev = NULL;

// Case 1: Single node in the list

if (current->next == *head && current->data == key) {

free(current);
*head = NULL;

return;

// Case 2: Deleting the head node

if ((*head)->data == key) {

struct Node* last = *head;

// Find the last node

while (last->next != *head) {

last = last->next;

last->next = (*head)->next;

struct Node* temp = *head;

*head = (*head)->next;

free(temp);

return;

// Case 3: Deleting a node that is not the head

do {

prev = current;

current = current->next;

if (current->data == key) {

prev->next = current->next;

free(current);

return;
}

} while (current != *head);

printf("Node with value %d not found.\n", key);

// Function to display the Circular Linked List

void display(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("(Head)\n");

// Main function

int main() {

struct Node* head = NULL;

// Insert elements

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

insertAtEnd(&head, 30);
insertAtEnd(&head, 40);

printf("Original Circular Linked List:\n");

display(head);

// Delete nodes

deleteNode(&head, 10);

printf("\nAfter deleting 10:\n");

display(head);

deleteNode(&head, 30);

printf("\nAfter deleting 30:\n");

display(head);

deleteNode(&head, 50); // Trying to delete a non-existing node

return 0;

Explanation

1. Insertion at End (insertAtEnd)

o Creates a new node.

o If the list is empty, sets next to itself and makes it the head.

o Otherwise, finds the last node and links it to the new node.

2. Deleting a Node (deleteNode)

o If the list is empty, prints "List is empty."

o If the list has only one node, frees the node and sets head = NULL.

o If the node to delete is the head, finds the last node, updates head, and frees the old
node.
o Otherwise, traverses the list to find the node, updates the next pointer of the
previous node, and frees the target node.

3. Displaying the List (display)

o Traverses and prints the list while ensuring it loops correctly.

Output

Original Circular Linked List:

10 -> 20 -> 30 -> 40 -> (Head)

After deleting 10:

20 -> 30 -> 40 -> (Head)

After deleting 30:

20 -> 40 -> (Head)

Node with value 50 not found.

Key Points

• Handles deletion at different positions (head, middle, last, or not found).

• Ensures circular linkage remains intact after deletion.

• Prevents memory leaks by freeing deleted nodes.

You might also like