#include #include "CPstring.h" // for cps 100e, fall 1996 // based on an assignment from cps 100e, fall 1995, // but declarations changed form single to doubly linked lists struct StudentNode { string first; string last; StudentNode * next; StudentNode * prev; StudentNode(const string & fname, const string & lname, StudentNode * after = NULL, StudentNode * before = NULL) { first = fname; last = lname; next = after; prev = before; } void Print() { cout << "\t" << first << " " << last << endl; } }; struct SectionNode { int section; SectionNode * next; StudentNode * list; SectionNode(int sec, SectionNode * link = NULL, StudentNode * sptr = NULL) { section = sec; next = link; list = sptr; } }; void EnterStudent(SectionNode * & current, int sec, const string & first, const string & last) // postcondition: creates a new SectionNode if sec is a new section number // enters the student in the list corresponding to sec { if (current != NULL) { if (current->section == sec) // add to front of list { StudentNode * header = current->list; header->next = new StudentNode(first,last,header->next,header); header->next->next->prev = header->next; } else { EnterStudent(current->next, sec, first, last); } } else { // add a new node with a header node current = new SectionNode(sec); current->list = new StudentNode("dummy","header"); current->list->next = new StudentNode(first,last,0,current->list); } } void CreateList(SectionNode * & head) // postcondition: creates a list of sections, each section list contains // a list of students in that section { string first, last; int section; while (cin >> section >> last >> first) { EnterStudent(head, section, first, last); } } void PrintSection(StudentNode * current) // precondition: current points to a list of students in a particular section. // postcondition: prints the list of the students in this section. { current->Print(); PrintSection(current->next); } void PrintList(SectionNode * head) // postcondition: prints each section number followed by a list of the students // in that section. { if (head != NULL) { cout << "Section " << head->section << endl; PrintSection(head->list->next); // skip header to print PrintList(head->next); } } StudentNode * FindStudent(SectionNode * secPtr, const string & first, const string & last) // postcondition: returns pointer to node containing first/last // returns NULL if not found in section { StudentNode * current = secPtr->list; while (current != 0) { if (current->first == first && current->last == last) { return current; } current = current->next; } return 0; } int WhichSection(SectionNode * head, const string & first, const string & last) // postcondition: returns the first section number the name appears in // returns 0 if the name is not in any section { cout << "WhichSection not implemented" << endl; return 0; } void FindAll(SectionNode * head, const string & first) // postcondition: prints all sections and full names of people with // first name == first { cout << "FindAll not implemented" << endl; } void Remove(SectionNode * head, const string & first, const string & last) // postcondition: removes (only first occurence) of a name found in list // pointed to by head { cout << "Remove not implemented" << endl; } main() // test all routines { SectionNode * head = NULL; cout << "Create list " << endl; CreateList(head); PrintList(head); cout << "Peter Teichman is in Section " << WhichSection(head,"PETER","TEICHMAN") << endl; cout << "Find all occurences of Matt" << endl; FindAll(head,"MATT"); cout << "Remove Ross Baker " << endl; Remove(head, "ROSS", "BAKER"); cout << "Remove Emma Wong" << endl; Remove(head,"Emma", "Wong"); // PrintList(head); }