From a5adda89302f24981e17cf9402af3649508b2a39 Mon Sep 17 00:00:00 2001 From: Aditya Chouksey Date: Fri, 17 Oct 2025 11:49:27 +0530 Subject: [PATCH] Create Doubly_linkedlist.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This C program implements a Doubly Linked List (DLL), a linear data structure in which each node contains three parts — data, a pointer to the previous node, and a pointer to the next node. Unlike a singly linked list, a doubly linked list allows bidirectional traversal, enabling easier insertion and deletion from both ends of the list. The program provides a menu-driven interface that allows users to: Insert a node at the beginning of the list Insert a node at the end of the list Delete a specific node by its value Display the list in forward order (from head to tail) Display the list in reverse order (from tail to head) It uses dynamic memory allocation (malloc) for creating new nodes and ensures that node connections are properly updated during insertions and deletions. The program continues running until the user chooses to exit. --- data_structures/list/Doubly_linkedlist.c | 158 +++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 data_structures/list/Doubly_linkedlist.c diff --git a/data_structures/list/Doubly_linkedlist.c b/data_structures/list/Doubly_linkedlist.c new file mode 100644 index 0000000000..057e898b46 --- /dev/null +++ b/data_structures/list/Doubly_linkedlist.c @@ -0,0 +1,158 @@ +#include +#include + +// Define structure for a node +struct Node { + int data; + struct Node* prev; + struct Node* next; +}; + +// Head pointer +struct Node* head = NULL; + +// Function to create a new node +struct Node* createNode(int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->prev = NULL; + newNode->next = NULL; + return newNode; +} + +// Insert at the beginning +void insertAtBeginning(int data) { + struct Node* newNode = createNode(data); + if (head == NULL) + head = newNode; + else { + newNode->next = head; + head->prev = newNode; + head = newNode; + } + printf("Inserted %d at the beginning.\n", data); +} + +// Insert at the end +void insertAtEnd(int data) { + struct Node* newNode = createNode(data); + if (head == NULL) { + head = newNode; + return; + } + struct Node* temp = head; + while (temp->next != NULL) + temp = temp->next; + temp->next = newNode; + newNode->prev = temp; + printf("Inserted %d at the end.\n", data); +} + +// Delete a node by value +void deleteNode(int data) { + if (head == NULL) { + printf("List is empty.\n"); + return; + } + struct Node* temp = head; + + // Traverse to find the node + while (temp != NULL && temp->data != data) + temp = temp->next; + + if (temp == NULL) { + printf("Node with value %d not found.\n", data); + return; + } + + // Adjust pointers + if (temp->prev != NULL) + temp->prev->next = temp->next; + else + head = temp->next; // deleting head + + if (temp->next != NULL) + temp->next->prev = temp->prev; + + free(temp); + printf("Deleted node with value %d.\n", data); +} + +// Display the list forward +void displayForward() { + struct Node* temp = head; + if (temp == NULL) { + printf("List is empty.\n"); + return; + } + printf("List (Forward): "); + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); +} + +// Display the list backward +void displayBackward() { + struct Node* temp = head; + if (temp == NULL) { + printf("List is empty.\n"); + return; + } + while (temp->next != NULL) + temp = temp->next; + + printf("List (Backward): "); + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->prev; + } + printf("\n"); +} + +// Main menu +int main() { + int choice, data; + while (1) { + printf("\n--- Doubly Linked List Menu ---\n"); + printf("1. Insert at Beginning\n"); + printf("2. Insert at End\n"); + printf("3. Delete a Node\n"); + printf("4. Display Forward\n"); + printf("5. Display Backward\n"); + printf("6. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter data: "); + scanf("%d", &data); + insertAtBeginning(data); + break; + case 2: + printf("Enter data: "); + scanf("%d", &data); + insertAtEnd(data); + break; + case 3: + printf("Enter data to delete: "); + scanf("%d", &data); + deleteNode(data); + break; + case 4: + displayForward(); + break; + case 5: + displayBackward(); + break; + case 6: + printf("Exiting program.\n"); + exit(0); + default: + printf("Invalid choice. Try again.\n"); + } + } + return 0; +}