#include "LinkedList.h" /** * Construct the list */ template List::List( ) { header = new ListNode; } /** * Copy constructor */ template List::List( const List & rhs ) { header = new ListNode; *this = rhs; } /** * Destructor */ template List::~List( ) { makeEmpty( ); delete header; } /** * Test if the list is logically empty. * return true if empty, false otherwise. */ template bool List::isEmpty( ) const { return header->next == NULL; } /** * Make the list logically empty. */ template void List::makeEmpty( ) { while( !isEmpty( ) ) remove( first( ).retrieve( ) ); } /** * Return an iterator representing the header node. */ template ListItr List::zeroth( ) const { return ListItr( header ); } /** * 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. */ template void List::insert( const Object & x, const ListItr & p ) { if( p.current != NULL ) p.current->next = new ListNode( x, p.current->next ); } /** * 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 { /* 1*/ ListNode *itr = header->next; /* 2*/ while( itr != NULL && itr->element != x ) /* 3*/ itr = itr->next; /* 4*/ return ListItr( itr ); } /** * Return iterator prior to the first node containing an item x. */ template ListItr List::findPrevious( const Object & x ) const { /* 1*/ ListNode *itr = header; /* 2*/ while( itr->next != NULL && itr->next->element != x ) /* 3*/ itr = itr->next; /* 4*/ return ListItr( itr ); } /** * 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; } } /** * Deep copy of linked lists. */ 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 ); } return *this; }