Refactor Doubly Linked List implementation

This commit is contained in:
2026-01-22 17:48:00 +05:30
committed by GitHub
parent 66fafe8813
commit 784f982a64
+53 -69
View File
@@ -1,111 +1,89 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// Define the DLL Node /* Node structure */
struct Node { typedef struct node {
int data; int data;
struct Node *prev; // Pointer to previous node struct node *prev;
struct Node *next; // Pointer to next node struct node *next;
}; } NODE;
// Global Head Pointer /* Global head pointer */
struct Node *head = NULL; NODE *head = NULL;
// a) Insert a node (Create/Append) - 15 Marks /* Insert node at end (Create list) */
// We insert at the END to simulate "Creating" a list naturally
void insert(int value) { void insert(int value) {
struct Node *temp; NODE *newNode, *temp;
// 1. Allocate memory newNode = (NODE *)malloc(sizeof(NODE));
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); if (!newNode) {
if (newNode == NULL) {
printf("Memory allocation failed\n"); printf("Memory allocation failed\n");
return; return;
} }
// 2. Set data
newNode->data = value; newNode->data = value;
newNode->next = NULL; // Since it will be the last node newNode->next = NULL;
// 3. Case 1: List is Empty
if (head == NULL) { if (head == NULL) {
newNode->prev = NULL; // First node has no prev newNode->prev = NULL;
head = newNode; head = newNode;
printf("Node %d inserted as head.\n", value); printf("Inserted %d\n", value);
return; return;
} }
// 4. Case 2: List has data. Traverse to the end.
temp = head; temp = head;
while (temp->next != NULL) { while (temp->next != NULL) {
temp = temp->next; temp = temp->next;
} }
// 5. Link the new node temp->next = newNode;
temp->next = newNode; // Old last node points to new node newNode->prev = temp;
newNode->prev = temp; // New node points back to old last node
printf("Node %d inserted at end.\n", value); printf("Inserted %d\n", value);
} }
// b) Delete a node (by value) - 15 Marks /* Delete node by value */
void delete(int value) { void delete_value(int value) {
struct Node *temp = head; NODE *temp = head;
// 1. Check if list is empty
if (head == NULL) { if (head == NULL) {
printf("List is empty.\n"); printf("List is empty\n");
return; return;
} }
// 2. Search for the node
while (temp != NULL && temp->data != value) { while (temp != NULL && temp->data != value) {
temp = temp->next; temp = temp->next;
} }
// 3. If node not found
if (temp == NULL) { if (temp == NULL) {
printf("Element %d not found.\n", value); printf("Element not found\n");
return; return;
} }
// --- Deletion Logic ---
// Case A: Deleting the Head Node
if (temp == head) { if (temp == head) {
head = head->next; // Move head forward head = temp->next;
if (head != NULL) { if (head != NULL)
head->prev = NULL; // New head's prev must be NULL head->prev = NULL;
} } else {
}
// Case B: Deleting a Middle or Last Node
else {
// Link the previous node to the next node
temp->prev->next = temp->next; temp->prev->next = temp->next;
if (temp->next != NULL)
// If there IS a next node (i.e., we are not deleting the last node),
// Link the next node back to the previous node
if (temp->next != NULL) {
temp->next->prev = temp->prev; temp->next->prev = temp->prev;
} }
}
// 4. Free memory
free(temp); free(temp);
printf("Node %d deleted.\n", value); printf("Deleted %d\n", value);
} }
// c) Display the list - 5 Marks /* Display list */
void display() { void display() {
struct Node *temp = head; NODE *temp = head;
if (head == NULL) { if (head == NULL) {
printf("List is empty.\n"); printf("List is empty\n");
return; return;
} }
printf("List (Forward): NULL <-> "); printf("List: ");
while (temp != NULL) { while (temp != NULL) {
printf("%d <-> ", temp->data); printf("%d <-> ", temp->data);
temp = temp->next; temp = temp->next;
@@ -113,35 +91,41 @@ void display() {
printf("NULL\n"); printf("NULL\n");
} }
/* Main function */
int main() { int main() {
int choice, val; int choice, value;
printf("--- Doubly Linked List Program ---\n");
while (1) { while (1) {
printf("\n1. Insert (Create)\n2. Delete\n3. Display\n4. Exit\n"); printf("\n--- DOUBLY LINKED LIST MENU ---\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter choice: "); printf("Enter choice: ");
scanf("%d", &choice); scanf("%d", &choice);
switch (choice) { switch (choice) {
case 1: case 1:
printf("Enter value: "); printf("Enter value: ");
scanf("%d", &val); scanf("%d", &value);
insert(val); insert(value);
break; break;
case 2: case 2:
printf("Enter value to delete: "); printf("Enter value to delete: ");
scanf("%d", &val); scanf("%d", &value);
delete(val); delete_value(value);
break; break;
case 3: case 3:
display(); display();
break; break;
case 4:
return 0;
default:
printf("Invalid Choice\n");
}
}
return 0;
}
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
}