6.dobly linked list #include #include // Define node structure for doubly linked list struct Node { int data; struct Node *prev; struct Node *next; }; struct Node *head = NULL; // Function to create a new node struct Node* createNode(int value) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->prev = NULL; newNode->next = NULL; return newNode; } // Insert node at the beginning void insertAtBeginning(int value) { struct Node *newNode = createNode(value); if (head == NULL) { head = newNode; } else { newNode->next = head; head->prev = newNode; head = newNode; } printf("%d inserted at beginning.\n", value); } // Insert node at the end void insertAtEnd(int value) { struct Node *newNode = createNode(value); if (head == NULL) { head = newNode; } else { struct Node *temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; newNode->prev = temp; } printf("%d inserted at end.\n", value); } // Delete a node by value void deleteNode(int value) { struct Node *temp = head; if (head == NULL) { printf("List is empty!\n"); return; } // If head node is to be deleted if (temp->data == value) { head = temp->next; if (head != NULL) head->prev = NULL; free(temp); printf("%d deleted from list.\n", value); return; } // Search for the node to be deleted while (temp != NULL && temp->data != value) temp = temp->next; if (temp == NULL) { printf("%d not found in list.\n", value); return; } if (temp->next != NULL) temp->next->prev = temp->prev; if (temp->prev != NULL) temp->prev->next = temp->next; free(temp); printf("%d deleted from list.\n", value); } // Display the list from start to end 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("NULL\n"); } // Display the list from end to start void displayBackward() { struct Node *temp = head; if (temp == NULL) { printf("List is empty!\n"); return; } // Move to last node while (temp->next != NULL) temp = temp->next; printf("List (Backward): "); while (temp != NULL) { printf("%d <-> ", temp->data); temp = temp->prev; } printf("NULL\n"); } int main() { int choice, value; while (1) { printf("\n--- Doubly Linked List Operations ---\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 value to insert: "); scanf("%d", &value); insertAtBeginning(value); break; case 2: printf("Enter value to insert: "); scanf("%d", &value); insertAtEnd(value); break; case 3: printf("Enter value to delete: "); scanf("%d", &value); deleteNode(value); break; case 4: displayForward(); break; case 5: displayBackward(); break; case 6: exit(0); default: printf("Invalid choice! Try again.\n"); } } return 0; }