mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
Refactor Doubly Linked List implementation
This commit is contained in:
+105
-121
@@ -1,147 +1,131 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Define the DLL Node
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node *prev; // Pointer to previous node
|
||||
struct Node *next; // Pointer to next node
|
||||
};
|
||||
/* Node structure */
|
||||
typedef struct node {
|
||||
int data;
|
||||
struct node *prev;
|
||||
struct node *next;
|
||||
} NODE;
|
||||
|
||||
// Global Head Pointer
|
||||
struct Node *head = NULL;
|
||||
/* Global head pointer */
|
||||
NODE *head = NULL;
|
||||
|
||||
// a) Insert a node (Create/Append) - 15 Marks
|
||||
// We insert at the END to simulate "Creating" a list naturally
|
||||
/* Insert node at end (Create list) */
|
||||
void insert(int value) {
|
||||
struct Node *temp;
|
||||
NODE *newNode, *temp;
|
||||
|
||||
// 1. Allocate memory
|
||||
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
|
||||
if (newNode == NULL) {
|
||||
printf("Memory allocation failed\n");
|
||||
return;
|
||||
}
|
||||
newNode = (NODE *)malloc(sizeof(NODE));
|
||||
if (!newNode) {
|
||||
printf("Memory allocation failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Set data
|
||||
newNode->data = value;
|
||||
newNode->next = NULL; // Since it will be the last node
|
||||
newNode->data = value;
|
||||
newNode->next = NULL;
|
||||
|
||||
// 3. Case 1: List is Empty
|
||||
if (head == NULL) {
|
||||
newNode->prev = NULL; // First node has no prev
|
||||
head = newNode;
|
||||
printf("Node %d inserted as head.\n", value);
|
||||
return;
|
||||
}
|
||||
if (head == NULL) {
|
||||
newNode->prev = NULL;
|
||||
head = newNode;
|
||||
printf("Inserted %d\n", value);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Case 2: List has data. Traverse to the end.
|
||||
temp = head;
|
||||
while (temp->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
temp = head;
|
||||
while (temp->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// 5. Link the new node
|
||||
temp->next = newNode; // Old last node points to new node
|
||||
newNode->prev = temp; // New node points back to old last node
|
||||
temp->next = newNode;
|
||||
newNode->prev = temp;
|
||||
|
||||
printf("Node %d inserted at end.\n", value);
|
||||
printf("Inserted %d\n", value);
|
||||
}
|
||||
|
||||
// b) Delete a node (by value) - 15 Marks
|
||||
void delete(int value) {
|
||||
struct Node *temp = head;
|
||||
/* Delete node by value */
|
||||
void delete_value(int value) {
|
||||
NODE *temp = head;
|
||||
|
||||
// 1. Check if list is empty
|
||||
if (head == NULL) {
|
||||
printf("List is empty.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Search for the node
|
||||
while (temp != NULL && temp->data != value) {
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
// 3. If node not found
|
||||
if (temp == NULL) {
|
||||
printf("Element %d not found.\n", value);
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Deletion Logic ---
|
||||
|
||||
// Case A: Deleting the Head Node
|
||||
if (temp == head) {
|
||||
head = head->next; // Move head forward
|
||||
if (head != NULL) {
|
||||
head->prev = NULL; // New head's prev must be NULL
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Case B: Deleting a Middle or Last Node
|
||||
else {
|
||||
// Link the previous node to the next node
|
||||
temp->prev->next = temp->next;
|
||||
|
||||
// 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;
|
||||
while (temp != NULL && temp->data != value) {
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Free memory
|
||||
free(temp);
|
||||
printf("Node %d deleted.\n", value);
|
||||
if (temp == NULL) {
|
||||
printf("Element not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (temp == head) {
|
||||
head = temp->next;
|
||||
if (head != NULL)
|
||||
head->prev = NULL;
|
||||
} else {
|
||||
temp->prev->next = temp->next;
|
||||
if (temp->next != NULL)
|
||||
temp->next->prev = temp->prev;
|
||||
}
|
||||
|
||||
free(temp);
|
||||
printf("Deleted %d\n", value);
|
||||
}
|
||||
|
||||
// c) Display the list - 5 Marks
|
||||
/* Display list */
|
||||
void display() {
|
||||
struct Node *temp = head;
|
||||
NODE *temp = head;
|
||||
|
||||
if (head == NULL) {
|
||||
printf("List is empty.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("List (Forward): NULL <-> ");
|
||||
while (temp != NULL) {
|
||||
printf("%d <-> ", temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int choice, val;
|
||||
printf("--- Doubly Linked List Program ---\n");
|
||||
|
||||
while (1) {
|
||||
printf("\n1. Insert (Create)\n2. Delete\n3. Display\n4. Exit\n");
|
||||
printf("Enter choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
printf("Enter value: ");
|
||||
scanf("%d", &val);
|
||||
insert(val);
|
||||
break;
|
||||
case 2:
|
||||
printf("Enter value to delete: ");
|
||||
scanf("%d", &val);
|
||||
delete(val);
|
||||
break;
|
||||
case 3:
|
||||
display();
|
||||
break;
|
||||
case 4:
|
||||
return 0;
|
||||
default:
|
||||
printf("Invalid Choice\n");
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
printf("List: ");
|
||||
while (temp != NULL) {
|
||||
printf("%d <-> ", temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
/* Main function */
|
||||
int main() {
|
||||
int choice, value;
|
||||
|
||||
while (1) {
|
||||
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: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
printf("Enter value: ");
|
||||
scanf("%d", &value);
|
||||
insert(value);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
printf("Enter value to delete: ");
|
||||
scanf("%d", &value);
|
||||
delete_value(value);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
display();
|
||||
break;
|
||||
|
||||
case 4:
|
||||
exit(0);
|
||||
|
||||
default:
|
||||
printf("Invalid choice\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user