//************************************************************ // // STringList.C // //************************************************************ // // Copyright (C) 1996 // // J. Fritz Barnes // Graduate Student // Computer Science Department // University of California // Davis, CA 95616 // // // Permission is granted to use at your own risk and // distribute this software in source and binary forms // provided the above copyright notice and this paragraph // are preserved on all copies. This software is provided // "as is" with no express or implied warranty. // //************************************************************ #include "STringList.h" #include /* ============================================================ STringList - Constructors/Destructors ============================================================ */ STringList :: STringList( void ) { headtail = new STringListElem; headtail->elemValue = "End of List Sentinel"; headtail->next = headtail->prev = headtail; length = 0; } STringList :: STringList( const STring& s ) { headtail = new STringListElem; headtail->elemValue = "End of List Sentinel"; headtail->next = headtail->prev = headtail; length = 0; Append( s ); } STringList :: STringList( const STringList& sl ) { headtail = new STringListElem; headtail->elemValue = "End of List Sentinel"; headtail->next = headtail->prev = headtail; length = 0; STringListElem* ptr = sl.headtail->next; while ( ptr != sl.headtail ) { Append( ptr->elemValue ); ptr = ptr->next; } } STringList::~STringList( void ) { STringListElem* ptr = headtail->next; while ( ptr != headtail ) { STringListElem* delPtr = ptr; ptr = ptr->next; delete delPtr; } delete headtail; } /* ============================================================ STringList - AssignMent Operator ============================================================ */ STringList& STringList::operator = ( const STringList& sl ) { // First Delete the old list STringListElem* ptr = headtail->next; while ( ptr != headtail ) { STringListElem* delPtr = ptr; ptr = ptr->next; delete delPtr; } // Set at initial state length = 0; headtail->next = headtail->prev = headtail; // Next copy elements from sl ptr = sl.headtail->next; while ( ptr != sl.headtail ) { Append( ptr->elemValue ); ptr = ptr->next; } return *this; } /* ============================================================ STringList - Element Access Functions ============================================================ */ const STring& STringList::operator [] ( int index ) const { return Find_Element( index )->elemValue; } int STringList::Get_Length( void ) { return length; } /* ============================================================ STringList - Element Insertion & appending ============================================================ */ void STringList::Append( const STring& s ) { STringListElem* newPtr = new STringListElem; newPtr->elemValue = s; newPtr->next = headtail; newPtr->prev = headtail->prev; headtail->prev->next = newPtr; headtail->prev = newPtr; length++; } void STringList::Append( const STringList& sl ) { STringListElem* ptr = sl.headtail->next; while ( ptr != sl.headtail ) { Append( ptr->elemValue ); ptr = ptr->next; } } void STringList::Insert( const int i, const STring& s ) { STringListElem* ptr = Find_Element( i ); STringListElem* newPtr = new STringListElem; newPtr->elemValue = s; newPtr->next = ptr; newPtr->prev = ptr->prev; ptr->prev->next = newPtr; ptr->prev = newPtr; length++; } /* ============================================================ STringList - Element Replacement and Removal ============================================================ */ void STringList::Replace( const int i, const STring& s ) { STringListElem* ptr = Find_Element( i ); ptr->elemValue = s; } void STringList::Remove( const int i ) { STringListElem* ptr = Find_Element( i ); ptr->prev->next = ptr->next; ptr->next->prev = ptr->prev; delete ptr; length--; } /* ============================================================ STringList - Find_Element helper function, this lets us locate the ith element ============================================================ */ STringListElem* STringList::Find_Element( int index ) { // Validate index assert( index >= 0 && index < length ); // Use for loop to find the item desired STringListElem* ptr = headtail->next; for( int i=0; inext; } return ptr; } const STringListElem* STringList::Find_Element( int index ) const { // Validate index assert( index >= 0 && index < length ); STringListElem* ptr = headtail->next; for( int i=0; inext; } return ptr; } /* ============================================================ STringList - Output operator ============================================================ */ ostream& operator << ( ostream& os, const STringList& sl ) { STringListElem* ptr = sl.headtail->next; while ( ptr != sl.headtail ) { os << ptr->elemValue << endl; ptr = ptr->next; } return os; }