Question
Design your own linked list class to hold a series of students. Each students first name, last name, and grade are saved in a node.
Design your own linked list class to hold a series of students. Each students first name, last name, and grade are saved in a node. Assume the list is in ascending order by students grade.
Provide a constructor for the class. The constructor initializes an empty list. A member function that appends a node (i.e. a student with first name, last name, and grade) to the list. A member function that inserts a node in the list. A member function that displays all the students first name, last name, and grade in the linked list.
Then, provide a member function that deletes a student from the list, and provide a search function that searches a specific student in the list. The function returns the position of a specific student in the linked list. If the student is not in the list, prints out a message indicating that the student is not found.
Heres what I have so far:
#include
class StudentList { private: struct ListNode { string firstName; string lastName; double grade; ListNode *next; //points to the next node }; ListNode *head; public: StudentList(string, string, double); StudentList(); void appendNode(string, string, double); void insertNode(string, string, double); void displayList() const; void deleteNode (string, string); int search(string, string); };
StudentList :: StudentList(string a, string b, double num) { firstName = a; lastName = b; grade = num; } void StudentList::appendNode(string a, string b, double num) { ListNode *nodePtr; //head of the train ListNode *newNode; newNode = new ListNode; newNode -> value = num; newNode = num; newNode -> firstName = a; newNode -> lastName =b; if(!head) head = newNode; else { nodePtr = head; nodePtr -> next = nullptr; while(nodePtr -> next != nullptr) //while loop to check if its in the end { nodePtr = nodePtr -> next; } //if it doesnt equal nullptr, then go to the next node until we reach the end nodePtr -> next = newNode; } }
void StudentList :: insertNode(string a, string b, double num) { ListNode *previousNode; //cabbose ListNode *newNode; //passenger car ListNode *nodePtr; //head of the train while(nodePtr -> next != nullptr && nodePtr < num){ previousNode = nodePtr; nodePtr = nodePtr -> next; } //checks if node is the last in the list if(previousNode == nullptr){ head = newNode; head -> next = nodePtr; } else { previousNode->next = newNode; newNode ->next = nodePtr; } } void StudentList :: displayList() const { ListNode *nodePtr; nodePtr = head; while(nodePtr){ cout << firstName << " , " << lastName << " , " << grade <
//define some students StudentList a("Jesus", "Christ", 100); StudentList b("Donald" , "Hearing", 95); StudentList c("James" , "Jones", 50); //appends the nodes to the link list list.appendNode(a); list.appendNode(b); list.appendNode(c); //displays students in a list cout <<"Here are the students in the list: "; list.displayList(); cout << endl; //Inserting anohter student cout <<"Inserting another student into the list"; StudentList d("Rocky" , "Alms", 99); list.insertNode(d); //Display the students in a list cout << "Here are the updated nodes: "; list.displayList(); cout << endl;
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