Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++, Doubly Linked List: frequency of elements /* Fix the countElements function. Considering the List: 1.1 2.2 3.3 4.4 5.5 5.5 3.3 3.3 The current

C++, Doubly Linked List: frequency of elements

/* Fix the countElements function. Considering the List: 1.1 2.2 3.3 4.4 5.5 5.5 3.3 3.3 The current (wrong) output of the countElements function is : 1.1: 1 2.2: 1 3.3: 3 4.4: 1 5.5: 2 5.5: 1 3.3: 2 The correct output should be: 1.1: 1 2.2: 1 3.3: 3 4.4: 1 5.5: 2 */

#include using namespace std;

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() { // Display elements of List 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 delDup_U(); void delDup_S(); void countElements(); };

void DCList::countElements() { // FIX NodeDC* ptr1 = head; if (ptr1 == nullptr || ptr1->next == nullptr) { cout << "List is Empty." << endl << endl; return; } int count = 1; NodeDC* ptr2 = nullptr; while (ptr1 != nullptr && ptr1->next != nullptr) { ptr2 = ptr1->next; while (ptr2 != nullptr) { if (ptr1->data == ptr2->data) { count++; ptr2 = ptr2->next; } else { ptr2 = ptr2->next; } } cout << ptr1->data << ": " << count << endl; count = 1; ptr1 = ptr1->next; } cout << endl; }

int main() { DCList* list = new DCList(); 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); delete list; DCList List; List.add_2Tail(5.5); List.add_2Tail(3.3); List.add_2Tail(3.3); List.Display(); List.countElements();

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

Step: 3

blur-text-image

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

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions