Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Need to rewrite whole program by using Template, but I really don\'t have an

ID: 3574837 • Letter: C

Question

C++

Need to rewrite whole program by using Template, but I really don't have any idea where to start. So, please, give me some ideas or the answer.

Structure Student must be class, and I need to overload some operators.

StudentList.h

#ifndef STUDENTLIST_H
#define STUDENTLIST_H

struct Student
{
double gpa;
std::string name;
};

class StudentList
{
private:
// Declare a structure for the list
struct ListNode
{
Student stu; // The value in this node
ListNode *next; // To point to the next node
};
  
ListNode *head; // List head pointer
int count; // To keep track of the number of nodes in the list
  

public:
StudentList(); // Constructor
~StudentList(); // Destructor
  
// Linked list operations
int getCount() const {return count;};
void insertNode(Student);
bool deleteNode(double);
void displayList() const;
void searchList() const;

};
#endif

StudentList.cpp

#include <iostream> // For cout and NULL
#include "StudentList.h"
using namespace std;

//**************************************************
// Constructor
// This function allocates and initializes a sentinel node
// A sentinel (or dummy) node is an extra node added before the first data record.
// This convention simplifies and accelerates some list-manipulation algorithms,
// by making sure that all links can be safely dereferenced and that every list
// (even one that contains no data elements) always has a "first" node.
//**************************************************
StudentList::StudentList()
{
head = new ListNode; // head points to the sentinel node
  
head->stu.gpa = -1;
head->stu.name = "";
head->next = NULL;
count = 0;
}

//**************************************************
// displayList shows the value
// stored in each node of the linked list
// pointed to by head.
//**************************************************

void StudentList::displayList() const
{
ListNode *pCur; // To move through the list

// Position pCur: skip the head of the list.
pCur = head->next;

// While pCur points to a node, traverse the list.
while (pCur)
{
// Display the value in this node.
cout << pCur->stu.gpa << " " << pCur->stu.name << endl;

// Move to the next node.
pCur = pCur->next;
}
cout << endl;
}

//**************************************************
// The insertNode function inserts a node with
// stu copied to its value member.
//**************************************************
void StudentList::insertNode(Student dataIn)
{
ListNode *newNode; // A new node
ListNode *pCur; // To traverse the list
ListNode *pPre; // The previous node
  
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->stu = dataIn;

// Initialize pointers
pPre = head;
pCur = head->next;

// Find location: skip all nodes whose gpa is less than dataIn's gpa
while (pCur != NULL && pCur->stu.gpa < dataIn.gpa)
{
pPre = pCur;
pCur = pCur->next;
}

// Insert the new node between pPre and pCur
pPre->next = newNode;
newNode->next = pCur;
  
// Update the counter
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.
//**************************************************
bool StudentList::deleteNode(double target)
{
ListNode *pCur; // To traverse the list
ListNode *pPre; // To point to the previous node
bool deleted = false;
  
// Initialize pointers
pPre = head;
pCur = head->next;

// Find node containing the target: Skip all nodes whose gpa is less than the target
while (pCur != NULL && pCur->stu.gpa < target)
{
pPre = pCur;
pCur = pCur->next;
}
  
// If found, delte the node
if (pCur != NULL && pCur->stu.gpa == target)
{
pPre->next = pCur->next;
delete pCur;
deleted = true;
count--;
}
return deleted;

}

//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
StudentList::~StudentList()
{
ListNode *pCur; // To traverse the list
ListNode *pNext; // To point to the next node

// Position nodePtr at the head of the list.
pCur = head->next;

// While pCur is not at the end of the list...
while (pCur != NULL)
{
// Save a pointer to the next node.
pNext = pCur->next;

// Delete the current node.
cout << "DEBUG - Destructor: Now deleting " << pCur->stu.name << endl;
delete pCur;

// Position pCur at the next node.
pCur = pNext;
}
cout << "DEBUG - Destructor: Now deleting the sentinel node gpa = " << head->stu.gpa << endl;
delete head; // delete the sentinel node
}

main.cpp

#include "StudentList.h"
using namespace std;

int main()
{
// Define a NumberList object.
StudentList list;
Student s1 = {3.1, "Paul"};
Student s[10] =
{{2.5, "John"}, {3.9, "Linda"}, {3.6, "Bob"}, {2.7, "Ann"}, {4.0, "Mary"}, {3.2, "Andy"}};
Student s2 = {2.3, "Tom"};
  
cout << "TESTING INSERT ";
//Insert one value to the list.
cout << " Insert " << s1.gpa << " " << s1.name << endl;
list.insertNode(s1);
// Display the values in the list.
list.displayList();
cout << " This list has " << list.getCount() << " student[s] ";
  
//Insert more values to the list
for (int i = 0; i < 6; i++)
{
cout << " Insert " << s[i].gpa << " " << s[i].name << endl;
list.insertNode(s[i]);
// Display the values in the list.
list.displayList();
cout << " This list has " << list.getCount() << " student[s] ";
}
  
//Insert one value to the list.
cout << " Insert " << s2.gpa << " " << s2.name << endl;
list.insertNode(s2);
list.displayList();
int n = list.getCount();
cout << " This list has " << n << " student[s] ";
  
cout << "TESTING DELETE ";
// Delete the first node
cout << " Delete the first node ";
list.deleteNode(2.3);
list.displayList();
  
// Delete the last node
cout << " Delete the last node ";
list.deleteNode(4.0);
list.displayList();
  
// Delete a node in the middle
cout << " Delete 3.1 ";
if( list.deleteNode(3.1) )
cout << "Deleted ";
else
cout << "Target (3.1) not found ";
list.displayList();
  
// Try to delete an item not in the list
cout << " Try to delete 3.0 ";
if( list.deleteNode(3.0) )
cout << "Deleted ";
else
cout << "Target (3.0) not found ";
list.displayList();
  
cout << " This list has " << list.getCount() << " student[s] ";
  
return 0;
}

Explanation / Answer

operator+=

operator<<

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote