Professional Documents
Culture Documents
// Function to delete a node with given data from the linked list
Node* deleteNode(Node* head, int data) {
Node* prev = nullptr;
Node* curr = head;
int main() {
Node* head = nullptr;
int choice, data;
do {
cout << "1. Insert Element" << endl;
cout << "2. Delete Element" << endl;
cout << "3. Search Element" << endl;
cout << "4. Display List" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter element to insert: ";
cin >> data;
head = insertNode(head, data);
break;
case 2:
cout << "Enter element to delete: ";
cin >> data;
head = deleteNode(head, data);
break;
case 3:
cout << "Enter element to search: ";
cin >> data;
if (searchNode(head, data))
cout << "Element found in the list." << endl;
else
cout << "Element not found in the list." << endl;
break;
case 4:
cout << "List elements: ";
displayList(head);
break;
case 5:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
}
} while (choice != 5);
return 0;
}