Question
/* c++ Considering the doubly linked list, Fix the deleteDuplicates_Unsorted function and create another function to delete duplicates for a sorted linked list. */ #include
/* c++ Considering the doubly linked list, Fix the deleteDuplicates_Unsorted function and create another function to delete duplicates for a sorted linked list. */
#include
class NodeDC { public: double data; NodeDC *prev, *next; NodeDC() : data(0), prev(nullptr), next(nullptr) {} NodeDC(double data) : data(data), prev(nullptr), next(nullptr) {} ~NodeDC() {} }; NodeDC* head;
class DCList { public: void add_2Tail(double data) { if (head == nullptr) { head = new NodeDC(data); return; } NodeDC* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = new NodeDC(data); temp->next->prev = temp; }
void Display() { if (head == nullptr) { cout << "List is empty." << endl << endl; return; } cout << "List:" << " "; NodeDC* temp = head; while (temp != nullptr) { cout << temp->data << " "; temp = temp->next; } cout << endl << endl; } void deleteDuplicates_Unsorted(); };
void DCList::deleteDuplicates_Unsorted() { NodeDC* ptr1 = head; NodeDC* ptr2 = nullptr; NodeDC* dup = nullptr; while (ptr1 != nullptr && ptr1->next != nullptr) { ptr2 = ptr1; while (ptr2->next != nullptr) { if (ptr1->data == ptr2->next->data) { dup = ptr2->next; ptr2 = ptr2->next->next; delete(dup); } else { ptr2 = ptr2->next; } ptr1 = ptr1->next; } } }
int main() { DCList* list = new DCList(); list->Display(); cout << "Adding elements..." << endl << endl; list->add_2Tail(1.1); list->add_2Tail(2.2); list->add_2Tail(3.3); list->add_2Tail(4.4); list->add_2Tail(5.5); list->Display(); delete list; DCList List; cout << "Adding elements..." << endl << endl; List.add_2Tail(5.5); List.add_2Tail(4.4); List.add_2Tail(3.3); List.add_2Tail(2.2); List.add_2Tail(1.1); List.Display(); List.deleteDuplicates_Unsorted(); List.Display();
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