///////////////////////////////////////////////////////////////// // LinkedList.C // // CMSC 341 - Fall 2001 Project 2 // // Kyu Bae xxx-xx-5868, Section 201 // // kyu_bae@yahoo.com / kbae1@umbc.edu // // Created : 20 September 2001 // // Current : 20 September 2001 // ///////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// // Default constructor: // Construct the list // Initialize the Header to NULL /////////////////////////////////////////////////////////////// template List::List( ) { header = new ListNode; } ////////////////////////////////////////////////////////////// // Default constructor: // Initialize header to NULL ////////////////////////////////////////////////////////////// template List::List( const List & rhs ) { header = new ListNode; *this = rhs; } //////////////////////////////////////////////////////// // Destructor: // calls makeEmpty() and delete header node /////////////////////////////////////////////////////// template List::~List( ) { makeEmpty( ); delete header; } //////////////////////////////////////////////////////////// // isEmpty(): // Test if the list is logically empty. // return true if empty, false otherwise. // checking if list has any node in the list and if not // return true or false // Return : boolean for empty ///////////////////////////////////////////////////////////// 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 item x after p. // Param x: Data class object // param p: ListItr object current position ////////////////////////////////////////////////////// template void List::insert( const Object & x, const ListItr & p ) { if( p.current->next == NULL ) { // for first element p.current->next = new ListNode( x, NULL, NULL ); p.current->prev = p.current->next; p.current->next->next = p.current->next; p.current->next->prev = p.current->next; return; } p.current->prev = new ListNode(x, p.current->next, p.current->next->prev); p.current->next->prev->next = p.current->prev; p.current->next->prev = p.current->prev; } ///////////////////////////////////////////////////////// // find(): // Return iterator corresponding to the first node // containing // an item x. // Iterator isPastEnd if item is not found. // Param x : Data class object // Return : object of ListItr ///////////////////////////////////////////////////////// template ListItr List::find( const Object & x ) const { ListNode *itr = header->next; while(itr!=NULL && itr->element!=x && itr->next!=header->next) itr = itr->next; if(itr->element != x) itr = NULL; return ListItr( itr ); } ///////////////////////////////////////////////////////// // findPrevious(): // Return iterator prior to the first node // containing an item x. // Param x : object of Data class // Return : object ListItr current position //////////////////////////////////////////////////////// template ListItr List::findPrevious( const Object & x ) const { ListNode *itr = header; while ( itr->next != NULL && itr->next->element !=x && itr != header->prev ) itr = itr->next; if(itr->next->element != x) itr = NULL; return ListItr( itr ); } ////////////////////////////////////////////////////////////////// // remove(): // Remove the first occurrence of an item x. // param x : object of Data class ///////////////////////////////////////////////////////////////// template void List::remove( const Object & x ) { ListItr p = find( x ); ListNode *oldNode; if(p.current != NULL) oldNode = p.current; else return; if(p.current == p.current->next) { delete oldNode; header->next = header->prev = NULL; } else { p.current->prev->next = p.current->next; p.current->next->prev = p.current->prev; if(header->next == p.current) header->next = p.current->next; if(header->prev == p.current) header->prev = p.current->prev; delete oldNode; } } ////////////////////////////////////////////////////////////// // operator=(): // Deep copy of linked lists. // Param rhs : assign its data members to rhs // Return : List object ///////////////////////////////////////////////////////////// template const List & List::operator=( const List & rhs ) { ListItr ritr = rhs.first( ); ListItr itr = zeroth( ); if( this != &rhs ) { makeEmpty( ); for( ; !ritr.isPastEnd(); ritr.advance(), itr.advance() ) { insert( ritr.retrieve( ), itr ); if(ritr.current->next == rhs.header->next) break; } } return *this; }