// various questions from test 1 // Beth Katz - October 2009 #include #include #include "NodeD.h" using namespace std; // do code in question 8 void question8( ); // do code in question 4 void question4( ); // returns smallest value in v (or 0 if it is empty) template T mystery(const vector & v); // returns smallest value in list with head (or 0 if empty) template T mystery(NodeD * head); // removes every instance of target from the list and returns // how many were removed int removeEveryTarget(NodeD * & head, int target); // returns a pointer to tail of list template NodeD * getTail(NodeD * head); // fill the containers template void fill (vector & v, NodeD * & head); // print list with label and items separated by spaces template void printList(NodeD * head, const string & label); int main( ) { vector intV; NodeD * intList = NULL; cout << "The answer from question 8:" << endl; cout << "===========================" << endl; question8( ); cout << "===========================" << endl; cout << "You can't be sure about that last character." << endl; cout << "After 'delete p3;' p3 is a dangling reference" << endl; cout << "There isn't a memory leak." << endl; cout << "When p1 changed, it was pointing to b." << endl; cout << endl << "Output for question 4:" << endl; question4( ); cout << endl << "Now you can play with some integers (include 42)" << endl; cout << "Enter some integers. End with control-D." << endl; fill(intV, intList); cout << "The smallest value is " << mystery(intV) << " (vector)" << endl; cout << "The smallest value is " << mystery(intList) << " (list)" << endl; printList(intList, "Before removal"); removeEveryTarget(intList, 42); printList(intList, "After removing 42"); return 0; } template NodeD * getTail(NodeD * head) { NodeD * tail = head; if (tail == NULL) { return NULL; } else { while (tail->getNext( ) != NULL) { tail = tail->getNext( ); } return tail; } } template void fill (vector & v, NodeD * & head) { T value; NodeD * tail = getTail(head); while (cin >> value) { v.push_back(value); if (head == NULL) { head = new NodeD(value, NULL, NULL); tail = head; } else { // insert at end like push_back NodeD * newNode = new NodeD(value, tail, NULL); tail->setNext(newNode); tail = newNode; } } } void question4( ) { vector a(4); vector n(3); a[0] = "horse"; a[1] = "panda"; a[2] = "fox"; a[3] = "giraffe"; n[0] = 75; n[1] = 55; n[2] = 80; cout << "Result of mystery(n): " << mystery(n) << endl; cout << "Result of mystery(a): " << mystery(a) << endl; } void question8( ) { char a = 'y', b = '$', *p1 = &b, *p2 = new char('w'), *p3 = new char(*p1); cout << a << b << *p1 << *p2 << *p3 << endl; b = *p1; delete p3; p1 = p2; cout << a << b << *p1 << *p2 << *p3 << endl; } template T mystery(const vector & v) { if (v.size( ) <= 0) { return 0; } T value = v[0]; for (unsigned i = 1; i < v.size( ); i++) { if (v[i] < value) { value = v[i]; } } return value; } template T mystery(NodeD * head) { if (head == NULL) { return 0; } NodeD * current = head->getNext( ); T value = head->getData( ); while (current != NULL) { if (current->getData( ) < value) { value = current->getData( ); } current = current->getNext( ); } return value; } int removeEveryTarget(NodeD * & head, int target) { NodeD * current = head; int removedCount = 0; if (head == NULL) { return 0; } while (current != NULL) { if (current->getData( ) != target) { // just advance current = current->getNext( ); } else { // need to remove removedCount++; if (current == head) { // remove front node NodeD * removeMe = head; head = head->getNext( ); if (head != NULL) { head->setPrev(NULL); } current = head; delete removeMe; } else { // remove a middle node NodeD * removeMe = current; NodeD * previous = current->getPrev( ); NodeD * next = current->getNext( ); previous->setNext(next); if (next != NULL) { next->setPrev(previous); } delete removeMe; current = next; } } } return removedCount; } template void printList(NodeD * head, const string & label) { cout << label << ": "; while (head != NULL) { cout << head->getData( ) << " "; head = head->getNext( ); } cout << endl; }