// List.C // // Implements the methods of the Linked List implementation // of the List ADT //------------------------- // Construct an empty list. //------------------------ template List::List( ) { header = new ListNode; } //------------------------- // Copy constructor // note use of operator= //------------------------- template List::List( const List & rhs ) { header = new ListNode; *this = rhs; } //------------- // Destructor //------------------ template List::~List( ) { makeEmpty( ); delete header; } //---------------------------------- // isEmpty ( ) // Test if the list is logically empty. // Return true if empty, false, otherwise. //-------------------------------------- template bool List::isEmpty( ) const { return header->next == NULL; } //------------------------------- // makeEmpty ( ) // Make the list logically empty. //------------------------------- template void List::makeEmpty( ) { while( !isEmpty( ) ) remove( first( ).retrieve( ) ); } //--------------------------------------------- // zeroth() // Return an iterator representing the header node. //----------------------------------------------- template ListItr List::zeroth( ) const { return ListItr( header ); } //--------------------------------------------------------- // first() // Return an iterator representing the first node in the list. // This operation is valid for empty lists. //------------------------------------------------------------ template ListItr List::first( ) const { return ListItr( header->next ); } //------------------------ // insert (x, p) // Insert item x after p. //-------------------------- template void List::insert( const Object & x, const ListItr & p ) { if( p.current != NULL ) p.current->next = new ListNode( x, p.current->next ); } //------------------------------------------------- // find (Object x) // Return iterator corresponding to the first node containing an item x. // Iterator isPastEnd if item is not found. //------------------------------------------------------- template ListItr List::find( const Object & x ) const { ListNode *itr = header->next; // assumes 'short circut' of && while( itr != NULL && itr->element != x ) itr = itr->next; return ListItr( itr ); } //--------------------------- // findPrevious (Object x) // Return iterator prior to the first node containing an item x. // Needed by remove () //---------------------------- template ListItr List::findPrevious( const Object & x ) const { ListNode *itr = header; while( itr->next != NULL && itr->next->element != x ) itr = itr->next; return ListItr( itr ); } //---------------------------------------- // remove (Object x) // Remove the first occurrence of an item x. //--------------------------------------- template void List::remove( const Object & x ) { ListItr p = findPrevious( x ); if( p.current->next != NULL ) { ListNode *oldNode = p.current->next; p.current->next = p.current->next->next; // Bypass deleted node delete oldNode; } } //---------------------------------------------- // Assignment Operator // Deep copy of linked lists. // ---------------------------------------------- template const List & List::operator=( const List & rhs ) { if( this != &rhs ) { makeEmpty( ); ListItr ritr = rhs.first( ); ListItr itr = zeroth( ); for( ; !ritr.isPastEnd( ); ritr.advance( ), itr.advance( ) insert( ritr.retrieve( ), itr ); return *this; }