5.linked list #include #include // Define node structure struct Node { int data; struct Node *next; }; struct Node *head = NULL; // Function to insert a node at the beginning void insertAtBeginning(int value) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = head; head = newNode; printf("%d inserted at beginning.\n", value); } // Function to insert a node at the end void insertAtEnd(int value) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; if (head == NULL) { head = newNode; } else { struct Node *temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; } printf("%d inserted at end.\n", value); } // Function to delete a node by value void deleteNode(int value) { struct Node *temp = head, *prev = NULL; if (temp == NULL) { printf("List is empty!\n"); return; } if (temp != NULL && temp->data == value) { head = temp->next; free(temp); printf("%d deleted from list.\n", value); return; } while (temp != NULL && temp->data != value) { prev = temp; temp = temp->next; } if (temp == NULL) { printf("%d not found in list.\n", value); return; } prev->next = temp->next; free(temp); printf("%d deleted from list.\n", value); } // Function to display the linked list void display() { struct Node *temp = head; if (temp == NULL) { printf("List is empty!\n"); return; } printf("Linked List: "); while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { int choice, value; while (1) { printf("\n--- 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\n"); printf("5. 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: display(); break; case 5: exit(0); default: printf("Invalid choice! Try again.\n"); } } return 0; }