Question: There are three files: StudentList.h StudentList.cpp main.cpp TheStudentListclass has aListNodestructure, and theListNodestructure has aStudentstructure. In other words, you work with an aggregate made of oneclass

There are three files:

  • StudentList.h
  • StudentList.cpp
  • main.cpp

TheStudentListclass has aListNodestructure, and theListNodestructure has aStudentstructure. In other words, you work with anaggregatemade of oneclass and two structs.

All but one of the basic linked list operations are given:

  • getCount()
  • insertNode()
  • deleteNode()
  • displayList()
  • searchList() // this is missing: you will be required to implement this function in another lab

In addition to reading and understanding the code, you are required to add another member function of theStudentListclass, that should be called as shown below:

list.displayList(2.8); 

The purpose of this function is to display the name and gpa for students whose gpa is greater than or equal to its argument.

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(std::string);

void displayList() const;

void searchList() const;

};

#endif

StudentList.cpp:

#include // For coutand 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 dataIn there.

newNode = new ListNode;

newNode->stu = dataIn;

// Initialize pointers

pPre = head;

pCur = head->next;

// Find location: skip all nodes whose name is less than dataIn's gpa

while (pCur != NULL && pCur->stu.name < dataIn.name)

{

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 target as its name value. The node, if found, is

// deleted from the list and from memory.

//**************************************************

bool StudentList::deleteNode(string 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 name is less than the target

while (pCur != NULL && pCur->stu.name < target)

{

pPre = pCur;

pCur = pCur->next;

}

// If found, delte the node

if (pCur != NULL && pCur->stu.name == 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:

/*

Student is a struct

This program demonstrates the insertNode, deleteNode, getCount(), and displayList member functions.

It builds and displays a sorted list

The list is sorted by name

Writea function that displays students with gpa >= a given gpa as show below

list.displayList(2.8);//<===== see line# 49

Run the program once and save the output as a comment at the end of this source file.

*/

#include

#include "StudentList.h"

using namespace std;

int main()

{

// Define a StudentList object.

StudentList list;

Student s[] = {{2.5, "Paul"}, {3.9, "Katie"}, {3.6, "Bill"}, {2.7, "Ann"}, {3.9, "Tina"}, {3.2, "Andy"}, {0, ""}};

//******************************

cout << "TESTING INSERT ";

// Build List from array (for demonstration purposes only)

for (int i = 0; s[i].name != ""; i++)

{

cout << "\tInsert " << s[i].gpa << " " << s[i].name << endl;

list.insertNode(s[i]);

// Display the values in the list.

list.displayList();

cout << "\t\tThis list has " << list.getCount() << " student[s] ";

}

//******************************

cout << "TESTING DISPLAY ";

list.displayList();

double gpa;

cout << "Enter gpa: ";

cin >> gpa;

cout << "List of students with gpa >= " << gpa << endl;

//list.displayList(gpa);//<====================================

//******************************

cout << "TESTING DELETE ";

string target[] = { "Andy", "Tina", "Katie", "Tim", ""}; // names to test deleteNode with

for (int i = 0; target[i] != ""; i++)

{

if( list.deleteNode(target[i]) )

cout << target[i] << " - Deleted! ";

else

cout << target[i] << " - Not found ";

}

list.displayList();

cout << "\t\tThis list has " << list.getCount() << " student[s] ";

return 0;

}

/***************************************************************

Save the OUTPUT below

*/

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!