Question
This is the code that was in the link #include #include #include using namespace std; class RosterNode { public: RosterNode(string nname=, double sc=0.0, RosterNode *link=NULL);
This is the code that was in the link
#include
#include
#include
using namespace std;
class RosterNode {
public:
RosterNode(string nname="", double sc=0.0, RosterNode *link=NULL);
RosterNode(const RosterNode& rost);
~RosterNode();
string Name() const { return nickname;}
double Score() const { return score;}
RosterNode* Next() const { return next;}
void setNext(RosterNode *link) {next = link; }
friend class LinkedList;
private:
string nickname; // nickname of the student
double score; // its final score
RosterNode* next;
};
class LinkedList {
public:
LinkedList(){head_ptr = NULL; }
void DisplayList() const;
double ComputeDisplayAverage() const;
void DisplayBelowAverage() const;
void InsertStudentAtHead(string nname, double sc);
void RemoveStudent();
private:
RosterNode *head_ptr;
};
RosterNode::RosterNode(string nname, double sc, RosterNode *link) // constructor with three parameters
{
nickname = nname;
score = sc;
next = link;
}
RosterNode::RosterNode(const RosterNode& rost) // copy constructor
{
nickname = rost.nickname;
score = rost.score;
next = rost.next;
}
RosterNode::~RosterNode() // destructor
{
next = NULL;
}
void LinkedList::DisplayList() const // display the content of the list,
// starting with the current element and following the pointer next
{
RosterNode *cursor;
for (cursor = head_ptr->Next(); cursor != NULL; cursor = cursor->Next())
cout Name() Score()
cout
}
double LinkedList::ComputeDisplayAverage() const // calculate and display the
// average of all scores in the list, starting with current element and
// following the pointer next
{
double avg = head_ptr->Score(); // calculate the average here, initially is
// the current score
int no = 0; // calculate how many scores are in the list, initially is 1
RosterNode *cursor;
// add the scores in the list until reaching NULL
for(cursor = head_ptr->Next(); cursor != NULL; cursor = cursor->Next()) {
avg += cursor->Score();
no++;
}
if (no == 0) return 0;
// calculate the average by dividing it by the number of elements
avg /= no;
// display the average
cout
return avg;
}
void LinkedList::DisplayBelowAverage() const // display all the nicknames in
// the list below the average starting with the current element and following
// the pointer next
{
double avg = ComputeDisplayAverage(); // call function member to
// compute and display average
int no = 0; // compute the number of students below average, initially 0
RosterNode *cursor;
// increment variable no until reaching NULL
for(cursor = head_ptr->Next(); cursor != NULL; cursor = cursor->Next()) {
if (cursor->Score()
no++;
}
cout
// display the students below average
for(cursor = head_ptr->Next(); cursor != NULL; cursor = cursor->Next()) {
if (cursor->Score()
cout Name()
}
cout
}
void LinkedList::InsertStudentAtHead(string nname, double sc)
// Insert a pair (nickname, score) in the list at the beginning of the list
// element by updating the parameter head_ptr->next
{
if (head_ptr == NULL) {
head_ptr = new RosterNode;
RosterNode *tmp = new RosterNode(nname, sc);
head_ptr->setNext(tmp);
}
else {
RosterNode *tmp = new RosterNode(nname, sc, head_ptr->Next());
head_ptr->setNext(tmp);
}
}
void LinkedList::RemoveStudent()
// Remove the node in the list that has the lowest score
// including if the student is stored at the head
{
RosterNode *cursor;
if (head_ptr == NULL) return;
if (head_ptr->Next() == NULL) {
delete head_ptr;
head_ptr = NULL;
return;
}
// identify the lowest score
double lowest = head_ptr->Next()->Score();
for (cursor = head_ptr->Next(); cursor != NULL; cursor = cursor->Next())
if (lowest > cursor->Score())
lowest = cursor->Score();
// identify the node with the lowest score
// check if the head is that student
if (head_ptr->Next()->Score()
// remove the head;
RosterNode *temp = head_ptr->Next();
head_ptr->next = temp->Next();
delete temp;
}
else {
// need to keep track of the node before the deletion point
for(cursor=head_ptr->Next(); cursor->Next()!=NULL; cursor=cursor->Next() )
if (cursor->Next()->Score()
RosterNode *temp = cursor->next;
cursor->setNext(temp->next);
delete temp;
}
}
#2 [5 points] To see the advantages of storing and processing data using a linked list over an array, write a mainO program that stores five pairs (nickname, score) as shown below in the Table 1, and performs the following operations: a) displays the linked list b) computes and displays the score average as The average score is xx c) displays how many values are below the average (smaller than the average) and the nicknames ofthe students below average, as There average score is xx.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