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; }
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;
}
Step by step
Solved in 4 steps with 1 images