in C++ Please refer to the class NumberList defined in the files called Numberli
ID: 3811710 • Letter: I
Question
in C++ Please refer to the class NumberList defined in the files called Numberlist.h and Numberlist.cpp. In NumberList class, add a new data member called count to trace the total number of node you have in current linked list (you need to modify some member functions for adding count). Add a member function called getAverage to calculate the average of all the double values stored in each node in the current list. You also need to have a driver program to test your modified new class and new member function CODE-->>
// Specification file for the NumberList class
#ifndef NUMBERLIST_H
#define NUMBERLIST_H
class NumberList
{
private:
// Declare a structure for the list
struct ListNode
{
double value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
NumberList()
{ head = nullptr; }
// Destructor
~NumberList();
// Linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayList() const;
};
#endif
// Implementation file for the NumberList class
#include <iostream> // For cout
#include "NumberList.h"
using namespace std;
//*************************************...
// appendNode appends a node containing the *
// value pased into num, to the end of the list. *
//*************************************...
void NumberList::appendNode(double num)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
}
//*************************************...
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//*************************************...
void NumberList::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//*************************************...
// The insertNode function inserts a node with *
// num copied to its value member. *
//*************************************...
void NumberList::insertNode(double num)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = nullptr; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to nullptr.
previousNode = nullptr;
// Skip all nodes whose value is less than num.
while (nodePtr != nullptr && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//*************************************...
// The deleteNode function searches for a node *
// with num as its value. The node, if found, is *
// deleted from the list and from memory. *
//*************************************...
void NumberList::deleteNode(double num)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != nullptr && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
//*************************************...
// Destructor *
// This function deletes every node in the list. *
//*************************************...
NumberList::~NumberList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr is not at the end of the list...
while (nodePtr != nullptr)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
Explanation / Answer
Code:
NumberList.h
#ifndef NUMBERLIST_H
#define NUMBERLIST_H
class NumberList
{
private:
// Declare a structure for the list
struct ListNode
{
double value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
int node_count;
public:
// Constructor
NumberList()
{
head = nullptr;
node_count=0;
}
// Destructor
~NumberList();
// Linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayList() const;
double getAverage();
int getCount(); //returns node count in the list
};
#endif
NumberList.cpp
// Implementation file for the NumberList class
#include <iostream> // For cout
#include "NumberList.h"
using namespace std;
//*************************************...
// appendNode appends a node containing the *
// value pased into num, to the end of the list. *
//*************************************...
void NumberList::appendNode(double num)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
node_count++;
}
//*************************************...
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//*************************************...
void NumberList::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//*************************************...
// The insertNode function inserts a node with *
// num copied to its value member. *
//*************************************...
void NumberList::insertNode(double num)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = nullptr; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to nullptr.
previousNode = nullptr;
// Skip all nodes whose value is less than num.
while (nodePtr != nullptr && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
node_count++;
}
//*************************************...
// The deleteNode function searches for a node *
// with num as its value. The node, if found, is *
// deleted from the list and from memory. *
//*************************************...
void NumberList::deleteNode(double num)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != nullptr && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
node_count--;
}
//*************************************...
// Destructor *
// This function deletes every node in the list. *
//*************************************...
NumberList::~NumberList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr is not at the end of the list...
while (nodePtr != nullptr)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
double NumberList::getAverage(){
ListNode *tmp=head;
double sum=0;
while(tmp!=NULL)
{
sum+= tmp->value;
tmp=tmp->next;
}
return sum/node_count;
}
int NumberList::getCount()
{
return node_count;
}
main.cpp
#include<iostream>
using namespace std;
#include "NumberList.h"
int main(void)
{
NumberList lst;
for(int i=0;i<10;i++)
lst.appendNode(static_cast<double>(i));
cout<<"After appending 10 elements to the list, the list is:"<<endl;
lst.displayList();
cout<<"Inserting a new node with value 17:"<<endl;
lst.insertNode(17);
cout<<"After inserting 17:"<<endl;
lst.displayList();
cout<<"The total number of nodes in the list are:"<<lst.getCount()<<endl;
cout<<"deleting a node with value 9"<<endl;
lst.deleteNode(9);
lst.displayList();
cout<<"The average of "<<lst.getCount()<<" numbers in the list is:"<<lst.getAverage()<<endl;
}
Output:
After appending 10 elements to the list, the list is:
0
1
2
3
4
5
6
7
8
9
Inserting a new node with value 17:
After inserting 17:
0
1
2
3
4
5
6
7
8
9
17
The total number of nodes in the list are:11
deleting a node with value 9
0
1
2
3
4
5
6
7
8
17
The average of 10 numbers in the list is:5.3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.