Script started on Thu Nov 15 15:00:01 2001 pluto[DING!]> cat clistnd.h // clistnd.h // declaration for ConcordNode // this is a node in a linked list holding // concordance data #ifndef listnd_h #define listnd_h class ConcordNode { friend class ConcordList; // make List a friend public: ConcordNode(const char * wrd = "", int freq = 0); // constructor // defaults are null word at 0 frequency char * getWord() const; // return the character string // associated with the node int getFrequency() const; // return the frequency data in the node void setWord(const char *); // sets the word associated with the node void setFreq(const int); // sets the frequency data assocaited void incrementFreq(); // increments the frequency private: char word[25]; // a string of maximum 24 characters int frequency; // an integer >= 0 ConcordNode *nextPtr; // next node in the list }; #endif pluto[3:00pm]> cat clistnd.cc // clistnd.cc // definition of member functions // for class ConcordNode #include #include #include "clistnd.h" // Constructor -- defaults in header file // word defaults to blank, frequency to 0 ConcordNode::ConcordNode(const char * wrd, int freq) { setWord(wrd); setFreq(freq); // node's nextPtr always set to null nextPtr = 0; } // get functions return the data char * ConcordNode::getWord() const { char tempwrd[25]; strcpy(tempwrd, word); return tempwrd; } int ConcordNode::getFrequency() const { return frequency; } // set functions set private data // // note does test for string being too long -- but // this is not very satisfactory. // this function should return an int of 1 if // setting worked and 0 otherwise void ConcordNode::setWord(const char * inwrd) { if (strlen(inwrd) <= 24) strcpy(word,inwrd); else strcpy(word," "); } void ConcordNode::setFreq(const int infreq) { if (infreq >= 0) frequency = infreq; else frequency = 0; } void ConcordNode::incrementFreq() { // Please write this function! } pluto[3:00pm]> cat clist.h // clist.h // ConcordList class declaration #ifndef clist_h #define clist_h #include #include #include "clistnd.h" class ConcordList { public: ConcordList(); // constructor ~ConcordList(); // destructor void insertAtFront(const char*); void insertAtBack(const char*); int removeFromFront(); int removeFromBack(); int isEmpty() const; void print() const; private: ConcordNode *firstPtr; // pointer to first node ConcordNode *lastPtr; // pointer to last node // Utility function to allocate a new node ConcordNode *getNewNode(const char*); }; #endif pluto[3:00pm]> cat clist.cc // clist.cc // ConcordList member function definition -- unfinished version #include #include #include #include "clist.h" #include "clistnd.h" // Default constructor ConcordList::ConcordList() { firstPtr = lastPtr = 0; } // Destructor ConcordList::~ConcordList() { if (!isEmpty()) { // List is not empty cout << "Destroying nodes ... " << endl; ConcordNode *currentPtr = firstPtr, *tempPtr; while (currentPtr != 0) { // delete remaining nodes tempPtr = currentPtr; cout << tempPtr->word << endl; currentPtr = currentPtr->nextPtr; delete tempPtr; } } cout << "All nodes destroyed" << endl << endl; } // Insert a node at the front of the list void ConcordList::insertAtFront(const char* value) { // char tempval[25]; // strcpy(tempval, value); ConcordNode *newPtr = getNewNode(value); if (isEmpty()) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; } } // Insert a node at the back of the list void ConcordList::insertAtBack(const char* value) { ConcordNode *newPtr = getNewNode(value); if (isEmpty()) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; } } // Delete a node from the front of the list int ConcordList::removeFromFront() { cout << "removeFromFront -- firstPtr = " << firstPtr -> word << ", lastPtr = " << lastPtr -> word << endl; if (isEmpty()) // List is empty return 0; // delete unsuccessful else { ConcordNode *tempPtr = firstPtr; if (firstPtr == lastPtr) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; delete tempPtr; return 1; // delete successful } } // Delete a node from the back of the list int ConcordList::removeFromBack() { cout << "removeFromBack -- firstPtr = " << firstPtr -> word << ", lastPtr = " << lastPtr -> word << endl; if (isEmpty()) return 0; // delete unsuccessful else { ConcordNode *tempPtr = lastPtr; if (firstPtr == lastPtr) firstPtr = lastPtr = 0; else { ConcordNode *currentPtr = firstPtr; while (currentPtr->nextPtr != lastPtr) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } delete tempPtr; return 1; // delete successful } } // Is the List empty? int ConcordList::isEmpty() const { return firstPtr == 0; } // Return a pointer to a newly allocated node ConcordNode *ConcordList::getNewNode(const char* value) { char tempval[25]; strcpy(tempval, value); ConcordNode *ptr = new ConcordNode(tempval); assert(ptr != 0); return ptr; } // Display the contents of the List void ConcordList::print() const { if (isEmpty()) { cout << "The list is empty" << endl << endl; return; } ConcordNode *currentPtr = firstPtr; cout << "The list is: "; while (currentPtr != 0) { cout << currentPtr->word << ' '; currentPtr = currentPtr->nextPtr; } cout << endl << endl; } pluto[3:00pm]> cat clist-driver.cc // clist-driver.cc // ConcordList class test #include #include "clist.h" void instructions(); main() { ConcordList clist; cout << "Testing a List of char * values" << endl; int choice; char value[25]; instructions(); do { cout << "? "; cin >> choice; switch (choice) { case 1: cout << "Enter a word: "; cin >> value; clist.insertAtFront(value); clist.print(); break; case 2: cout << "Enter a word: "; cin >> value; clist.insertAtBack(value); clist.print(); break; case 3: if (clist.removeFromFront()) cout << "Removed first element from list" << endl; clist.print(); break; case 4: if (clist.removeFromBack()) cout << value << "Removed last element from list" << endl; clist.print(); break; } } while (choice != 5); cout << "End test of word List" << endl; return 0; } void instructions() { cout << "Enter one of the following:" << endl << " 1 to insert at beginning of list" << endl << " 2 to insert at end of list" << endl << " 3 to delete from beginning of list" << endl << " 4 to delete from end of list" << endl << " 5 to end list processing" << endl; } pluto[3:00pm]> CC clistnd.cc clist.cc clist-driver.cc clistnd.cc: clist.cc: clist-driver.cc: pluto[3:00pm]> a.out Testing a List of char * values Enter one of the following: 1 to insert at beginning of list 2 to insert at end of list 3 to delete from beginning of list 4 to delete from end of list 5 to end list processing ? 2 Enter a word: three The list is: three ? 1 Enter a word: two The list is: two three ? 1 Enter a word: one The list is: one two three ? 2 Enter a word: four The list is: one two three four ? 2 Enter a word: five The list is: one two three four five ? 3 removeFromFront -- firstPtr = one, lastPtr = five Removed first element from list The list is: two three four five ? 4 removeFromBack -- firstPtr = two, lastPtr = five fiveRemoved last element from list The list is: two three four ? 3 removeFromFront -- firstPtr = two, lastPtr = four Removed first element from list The list is: three four ? 4 removeFromBack -- firstPtr = three, lastPtr = four fiveRemoved last element from list The list is: three ? 1 Enter a word: zero The list is: zero three ? 2 Enter a word: six The list is: zero three six ? 5 End test of word List Destroying nodes ... zero three six All nodes destroyed