// 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; }