#include #include #include "stlist.h" // ************************************************************* // // first member functions, and other functions // for StockNode struct // // ************************************************************* StockNode::StockNode() : stock(), next(0) { // fields initialized to default values (pointer is NULL } StockNode::StockNode(const string & symbol, char Exchange, double price, unsigned int volume, double volatility, const string & name) : stock(symbol, Exchange, price, volume, volatility, name), next(0) { } // ************************************************************* // // Stock List member fucntions come here // // ************************************************************* int STOCK_VECTOR_SIZE = 256; //# of entries in vector StockList::StockList() : myList(STOCK_VECTOR_SIZE,0), myCount(0), myBucket(0), myCurrent(0) // postcondition: list is empty, has room for plenty of stocks { // note: myList initialized to NULL pointers when constructed } StockList::~StockList() // postcondition: list is destroyed { int k; StockNode * current; // current node in linked list StockNode * save; // will be node after one to delete for (k=0; k< myList.Length();k++) { current = myList[k]; if (current) { do { save = current->next; // save next node delete current; current = save; } while (current); } } } void StockList::First() // postcondition: current item reset to beginning of list { myBucket = 0; if (myList[myBucket]!=0) { myCurrent = myList[myBucket]; } else { GetNextNonEmptyBucket(); } } void StockList::GetNextNonEmptyBucket() // precondition: myCurrent->next = 0 // postcondition: advance myBucket and myCurrent so that they // refer to the next item in the list or refer // past the end of the list if nothing accessible { // code below sets bucket and current beyond the list // so that access using iterator will fail for(myBucket++; myBucket < STOCK_VECTOR_SIZE; myBucket++) { if (myList[myBucket]) { myCurrent = myList[myBucket]; break; } } } void StockList::Next() // precondition: !IsDone // postcondition: current item is advanced { if (myCurrent && myCurrent->next) { myCurrent = myCurrent->next; } else { GetNextNonEmptyBucket(); } } bool StockList::IsDone() // postcondition: returns true if current item off end of list // else returns false (current item is accessible) { return (myBucket >= STOCK_VECTOR_SIZE); } Stock & StockList::Current() // precondition: !IsDone // postcondition: returns current item in list { // index out of range? if (myBucket < 0 || myBucket>=STOCK_VECTOR_SIZE || IsDone()) { cerr << "error, current item not accessible" << endl; exit(1); } return myCurrent->stock; } bool StockList::SetCurrent(const string & symbol) // postcondition: Sets current to the stock with symbol // returns true if stock found, else returns false // and sets current to an invalid entry { myBucket = symbol[0]; // bucket for symbol stock myCurrent= myList[myBucket]; // search this list while (myCurrent!=0) { if (myCurrent->stock.GetSymbol() == symbol) { return true; } myCurrent = myCurrent->next; } // setting current failed myBucket = STOCK_VECTOR_SIZE; return false; } int StockList::NumStocks() const // postcondition: returns count of # items in list { return myCount; } void StockList::Add(const string & symbol, char exchange, double price, unsigned int volume, double volatility, const string & name) // postcondition: add stock to list alphabetized { StockNode * newNode; // Node being created StockNode * current; // Current Node newNode = new StockNode(symbol, exchange, price, volume, volatility, name); int k = symbol[0]; // insert into correct linked list, first see if it belongs in front if (myList[k]==0 || symbol < myList[k]->stock.GetSymbol()) { newNode->next = myList[k]; myList[k] = newNode; } else // not in front, find location and link in alphabetical order { current = myList[k]; while (current && current->stock.GetSymbol() <= symbol) { current = current->next; } current->next = newNode; } myCount++; } void StockList::Delete(const string & symbol) // precondition: symbol is symbol of something on list // postcondition: item in list with id_number == id deleted, // size of list decreased by one { // add code here, remove line below cerr << "warning, Delete not implemented" << endl; } void StockList::Print() // postcondition: list of stocks printed { cout << "Symbol" << '\t' << "Exchange" << '\t' << "Last" << '\t' << "Change" << '\t' << "Volume" << '\t' << "Company Name" << endl; // will change code below to user iterator functions for(First(); ! IsDone(); Next()) { Current().Print(); } } void StockList::Print(const string & symbol) // postcondition: stock printed { if (SetCurrent(symbol)) { Current().Print(); } else { cout << symbol << " not found in database" << endl; } } void StockList::Print(char ch) // postcondition: print all stocks beginning with ch { StockNode * ptr = myList[ch]; while (ptr) { ptr->stock.Print(); ptr = ptr->next; } }