#include "IntQueue.h" int IntQueue::numberOfQueues = 0; IntQueue::IntQueue( ) : front( NULL ), back( NULL ), theSize( 0 ) { numberOfQueues++; } IntQueue::IntQueue( int x ) { front = back = new Node( x ); theSize = 1; numberOfQueues++; } IntQueue::IntQueue( const IntQueue & q ) : front( NULL ), back( NULL ), theSize( 0 ) { *this = q; numberOfQueues++; } IntQueue::~IntQueue( ) { clear( ); numberOfQueues--; } const IntQueue & IntQueue::operator= ( const IntQueue & rhs ) { if( this != &rhs ) { clear( ); for( Node *p = rhs.front; p != NULL; p = p->next ) enqueue( p->data ); } return *this; } bool IntQueue::isEmpty( ) const { return size( ) == 0; } int IntQueue::size( ) const { return theSize; } void IntQueue::clear( ) { while( !isEmpty( ) ) dequeue( ); } void IntQueue::enqueue( int x ) { if( isEmpty( ) ) front = back = new Node( x ); else back = back->next = new Node( x ); theSize++; } int IntQueue::dequeue( ) { int frontItem = getFront( ); Node *tmp = front; front = front->next; delete tmp; theSize--; return frontItem; } int IntQueue::getFront( ) const { if( isEmpty( ) ) throw UnderflowException( ); return front->data; } void IntQueue::print( ostream & out ) const { out << "[ "; for( Node *p = front; p != NULL; p = p->next ) out << p->data << " "; out << "]"; } bool IntQueue::operator== ( const IntQueue & rhs ) const { if( size( ) != rhs.size( ) ) return false; for( Node *p = front, *q = rhs.front; p != NULL; p = p->next, q = q->next ) if( p->data != q->data ) return false; return true; } bool IntQueue::operator== ( int rhs ) const { return size( ) == 1 && front->data == rhs; } int IntQueue::activeQueues( ) { return numberOfQueues; } IntQueue::Node::Node( int d, Node * n ) : data (d ), next( n ) { } ostream & operator<< ( ostream & out, const IntQueue & q ) { q.print( out ); return out; } bool operator==( int lhs, const IntQueue & rhs ) { return rhs == lhs; }