/************************************************************************** GDB Test File 2 - contains a few errors ***************************************************************************/ #include #include #include /************************** Class Declarations ***************/ class ListNode { string token; int timesFound; // Times that token has been found ListNode *next; // Next node in the list public: ListNode(string word) : token(word), timesFound(1), next(NULL) {} ListNode(string word, ListNode *next) : token(word), timesFound(1), next(next) {} ~ListNode() { if ( next != NULL ) delete next; } string getWord() { return token; } int getNumOccurrences() { return timesFound; } void incNumOccurrences() { timesFound++; } ListNode * getNext() { return next; } void setNext(ListNode * next) { ListNode::next = next; } }; class ListADT { ListNode *head; // Head of the list public: ListADT( char * fileName ); // Initialize the list from the specified file ~ListADT(); bool findWord( string word ); void searchInsert( string word ); void printList (); }; /**************************************************************************/ int main( int argc, char *argv[] ) { // Must have an input file specified on the command line. if ( argc != 2 ) { cerr << argv[0] << " input-file" << endl; exit(-1); } // if ListADT myList( argv[1] ); // List of words. cout << "\nThe linked list contains:" << endl; myList.printList(); } // main /************************** Class Implementations ***************************/ /************************************************************************** Builds a list of words contained in the specified input file. Input: name of the input file Output: builds a list where head contains the address of the first node Error: program ends with error message and exit code -2 if the file could not be opened for input. **************************************************************************/ ListADT::ListADT( char * fileName ) { head = (ListNode *) 1; // Initialize list to empty. string word; ifstream infile( fileName, ios::in ); // Open input file if ( !infile ) { cerr << "File " << fileName << " could not be opened for input." << endl; exit(-2); } // if while ( true ) { infile >> word; if ( infile.eof() ) break; searchInsert( word ); } // while } // ListADT::ListADT /************************************************************************** Determines if the specified word is in the list or not. Input: word to be searched for Output: flag indicating whether a word has been found or not. Errors: N/A **************************************************************************/ bool ListADT::findWord ( string word ) { bool foundWord = false; for ( ListNode *ptr = head; ptr != NULL && !foundWord; ptr = ptr->getNext() ) { if ( ptr->getWord() == word ) { foundWord = true; } // if } // for } // ListADT::findWord /************************************************************************** Searches the list for the word. If the word is in the list, the repetition count of the word is incremented. Otherwise, the word is added to the list. Input: Pointer to the start of the list - ListHead Pointer to the buffer which contains the word - Token Output: A list of words with their frequencies of occurrence Errors: If no space for new word, then print message and exit program **************************************************************************/ void ListADT::searchInsert ( string word ) { ListNode *newNode, *temp, *prev; int value; if ( head == NULL || head->getWord() > word ) { head = new ListNode( word, head ); if ( head = NULL ) { // Ran out of space. cerr << "ERROR: ran out of space\n"; exit(-3); } // if } else if ( head->getWord() == word ) { head->incNumOccurrences(); } else { newNode = new ListNode( word ); if ( newNode == NULL ) { // Ran out of space. cerr << "ERROR: ran out of space\n"; exit(-3); } // if prev = head; for ( temp = head->getNext(); temp != NULL; temp = temp->getNext() ) { if ( word == temp->getWord() ) { temp->incNumOccurrences(); break; } else if ( word < temp->getWord() ) { newNode->setNext( temp ); prev->setNext( newNode ); break; } // if prev = temp; } // for if ( temp == NULL && prev != NULL ) { // Fell off list prev->setNext( newNode ); } // if } // if } // ListADT::SearchInsert /************************************************************************** Prints the list with the word repetition counts. Input: N/A Output: The words and their frequencies of occurrence. **************************************************************************/ void ListADT::printList() { for ( ListNode *ptr = head; ptr != NULL ; ptr = ptr->getNext() ) { cout << "word: " << ptr->getWord() << " occurs " << ptr->getNumOccurrences() << " time(s)" << endl; } // for } // ListADT::printList /************************************************************************** Deletes the list. Input: N/A Output: The list has been freed. **************************************************************************/ ListADT::~ListADT () { if ( head != NULL ) { delete head; } // if } // ListADT::~ListADT