Section 5 - FloatList Source Code and Run
7A.5.1 The Complete Listing
Remember that this project is using multiple files. That means that the base classes Node and List are declared and implemented in the files List.h and List.cpp. The following is the main .cpp file (which I am still calling Foothill2.cpp). It contains the derived classes FloatNode and FloatList as well as the main().
#include <iostream> #include <string> using namespace std; #include "List.h" // FloatNode prototype -------------------------------- class FloatNode : public Node { private: float data; public: FloatNode(float x); void show(); bool setData(float userData); float getData(); }; // FloatList prototype -------------------------------- class FloatList : public List { public: void insert(float x); // insert in increasing numerical order bool remove(float f); // remove the first occurrence from list }; // main method --------------------------------------- int main() { FloatList myList; int k; float x; // build list of 10 floats, 2 at a time (1 random and 1 k*100) for (k = 0; k < 5; k++) { x = 1000 * (float)rand()/(float)RAND_MAX; // bet 0 and 1000 myList.insert(x); myList.insert(k*100); } // should be sorted myList.showList(); // remove 5 nodes (and delete them!) for (k = 0; k < 5; k++) { x = k*100; if (myList.remove(x)) cout << x << " removed\n"; else cout << x << " not found.\n"; } myList.showList(); if (!myList.remove (-10)) // should have no effect cout << " -10 not in list as expected. "; myList.showList(); return 0; } // FloatList method definitions -------------------------- void FloatList::insert(float x) { FloatNode *fp; // create a new FloatNode FloatNode *newfp = new FloatNode(x); // always stay one node behind the node you are testing for (resetCurrent(); getCurrent() ; iterate() ) { if (getNext() == NULL) break; // pointing to last node in list // if x > current then we continue to search fp = (FloatNode*)(getNext()); if ( x <= fp->getData() ) break; // found the exact place for this float } getCurrent()->insertAfter(newfp); } bool FloatList::remove(float f) { FloatNode *fp; // always stay one node behind the node you are testing for (resetCurrent(); getCurrent() ; iterate() ) { if (getNext() == NULL) return false; // end of list and not found // if f == current then we found match fp = (FloatNode*)(getNext()); if ( f == fp->getData() ) { Node *doneWithNode = getCurrent()->removeAfter(); delete doneWithNode; return true; // we found, we removed, we return } } return false; } // FloatNode method definitions -------------------------- FloatNode::FloatNode(float x) : data(x) { // nothing else needed } void FloatNode::show() { cout.setf(ios::fixed); cout.precision(3); cout << "[" << data << "] "; } bool FloatNode::setData(float userData) { // no filtering needed here, but in most cases, there would be data = userData; return true; } float FloatNode::getData() { return data; }
Here is the run:

The line
x = 1000 * (float)rand()/(float)RAND_MAX; // bet 0 and 1000
generates a pseudo random number between 0 and 1000. The standard library method rand() generates a random int between 0 and RAND_MAX. We use simple arithmetic to convert this to a random number between 0 and 1000. You can look up rand() on the Web for more information if you are interested.