/* ============================================================ LIST_OF_PEOPLE ============================================================ */ #include #include #include #include #include "ListOfPeople.h" #define TRUE 1 #define FALSE 0 /* ============================================================ CONSTRUCTOR, DESTRUCTOR FOR LIST_OF_PEOPLE TYPES ============================================================ */ ListOfPeople :: ListOfPeople () { _head = NULL ; _tail = NULL ; } ListOfPeople :: ListOfPeople ( const ListOfPeople& p ) { _head = NULL ; _tail = NULL ; Person *c = p.head() ; while ( c != NULL ) { append ( c ) ; c = c -> next() ; } } ListOfPeople :: ~ListOfPeople () { // Delete all elements of the list Person *p = _head ; while ( p != NULL ) { Person *p1 = p -> next() ; delete p ; p = p1 ; } // Make sure that no list shows _head = NULL ; _tail = NULL ; } /* ============================================================ OPERATOR= ============================================================ */ ListOfPeople& ListOfPeople :: operator= ( const ListOfPeople& list ) { if ( &list == this ) return ( *this ) ; Person *p, *p1 ; p = _head ; // Delete the existing list elements (if any) while ( p != NULL ) { p1 = p -> next() ; delete p ; p = p1 ; } _head = NULL ; _tail = NULL ; Person c1 ; Person *c = list._head ; while ( c != NULL ) { append ( c ) ; c = c -> next() ; } return ( *this ) ; } /* ============================================================ PUSH ============================================================ */ void ListOfPeople :: push ( const Person& p ) { Person *node = new Person ( p ) ; node -> set_next( _head ) ; node -> set_previous ( _tail ) ; if ( _head != NULL ) _head -> set_previous ( node ) ; if ( _tail == NULL ) _tail = node ; _head = node ; } void ListOfPeople :: push ( Person* p ) { push ( *p ) ; } /* ============================================================ APPEND ============================================================ */ void ListOfPeople :: append ( const Person& p ) { Person *node = new Person ( p ) ; node -> set_previous ( _tail ) ; node -> set_next ( NULL ) ; if ( _tail != NULL ) { _tail -> set_next ( node ) ; } if ( _head == NULL ) { _head = node ; } _tail = node ; } void ListOfPeople :: append ( Person* p ) { append ( *p ) ; } /* ============================================================ EMPTY INQUIRY ============================================================ */ int ListOfPeople :: is_empty () const { if ( _head == NULL ) return ( TRUE ) ; else return ( FALSE ) ; } int ListOfPeople :: is_not_empty () const { if ( _head != NULL ) return ( TRUE ) ; else return ( FALSE ) ; } /* ============================================================= PRINT ============================================================= */ ostream& ListOfPeople :: output ( ostream& co ) const { Person *c ; if ( _head == NULL ) { co << "NONE" << endl ; return ( co ) ; } c = _head ; while ( c != NULL ) { co << *c << endl ; c = c -> next() ; } return ( co ) ; } ostream& operator<< ( ostream& co, const ListOfPeople& cc ) { return ( cc.output ( co ) ) ; } /* ============================================================ LENGTH ============================================================ */ int ListOfPeople :: length() const { Person *p = _head ; int n = 0 ; while ( p != NULL ) { n++ ; p = p -> next() ; } return ( n ) ; } /* ============================================================ WEIGHT ============================================================ */ double ListOfPeople :: weight() const { Person *p = _head ; double sum = 0 ; while ( p != NULL ) { sum += p -> weight() ; p = p -> next() ; } return ( sum ) ; } /* ============================================================ GET ============================================================ */ Person * ListOfPeople :: get () { Person *c = _head ; if ( c != NULL ) { _head = _head -> next() ; if ( _head == NULL ) _tail = NULL ; } return ( c ) ; } void ListOfPeople :: remove ( Person * p ) { if ( p == _head ) { _head = _head -> next() ; if ( _head != NULL ) _head -> set_previous ( NULL ) ; else _tail = NULL ; p -> set_next( NULL ) ; p -> set_previous ( NULL ) ; return ; } if ( p == _tail ) { _tail = _tail -> previous() ; if ( _tail != NULL ) _tail -> set_next( NULL ) ; else _head = NULL ; p -> set_next ( NULL ) ; p -> set_previous ( NULL ) ; return ; } p -> next() -> set_previous ( p -> previous() ) ; p -> previous() -> set_next ( p -> next() ) ; p -> set_next ( NULL ) ; p -> set_previous ( NULL ) ; } /* ============================================================ GET_FOR_FLOOR ============================================================ */ Person * ListOfPeople :: get_for_floor ( const int i ) { Person *p = _head ; while ( p != NULL ) { if ( p -> desired_floor() == i ) { remove ( p ) ; return ( p ) ; } p = p -> next() ; } return ( NULL ) ; } /* ============================================================ DECREMENT_STAY ============================================================ */ void ListOfPeople :: decrement_stay () { Person *p = _head ; while ( p != NULL ) { p -> decrement_stay() ; p = p -> next() ; } } /* ============================================================ DECREMENT_ENTRY_TIME ============================================================ */ void ListOfPeople :: decrement_entry_time () { Person *p = _head ; while ( p != NULL ) { p -> decrement_entry_time() ; p = p -> next() ; } }