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; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 <iostream>
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;
}

 

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education