/////////////////////////////////////////////////////////// // // list_string.cpp // // The implementation of a linked-list based // character string class. // // Revision history: // 1/3/00 (Jeff Ondich) Filled in a few functions. // and commented the rest. // // 1/8/03 (JO) Updated slightly. // /////////////////////////////////////////////////////////// #include "list_string.h" // Initializes the string to empty. ListString::ListString() { // Using 'Q' here (rather than, say, '\0') // makes it easier to spot one-off errors // involving the head node. mHead.mData = 'Q'; mHead.mNext = NULL; } // Initializes the string using the given // null-terminated string as initial data. ListString::ListString( const char *str ) { // See above. mHead.mData = 'Q'; mHead.mNext = NULL; // This code or a variant of it will be useful in operator=, too. ListStringNode *pCurrentNode = &mHead; for( int i=0; str[i] != '\0'; i++ ) { pCurrentNode->mNext = new ListStringNode; if( pCurrentNode->mNext == NULL ) { cerr << "Allocation error in ListString::ListString(const char *str), " << "str = \"" << str << "\"" << endl; } else { pCurrentNode = pCurrentNode->mNext; pCurrentNode->mNext = NULL; pCurrentNode->mData = str[i]; } } } // Copy constructor for ListString. // Initializes the string using the given // ListString string as initial data. ListString::ListString( const ListString& s ) { // Stub--init to empty. mHead.mData = 'Q'; mHead.mNext = NULL; } // Destructor for ListString. // Be careful here. It's very easy to dereference // a NULL pointer while de-allocating the nodes. ListString::~ListString() { // Stub. Very important to delete all memory. } // Returns the length, in characters (or, equivalently, // in nodes) of the ListString. Does *not* include the // head node. int ListString::Length() const { int length = 0; ListStringNode *pCurrentNode = mHead.mNext; while( pCurrentNode != NULL ) { length++; pCurrentNode = pCurrentNode->mNext; } return( length ); } // Changes all letters in the ListString to upper case. void ListString::MakeUpper() { // Stub. } // Changes all letters in the ListString to lower case. void ListString::MakeLower() { // Stub. } // Reverses the order of the non-head nodes in the // ListString. void ListString::Reverse() { // Stub. } // If the given character is contained in the // ListString, Find returns the first (0-based) index at which the // character appears. Otherwise, Find returns -1. int ListString::Find( char c ) const { // Stub. return( -1 ); } // If the given null-terminated string is contained in the // ListString, Find returns the first (0-based) index at which // str appears. Otherwise, Find returns -1. int ListString::Find( const char *str ) const { // Stub. return( -1 ); } // If the given null-terminated string is contained in the // ListString, Find returns the first (0-based) index at which // str appears. Otherwise, Find returns -1. int ListString::Find( const ListString& s ) const { // Stub. return( -1 ); } // If index is >= 0 and < length of the ListString, operator[] // returns a reference to the character at the given 0-based // index. Otherwise, operator[] returns a reference to the // character in the head node. char& ListString::operator[]( int index ) { // Stub. return( mHead.mData ); } // Copies the given ListString to the ListString. // ****This requires that you first delete the current // contents of the ListString, and then allocate new nodes // to hold the copy. (If you want to be clever, you // can reuse the old nodes with the new data.)**** ListString& ListString::operator=( const ListString& s ) { // Stub. return( *this ); } // Copies the given ListString to the ListString. // ****This requires that you first delete the current // contents of the ListString, and then allocate new nodes // to hold the copy. (If you want to be clever, you // can reuse the old nodes with the new data.)**** ListString& ListString::operator=( const char *str ) { // Stub. return( *this ); } // Prints the given ListString to the given ostream. // Because operator<< is declared to be a friend of // ListString, it is allowed access to ListString's private // data. ostream& operator<<( ostream& out, const ListString& s ) { ListStringNode *pCurrentNode = s.mHead.mNext; while( pCurrentNode != NULL ) { out << pCurrentNode->mData; pCurrentNode = pCurrentNode->mNext; } return( out ); } // Extracts a word (i.e. a maximal contiguous block of // non-space characters, where space characters are those // for which the return value of isspace is true) // from the given istream and puts it into the given // ListString. // // Because operator<< is declared to be a friend of // ListString, it is allowed access to ListString's private // data. istream& operator>>( istream& in, ListString& s ) { // Stub. return( in ); }