Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #define LINKEDLIST_H using namespace std;

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 < next; } } void StudentList :: deleteNode(string a, string b) { ListNode *nodePtr; //head of the train ListNode *previousNode; //caboose of the train //if the list is empty, do nothing if (!head){ return; } //checks if the node is first in the list if(previousNode == head){ nodePtr = head-> next; delete head; head = nodePtr; } else { nodePtr = head; } while (nodePtr != nullptr && nodePtr -> firstName != a && nodePtr -> lastName != b) { previousNode = nodePtr; nodePtr = nodePtr -> next; } //if nodePtr finds the name, //link the previous node to the node adter then delete the node if(nodePtr) { previousNode -> next = nodePtr -> next; delete nodePtr; } } int StudentList :: search (string a, string b) { ListNode *nodePtr = head; while (nodePtr -> next != nullptr && nodePtr -> firstName != a || nodePtr -> lastName !=b) { nodePtr = nodePtr -> next; } if(nodePtr -> firstName = a && nodePtr -> lastName = b) { return true; } else { return false; } } int main(){ //define the link list obj LinkedList list;

//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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions

Question

How can you defend against SQL injection attacks?

Answered: 1 week ago