Question: Help with this please C++. In Module 3, you were asked to design and implement a class to maintain student data in a linked list.
Help with this please C++.
In Module 3, you were asked to design and implement a class to maintain student data in a linked list. Because the search on a linked list is sequential and, therefore, can be time-consuming, design and implement a program so that this student data can be stored in a binary search tree.
Below is Module 3 code in C++.
#include #include #include using namespace std; class Student{ private: int id; string name; string phone; public: Student(); Student(int id, string name); Student(int id, string name, string phone); int getId(); void setName(string name); string getName(); void setPhone(string phone); string getPhone(); }; Student::Student(){ id = 0; name = ""; phone = ""; } Student::Student(int id1, string name1){ id = id1; name = name1; phone = ""; } Student::Student(int id1, string name1, string phone1){ id = id1; name = name1; phone = phone1; } int Student::getId(){ return id; } void Student::setName(string name1){ name = name1; } string Student::getName(){ return name; } void Student::setPhone(string phone1){ phone = phone1; } string Student::getPhone(){ return phone; } class LinkedList{ private: struct Node{ Student student; Node* next; Node(Student s, Node* n){ student = s; next = n; } }; Node* first; int size; public: LinkedList(); int getSize(); bool isEmpty(); void add(Student s); //add in sorted order of id void remove(int id); void print(); void save(string filename); bool contains(int id); }; LinkedList::LinkedList(){ first = nullptr; size = 0; } int LinkedList::getSize(){ return size; } bool LinkedList::isEmpty(){ return first == nullptr; } void LinkedList::add(Student s) //add in sorted order of id { if(contains(s.getId())) { cout << "Duplicate student id " << s.getId() << endl; return; } Node* n = new Node(s , nullptr); if(isEmpty()) first = n; else{ Node* curr = first; Node* prev = nullptr; //find a suitable place to insert in ascending order of id while(curr != nullptr){ if(s.getId() < curr->student.getId()) break; prev = curr; curr = curr->next; } n->next = curr; //insert as fisrt node? if(curr == first) { first = n; } else { prev->next = n; } } size++; } void LinkedList::remove(int id){ if(!contains(id)) { cout << id << " does not exist in list. Can't remove" << endl; return; } Node* curr = first; Node* prev = nullptr; while(curr != nullptr){ if(id == curr->student.getId()) break; prev = curr; curr = curr->next; } Node* temp = curr; if(curr == first) { first = first->next; } else { prev->next = curr->next; } delete temp; size--; } void LinkedList::print(){ Node* curr = first; cout << setw(10) << "Id" << setw(20) << "Name" << setw(15) << "Phone" << endl; while(curr != nullptr){ Student s = curr->student; cout << setw(10) << s.getId() << setw(20) << s.getName() << setw(15) << s.getPhone() << endl; curr = curr->next; } cout << endl; } void LinkedList::save(string filename){ ofstream out(filename.c_str()); if(out.fail()){ cout << "Could not open file " << filename << " for writing" << endl; return; } Node* curr = first; while(curr != nullptr){ Student s = curr->student; out << s.getId() << "," << s.getName() << "," << s.getPhone() << endl; curr = curr->next; } out.close(); cout << "list of students saved to file " << filename << endl; } bool LinkedList::contains(int id) { Node *curr = first; while(curr != nullptr){ if(curr->student.getId() == id) return true; curr = curr->next; } return false; } int main(){ int id; string name, phone; int choice = 0; LinkedList slist; while(choice != 4){ cout << "1. Add student" << endl; cout << "2. Delete student" << endl; cout << "3. Display all" << endl; cout << "4. Quit" << endl; cout << "Enter your choice: " ; cin >> choice; switch(choice) { case 1: cout << "Enter details for new student" << endl; cout << "Id: "; cin >> id; cout << "Name: "; cin >> name; cout << "Phone: " ; cin >> phone; slist.add(Student(id, name, phone)); break; case 2: cout << "Enter the student id to be removed: "; cin >> id; slist.remove(id); break; case 3: slist.print(); break; case 4: break; default: cout << "Invalid choice!" << endl; } } //EXTRA CREDIT string filename; cout << "Enter the filename to save the student list: "; cin >> filename; slist.save(filename); }