#include <stdio.h>
#include <stdlib.h>
// Node structure for circular linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = newNode; // points to itself initially
return newNode;
}
// Function to insert node at end of circular linked list
void insertEnd(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next; // advance to last node
}
temp->next = newNode; // last node points to new node
newNode->next = *head; // new node points to head
}
// Function to delete node with given value from circular linked list
void deleteNode(Node** head, int key) {
if (*head == NULL) return; // empty list
Node *curr = *head, *prev = NULL;
// If head node itself holds the key and list has only one node
if (curr->data == key && curr->next == *head) {
free(curr);
*head = NULL;
return;
}
// If head node holds the key and list has more than one node
if (curr->data == key) {
// Find last node to update its next pointer
Node* last = *head;
while (last->next != *head) {
last = last->next; // advance to last node
}
last->next = curr->next; // last node points to second node
*head = curr->next; // head updated to second node
free(curr); // delete old head
return;
}
// Search for the node to delete
prev = curr;
curr = curr->next;
while (curr != *head && curr->data != key) {
prev = curr;
curr = curr->next; // advance curr and prev
}
// If node with key found
if (curr != *head && curr->data == key) {
prev->next = curr->next; // bypass curr
free(curr); // delete node
}
}
// Function to print circular linked list
void printList(Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next; // advance to next node
} while (temp != head);
printf("(back to head %d)\n", head->data);
}
int main() {
Node* head = NULL;
insertEnd(&head, 1);
insertEnd(&head, 2);
insertEnd(&head, 3);
insertEnd(&head, 4);
printf("Original list:\n");
printList(head);
deleteNode(&head, 3);
printf("After deleting node with value 3:\n");
printList(head);
return 0;
}
while (temp->next != *head) { temp = temp->next; }
advance temp to last node to insert new node at end
if (curr->data == key && curr->next == *head) { free(curr); *head = NULL; return; }
handle deletion when only one node exists and it matches key
if (curr->data == key) { find last node; update last->next; update head; free old head; return; }
handle deletion when head node matches key and list has multiple nodes
while (curr != *head && curr->data != key) { prev = curr; curr = curr->next; }
search for node with key by traversing list
if (curr != *head && curr->data == key) { prev->next = curr->next; free(curr); }
remove found node by bypassing it and freeing memory
Original list:
1 -> 2 -> 3 -> 4 -> (back to head 1)
After deleting node with value 3:
1 -> 2 -> 4 -> (back to head 1)