mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
148 lines
3.1 KiB
C
148 lines
3.1 KiB
C
|
|
#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
|
|
};
|
|
|
|
// Global Head Pointer
|
|
struct Node *head = NULL;
|
|
|
|
// a) Insert a node (Create/Append) - 15 Marks
|
|
// We insert at the END to simulate "Creating" a list naturally
|
|
void insert(int value) {
|
|
struct Node *temp;
|
|
|
|
// 1. Allocate memory
|
|
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
|
|
if (newNode == NULL) {
|
|
printf("Memory allocation failed\n");
|
|
return;
|
|
}
|
|
|
|
// 2. Set data
|
|
newNode->data = value;
|
|
newNode->next = NULL; // Since it will be the last node
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 4. Case 2: List has data. Traverse to the end.
|
|
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
|
|
|
|
printf("Node %d inserted at end.\n", value);
|
|
}
|
|
|
|
// b) Delete a node (by value) - 15 Marks
|
|
void delete(int value) {
|
|
struct 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
|
|
}
|
|
}
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
// 4. Free memory
|
|
free(temp);
|
|
printf("Node %d deleted.\n", value);
|
|
}
|
|
|
|
// c) Display the list - 5 Marks
|
|
void display() {
|
|
struct 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");
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|