Question
You are required to implement the following functions for doubly linked lists. Note that code for creating a doubly linked list from an array of
You are required to implement the following functions for doubly linked lists. Note that code for creating a doubly linked list from an array of values is given to you, as well as testing code (driver program) to make sure that all functions work as expected. 1. find: Returns true if the given value is found in the list, and false otherwise. template
2. insertBefore: Finds a given value and inserts a new node before it with a new value. Returns true if the insertion is successful, else false. template
3. erase: Finds a given value and deletes the matching node from the linked list. Returns true if the insertion is successful, else false. template
Sample output:
Printing Double linked list: 1 2 4 7 6 8
Finding 7 in the List: 7 exist in the list
Inserting 3 before 4 : 1 2 3 4 7 6 8
Inserting 0 before 1 (inserting at the start of the list): 0 1 2 3 4 7 6 8
Deleting 3: 0 1 2 4 7 6 8
Deleting 8 (deleting last element in the list): 0 1 2 4 7 6
Deleting 0 (deleting first element in the list): 1 2 4 7 6
Deleting -99 (trying to delete an element which is not present in the list): Value not Found! GIVEN THE C++ TEMPLATE PROGRAM
#include
//Doubly Linked List structure template
DoubleNode(const Object & d = Object{}, DoubleNode * p = nullptr, DoubleNode * n = nullptr) : data{ d }, prev{ p }, next{ n } { } }; //function to create Doubly Linked List with values template
cout << "Printing Double linked list: "; printDLL(head); cout << " Finding 7 in the List: "; if (findInDLL(head, 7)) cout << "7 exist in the list ";
//Testing insertBefore function cout << " Inserting 3 before 4 : "; bool success = insertBeforeDLL(head, 4, 3); if (success) printDLL(head); else cout << "Value not Found! ";
cout << " Inserting 0 before 1 (inserting at the start of the list): "; success = insertBeforeDLL(head, 1, 0); if (success) printDLL(head); else cout << "Value not Found! ";
cout << " Deleting 3: "; success = eraseInDLL(head, 3); if (success) printDLL(head); else cout << "Value not Found! ";
cout << " Deleting 8 (deleting last element in the list): "; success = eraseInDLL(head, 8); if (success) printDLL(head); else cout << "Value not Found! ";
cout << " Deleting 0 (deleting first element in the list): "; success = eraseInDLL(head, 0); if (success) printDLL(head); else cout << "Value not Found! ";
cout << " Deleting -99 (trying to delete an element which is not present in the list): "; success = eraseInDLL(head, -99); if (success) printDLL(head); else cout << "Value not Found! "; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started