/* ------------------------------------------------------------------------- */ /* list1.cc */ /* implemen'n module for templated singly linked circular list class */ /* with global memory alloc'n */ /* ------------------------------------------------------------------------- */ #include "list1.h" // Print list to cout, template void List1 :: Print ( ) const { if ( Back ) { Node *temp = Back->Next; Node *nptr = temp->Next; cout << temp->Datum; while ( nptr != temp ) { cout << nptr->Datum; nptr = nptr->Next; } } return; } // end of Print( ) // delete nodes of list template void List1 :: MakeEmpty( ) { if ( Back) { Node *from = Back, *temp = Back, *to = Back->Next; delete temp; while ( to != Back ) { temp = to; to = to->Next; delete temp; } } return; } // end of MakeEmpty( ) // Member function push_front template void List1 :: push_front(const Dtype& X) { if ( !Back ) { Back = new Node( X ); if ( !Back ) { // check that new Node succeeded cerr << "Out of space in free store; exiting." << endl; exit(2); } Back->Next = Back; Count = 1; return; } Node *temp = Back->Next; Back->Next = new Node( X, temp); if ( !(Back->Next) ) { // check that new Node succeeded cerr << "Out of space in free store; exiting." << endl; exit(2); } Count++; return; } // end of push_front // Member function push_back template void List1 :: push_back(const Dtype& X) { if ( !Back ) { Back = new Node( X ); if ( !Back ) { // check that new Node succeeded cerr << "Out of space in free store; exiting." << endl; exit(2); } Back->Next = Back; Count = 1; return; } Node *temp = Back; Back = new Node(X, temp->Next); if (Back == NULL) { // check that new Node succeeded cerr << "Out of space in free store; exiting." << endl; exit(2); } temp->Next = Back; Count++; return; } // end of push_back // Member function pop_front // Deletes front item in list if list is non-empty template void List1 :: pop_front( ) { if ( Back ) { if ( Back == Back->Next ) { delete Back; Back = 0; Count = 0; return; } Node *temp = Back->Next; Back->Next = temp->Next; delete temp; Count--; } return; } // end of pop_front template Dtype& List1 :: front( ) const { if ( Back ) return Back->Next->Datum; else { cerr << "\nERROR -- front of empty queue; exiting." << endl; exit(3); } } // end of front template Dtype& List1 :: back( ) const { if ( Back ) return Back->Datum; else { cerr << "ERROR -- back of empty queue; exiting." << endl; exit(3); } } // end of back template int List1 :: IsEmpty( ) const { if ( Count ) return 0; else return 1; } // end of empty template int List1 :: Length( ) const { return Count; } // end of size // Reverse ( ) -- link inversion reversal in place template void List1 :: Reverse( ) { if ( !Back ) return; if ( Back == Back->Next ) return; Node *nfront = Back; Back = Back->Next; if ( Back->Next == nfront ) return; Node *from = Back; Node *to = Back->Next; Node *prev = nfront; while ( from != nfront ) { from->Next = prev; prev = from; from = to; to = to->Next; } nfront->Next = prev; return; } // end of Reverse( ) // Concat( ) -- concatenates two lists, in place template void List1 :: Concat( List1 & Y ) { if ( !Y.Back ) return; if ( !Back ) { Back = Y.Back; Count = Y.Count; Y.Back = 0; Y.Count = 0; return; } Node *temp = Y.Back->Next; Y.Back->Next = Back->Next; Back->Next = temp; Back = Y.Back; Count += Y.Count; Y.Back = 0; Y.Count = 0; return; } // end of Concat( )