// This is a template for implementing an ordered set (list) // with elements being objects of class T and the set (list) // is ordered according to the value key() (of type Key) of // each element. // The class T does not contain a linking pointer for the // ordered set (list) structure. // // For example, to instantiate an ordered set (list) of items // of class PROF with a value of class NAME as the key, you // can simply declare // List prof_list; // It requires that class PROF must have the following // member functions: // Name key(); //return the key value of the object // int equal( Name somename); //key() == somename? // int less_than( Name somename ) //key() < somename? // void print() //print the object // // Then, the following functions are available for use: // void prof_list.add( Prof * ); // void prof_list.remove( Name ); // Prof * search( Name ); // void print_all(); // --------------------------------------------------------- // Author: S. Yu // Last modification: Feb. 6, 1997 #define FALSE 0 #define TRUE 1 #define null 0 // List item class // template class ListItem { public: T * object; ListItem * next; ListItem( T * obj=null, ListItem * p=null ) { object = obj; next = p; } }; // List class // template class List { ListItem * head; ListItem * look_for( Key ); public: List(); ~List(); void add( T * ); void remove( Key ); T * search( Key ); void print_all(); }; template //constructor List::List() { head = new ListItem; } template //destructor List::~List() { ListItem * current; ListItem * next; current = head; next = head->next; while (next != null) { delete current; current = next; next = next->next; } delete current; } // Given an object, find p such that p->next contains an object // equal to the given object or greater than the object // template ListItem * List::look_for( Key key) { int found; ListItem * p; p = head; found = FALSE; while ((p->next != null) && (found == FALSE)) { if ((p->next)->object->equal( key )) found = TRUE; //found the item else if ((p->next)->object->less_than( key )) p = p->next; //not found yet else found = TRUE; //found the place to add } return p; } //Given a pointer to an object, add the object to the list //in the order of the key //class T should have functions: Key key(); // template void List::add( T* object ) { ListItem * p; ListItem * new_p; p = look_for( object->key() ); if ((p->next != null) && ((p->next)->object)->equal( object->key() )) cerr << "Object exists. Addition is not done.\n"; else { new_p = new ListItem(object, p->next); p->next = new_p; } } // Given a key, delete the item that has the key // template void List::remove( Key key ) { ListItem * item; ListItem * old_item; item = look_for( key ); if ((item->next != null) && (item->next)->object->equal( key )) { old_item = item->next; item->next = old_item->next; delete old_item; } else cerr << "Object does not exist. Deletion is not done.\n"; } // Given a key, return the pointer to the object that has the key // template T * List::search( Key key ) { ListItem * item; item = look_for( key ); if ((item->next != null) && ((item->next)->object)->equal( key )) return item->next->object; else { // cerr << "Object does not exist.\n"; return null; } } template void List::print_all() { ListItem * p; p = head->next; while (p != null) { (p->object)->print(); cout << '\n'; p = p->next; } }