//////////////////////////////// list.cpp ////////////////////////////////// #include "list.h" //------------------------------ Constructor --------------------------------- List::List() { head = NULL; } //------------------------------ isEmpty ------------------------------------- // check to see if List is empty bool List::isEmpty() const { return head == NULL; } //------------------------------- insert ------------------------------------- // insert an item into list; operator< of the NodeData class // has the responsibility for the sorting criteria bool List::insert(NodeData* dataptr) { Node* ptr= new Node; if (ptr == NULL) return false; // out of memory, bail ptr->data = dataptr; // link the node to data // if the list is empty or if the node should be inserted before // the first node of the list if (isEmpty() || *ptr->data < *head->data) { ptr->next = head; head = ptr; } // then check the rest of the list until we find where it belongs else { Node* current = head->next; // to walk list Node* previous = head; // to walk list, lags behind // walk until end of the list or found position to insert while (current != NULL && *current->data < *ptr->data) { previous = current; // walk to next node current = current->next; } // insert new node, link it in ptr->next = current; previous->next = ptr; } return true; } //------------------------------- buildList ---------------------------------- // continually insert new items into the list void List::buildList(ifstream& infile) { NodeData* ptr; bool successfulRead; // read good data bool success; // successfully insert for (;;) { ptr = new NodeData; successfulRead = ptr->setData(infile); // fill the NodeData object if (infile.eof()) { delete ptr; break; } // insert good data into the list, otherwise ignore it if (successfulRead) { success = insert(ptr); } else { delete ptr; } if (!success) break; } } //---------------------------------- << ------------------------------------ // output operator for class List, print data, // responsibility for output is left to object stored in the list ostream& operator<<(ostream& output, const List& thelist) { List::Node* current = thelist.head; while (current != NULL) { output << *current->data; current = current->next; } return output; // enables output << x << y; } //-------------------------- printBackwards ---------------------------------- // print the list in reverse order void List::printBackwards() const { printBackwardsHelper(head); } void List::printBackwardsHelper(Node* current) const { if (current != NULL) { printBackwardsHelper(current->next); cout << *current->data; } }