#include "LSet.h" template Set::Set( ) : first( NULL ), theSize( 0 ) { } template Set::Set( const Set & rhs ) : first( NULL ), theSize( 0 ) { *this = rhs; } template const Set & Set::operator= ( const Set & rhs ) { if( this != &rhs ) { makeEmpty( ); for( Node *p = rhs.first; p != NULL; p = p->next ) insert( p->data ); // note: order of items will change } return *this; } template Set::~Set( ) { makeEmpty( ); } template bool Set::isEmpty( ) const { return getSize( ) == 0; } template int Set::getSize( ) const { return theSize; } template void Set::print( ostream & out ) const { if( isEmpty( ) ) out << "Empty set"; else { out << "{ " << first->data; for( Node *p = first->next; p != NULL; p = p->next ) out << ", " << p->data; out << " }"; } } template bool Set::contains( const Object & x ) const { for( Node *p = first; p != NULL; p = p->next ) if( p->data == x ) return true; return false; } template void Set::insert( const Object & x ) { if( !contains( x ) ) { first = new Node( x, first ); theSize++; } } template void Set::remove( const Object & x ) { if( isEmpty( ) ) return; if( first->data == x ) { Node *old = first; first = first->next; delete old; theSize--; return; } for( Node *p = first; p->next != NULL; p = p->next ) if( p->next->data == x ) { Node *old = p->next; p->next = p->next->next; delete old; theSize--; return; } } template void Set::makeEmpty( ) { while( !isEmpty( ) ) remove( first->data ); } template Set Set::setUnion( const Set & rhs ) const { Set result = *this; for( Node *p = rhs.first; p != NULL; p = p->next ) result.insert( p->data ); return result; } template Set Set::intersect( const Set & rhs ) const { Set result; for( Node *p = first; p != NULL; p = p->next ) if( rhs.contains( p->data ) ) result.insert( p->data ); return result; }