// FILE: 1ab3.cpp // PURPOSE: Linked list problem practice //--------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // COMMAND LINE: // g++ (or gxx) 1ab3.cpp //---------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Read question below and complete AddStudent() accordingly //---------------------------------------------------------------- #include #include #include /*-------------------------------------------------------------- This question involves the two structures declared below. The first one is used to store information about one student; the second one is used to implement linked lists of students. struct Student { Student(const string &="", int=0); string name; int age; }; struct ListNode { Student data; ListNode *next; }; Write function AddStudent as started below. Function AddStudent should add a new person to the given list, which is sorted in ascending order according to the ages of the students in the list. In particular, function AddStudent should create a list node whose data field is a student with the given name and age and should insert the new node into the linked list pointed to by L at the appropriate place. (If there is already a student with the given age, the new node can be inserted either before or after that node.) EXAMPLE 1: Value of L and its list Function call ----------------------- ------------------------ L--> NULL AddStudent(L, "Jean", 16) Resulting value of L and its list --------------------------------- ___________. |name: Jean| L--> |age: 16 |--> NULL +----------+ EXAMPLE 2: Value of L and its list Function call --------------------------------- -------------------- ___________. ___________. AddStudent(L, "Yve", 3) |name: Pat | |name: Jean| L--> |age: 14 |--> |age: 16 |--> NULL +----------+ +----------+ Resulting value of L and its list --------------------------------- ___________. ___________. ___________. |name: Yve | |name: Pat | |name: Jean| L--> |age: 3 |--> |age: 14 |--> |age: 16 |--> NULL +----------+ +----------+ +----------+ Complete function AddStudent below. Assume that it is only called with values that satisfy its precondition. void AddStudent(ListNode *&L, const string &name, int age) { // pre: L is NULL or it points to the first node of a linked list; // the list is sorted according to the ages of the students in the list --------------------------------------------------------------*/ struct Student { Student(const string &nm="", int ag=0) : name(nm), age(ag) {} string name; int age; }; struct ListNode { Student data; ListNode *next; }; void AddStudent(ListNode *&L, const string &name, int age) { } //*** AddStudent void print(ListNode *L, bool, Student); void driver1(ListNode *&); void driver2(ListNode *&); int main(void) { ListNode *P = NULL; driver1(P); driver2(P); return 0; } void print(ListNode *L, bool beforeAddStudent, Student std) { cout << "\n" << ( beforeAddStudent ? "BEFORE " : "AFTER " ) << "AddStudent(L, \"" << std.name << "\", " << std.age << "):" << "\n\nL--> "; if ( L != NULL ) { for (ListNode *L2 = L; L2 != NULL; L2 = L2->next ) { cout << "name: " << L2->data.name << setw(8 - L2->data.name.length()) << setiosflags(ios::left) << "-->"; } } cout << " NULL" << endl; if ( L == NULL ) return; cout << " "; for (ListNode *L2 = L; L2 != NULL; L2 = L2->next ) { cout << "age: " << setw(8) << L2->data.age; } cout << endl << endl; }//*** print void driver1(ListNode *&L) { string title("Linked List Problem: EXAMPLE 1"); cout << "\n\n\n\n\n\n" << setw(39 + title.length()/2) << title.c_str() << endl; print(L, true, Student("Jean", 16)); AddStudent(L, "Jean", 16); print(L, false, Student("Jean", 16)); cout << "\n\nPress : "; cin.ignore(10, '\n'); }//*** driver1 void driver2(ListNode *&L) { string title("Linked List Problem: EXAMPLE 2"); cout << "\n\n\n\n\n\n" << setw(39 + title.length()/2) << title.c_str() << endl; ListNode *L2 = new ListNode; L = L2; L2->data = Student("Pat", 14); L2->next = new ListNode; L2 = L2->next; L2->data = Student("Jean", 16); L2->next = NULL; print(L, true, Student("Yve", 3)); AddStudent(L, "Yve", 3); print(L, false, Student("Yve", 3)); cout << "\n\nPress : "; cin.ignore(10, '\n'); }//*** driver2