Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 bool findInDLL(DoubleNode* head, Object value);

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 bool insertBeforeDLL(DoubleNode*& head, Object value, Object newValue);

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 bool eraseInDLL(DoubleNode*& head, Object value);

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 #include using namespace std;

//Doubly Linked List structure template struct DoubleNode { Object data; DoubleNode *prev; DoubleNode *next;

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 DoubleNode* createDLL(Object ary[], int size) { DoubleNode* first = new DoubleNode(ary[0]); DoubleNode* temp = first; for (int i = 1; i < size; i++) { DoubleNode* node = new DoubleNode(ary[i]); temp->next = node; node->prev = temp; temp = node; } return first; } template void printDLL(DoubleNode* head) { while (head != nullptr) { cout << head->data << "\t"; head = head->next; } cout << endl; } template bool findInDLL(DoubleNode* head, Object value) { //your code goes here } template bool insertBeforeDLL(DoubleNode* & head, Object givenValue, Object newValue) { DoubleNode *newNode = new DoubleNode(newValue); //your code goes here } template bool eraseInDLL(DoubleNode*& head, Object givenValue) { DoubleNode *temp = head; //your code goes here } int main() { int ary[] = { 1,2,4,7,6,8 }, size = 6; DoubleNode* head = createDLL(ary, size);

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

Diagram the experiment suggested in question

Answered: 1 week ago

Question

3. Who would the members be?

Answered: 1 week ago