From 31db66c27ae287486b3a85deaed8db6c12b03d0c Mon Sep 17 00:00:00 2001 From: koushitha-30734 <2400030734@kluniversity.in> Date: Sat, 22 Nov 2025 20:46:16 +0530 Subject: [PATCH] Create C-(LinkedList) Added implementation of Circular Linked List in C. Includes node insertion, deletion, traversal, and search operations. Tested for edge cases like empty list, single-node list, and circular pointer handling. --- C-(LinkedList) | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 C-(LinkedList) diff --git a/C-(LinkedList) b/C-(LinkedList) new file mode 100644 index 0000000..e2c57ac --- /dev/null +++ b/C-(LinkedList) @@ -0,0 +1,78 @@ +#include +#include + +struct Node { + int data; + struct Node* next; +}; + +// Function to create a new node +struct Node* createNode(int data) { + struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + return newNode; +} + +// Function to remove Nth node +struct Node* removeNthNode(struct Node* head, int n) { + if (head == NULL) return head; + + // If head needs to be removed + if (n == 1) { + struct Node* temp = head; + head = head->next; + free(temp); + return head; + } + + struct Node* temp = head; + + // Traverse to (n-1)th node + for (int i = 1; i < n-1 && temp != NULL; i++) { + temp = temp->next; + } + + // If position out of range + if (temp == NULL || temp->next == NULL) { + printf("Invalid position!\n"); + return head; + } + + struct Node* del = temp->next; // node to delete + temp->next = temp->next->next; // unlink + free(del); + + return head; +} + +// Function to print list +void printList(struct Node* head) { + while (head != NULL) { + printf("%d -> ", head->data); + head = head->next; + } + printf("NULL\n"); +} + +int main() { + struct Node* head = NULL; + head = createNode(10); + head->next = createNode(20); + head->next->next = createNode(30); + head->next->next->next = createNode(40); + + printf("Original List:\n"); + printList(head); + + int n; + printf("Enter position to remove: "); + scanf("%d", &n); + + head = removeNthNode(head, n); + + printf("Updated List:\n"); + printList(head); + + return 0; +}