From bce102e018348d48676264d020a6e8a0d3ee0a7c Mon Sep 17 00:00:00 2001 From: guptushar27 <131260251+guptushar27@users.noreply.github.com> Date: Sat, 4 Oct 2025 20:51:41 +0530 Subject: [PATCH 1/2] Create LinkedList.c Adds implementation of a Singly Linked List in C with functions to create, insert, display, and delete nodes. Includes proper memory handling and demonstrates linked list operations. --- C/data_structures/LinkedList.c | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 C/data_structures/LinkedList.c diff --git a/C/data_structures/LinkedList.c b/C/data_structures/LinkedList.c new file mode 100644 index 00000000..85b95477 --- /dev/null +++ b/C/data_structures/LinkedList.c @@ -0,0 +1,92 @@ +#include +#include + +// Define the structure for a node +struct Node { + int data; // To store data + struct Node* next; // Pointer to the next node +}; + +// Function to create a new node +struct Node* createNode(int value) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + if (newNode == NULL) { + printf("Memory allocation failed!\n"); + exit(1); + } + newNode->data = value; + newNode->next = NULL; + return newNode; +} + +// Function to insert a node at the end of the list +void insertEnd(struct Node** head, int value) { + struct Node* newNode = createNode(value); + if (*head == NULL) { + *head = newNode; // If list is empty, make newNode the head + return; + } + struct Node* temp = *head; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = newNode; +} + +// Function to display the linked list +void display(struct Node* head) { + struct Node* temp = head; + printf("Linked List: "); + while (temp != NULL) { + printf("%d -> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); +} + +// Function to delete a node by value +void deleteNode(struct Node** head, int value) { + struct Node *temp = *head, *prev = NULL; + + // If head node itself holds the value + if (temp != NULL && temp->data == value) { + *head = temp->next; // Change head + free(temp); // Free memory + return; + } + + // Search for the value to be deleted + while (temp != NULL && temp->data != value) { + prev = temp; + temp = temp->next; + } + + // If value not found + if (temp == NULL) { + printf("Value %d not found in list.\n", value); + return; + } + + // Unlink node from list and free memory + prev->next = temp->next; + free(temp); +} + +// Main function to demonstrate the linked list +int main() { + struct Node* head = NULL; // Initialize empty list + + // Insert elements + insertEnd(&head, 10); + insertEnd(&head, 20); + insertEnd(&head, 30); + + display(head); // Output: 10 -> 20 -> 30 -> NULL + + // Delete a node + deleteNode(&head, 20); + + display(head); // Output: 10 -> 30 -> NULL + + return 0; +} From fdca1c0135e38a4264fd99d8d8e3c53c0e053c16 Mon Sep 17 00:00:00 2001 From: guptushar27 <131260251+guptushar27@users.noreply.github.com> Date: Sat, 4 Oct 2025 20:54:59 +0530 Subject: [PATCH 2/2] Update LinkedList.c --- C/data_structures/LinkedList.c | 102 ++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/C/data_structures/LinkedList.c b/C/data_structures/LinkedList.c index 85b95477..fcea37d9 100644 --- a/C/data_structures/LinkedList.c +++ b/C/data_structures/LinkedList.c @@ -1,92 +1,140 @@ #include #include -// Define the structure for a node +/* +Problem: +Implement a Singly Linked List that allows insertion, deletion, and display operations. +Each node contains an integer value and a pointer to the next node. +*/ + +// Define the structure for a linked list node struct Node { - int data; // To store data + int data; // Stores the data value struct Node* next; // Pointer to the next node }; -// Function to create a new node +// Function to create a new node with the given value struct Node* createNode(int value) { - struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); - if (newNode == NULL) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory + if (newNode == NULL) { // Check for memory allocation failure printf("Memory allocation failed!\n"); exit(1); } - newNode->data = value; - newNode->next = NULL; + newNode->data = value; // Set data value + newNode->next = NULL; // Initialize next pointer as NULL return newNode; } -// Function to insert a node at the end of the list +// Function to insert a new node at the end of the linked list void insertEnd(struct Node** head, int value) { - struct Node* newNode = createNode(value); - if (*head == NULL) { - *head = newNode; // If list is empty, make newNode the head + struct Node* newNode = createNode(value); // Create new node + if (*head == NULL) { // If list is empty, make newNode the head + *head = newNode; return; } + + // Traverse to the end of the list struct Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } + + // Link the new node at the end temp->next = newNode; } -// Function to display the linked list +// Function to display all elements of the linked list void display(struct Node* head) { struct Node* temp = head; printf("Linked List: "); while (temp != NULL) { - printf("%d -> ", temp->data); - temp = temp->next; + printf("%d -> ", temp->data); // Print current node data + temp = temp->next; // Move to next node } printf("NULL\n"); } -// Function to delete a node by value +// Function to delete a node by its value void deleteNode(struct Node** head, int value) { struct Node *temp = *head, *prev = NULL; - // If head node itself holds the value + // Case 1: If the list is empty + if (*head == NULL) { + printf("List is empty. Nothing to delete.\n"); + return; + } + + // Case 2: If the node to be deleted is the head if (temp != NULL && temp->data == value) { - *head = temp->next; // Change head - free(temp); // Free memory + *head = temp->next; // Move head to next node + free(temp); // Free memory of deleted node return; } - // Search for the value to be deleted + // Case 3: Search for the node to delete while (temp != NULL && temp->data != value) { prev = temp; temp = temp->next; } - // If value not found + // If node not found if (temp == NULL) { printf("Value %d not found in list.\n", value); return; } - // Unlink node from list and free memory + // Unlink and delete the node prev->next = temp->next; free(temp); } -// Main function to demonstrate the linked list +// Main function to demonstrate linked list operations int main() { - struct Node* head = NULL; // Initialize empty list + struct Node* head = NULL; // Initialize an empty linked list - // Insert elements + // Insert some nodes insertEnd(&head, 10); insertEnd(&head, 20); insertEnd(&head, 30); - display(head); // Output: 10 -> 20 -> 30 -> NULL + // Display the linked list + display(head); // Expected Output: 10 -> 20 -> 30 -> NULL - // Delete a node + // Delete a node with value 20 deleteNode(&head, 20); - display(head); // Output: 10 -> 30 -> NULL + // Display list after deletion + display(head); // Expected Output: 10 -> 30 -> NULL return 0; } + +/* +---------------------------------------- +Algorithm Explanation: +---------------------------------------- +1. Create Node: + - Dynamically allocate memory for each node. + - Store the data and set 'next' pointer as NULL. + +2. Insert at End: + - Traverse to the last node. + - Link the newly created node at the end. + +3. Delete by Value: + - Search for the node with the given value. + - Adjust links to remove the node and free its memory. + +4. Display: + - Traverse through the list and print each node’s data. + +---------------------------------------- +Time & Space Complexities: +---------------------------------------- +Operation Time Complexity Space Complexity +--------------------------------------------------------- +Insertion (End) O(n) O(1) +Deletion (By Value) O(n) O(1) +Traversal/Display O(n) O(1) +---------------------------------------- +*/