#include #include "scolor.hpp" using namespace std; struct WordListNode { string data; WordListNode *next; }; class WordList { WordListNode *start = 0; public: WordListNode* getstart(){ return start; } // WordList():start(0){} // If we couldn't give start an initial value above, we'd do this void add(string w){ // Will add in alphabetic order if(!start){ // could also be if(start != NULL) start = new WordListNode; start->data = w; // -> because start is a WordListNode*. If it was a WordListNode, we'd use . instead // start->data is the same as (*start).data start->next = 0; // Could also say start->next = NULL; return; } WordListNode *place = start, *last = 0; while(place && place->data < w){ last = place; place = place->next; } WordListNode *new_one = new WordListNode; new_one->data = w; // WordListNode new_one; // Don't do this! It'll make a new WordListNode in the stack frame for this method call! new_one->next = place; if(last) last->next = new_one; else start = new_one; } void print(){ WordListNode *place = start; cout << "["; while(place){ cout << place->data << ", "; place = place->next; } cout << "\b\b]\n"; } ~WordList(){ WordListNode *place = start, *last; while(place){ last = place; place = place->next; delete last; } } string operator[](size_t index) { WordListNode *place = start; while(index-- && place->next) // Returns the last item if there are less than index items place = place->next; return place->data; } operator string() { WordListNode *place = start; string retval = ""; retval += "["; while(place){ retval += place->data + ", "; place = place->next; } retval += "\b\b]"; return retval; } WordListNode* find(string tofind){ WordListNode* place = start; while(place){ if(place->data == tofind) return place; place = place->next; } return 0; } // friend ostream& operator << (ostream&, WordList&); }; /* ostream& operator << (ostream& out, WordList& wl){ WordListNode *place = wl.start; out << "["; while(place){ out << place->data << ", "; place = place->next; } out << "\b\b]\n"; return out; }*/ ostream& operator << (ostream& out, WordList& wl){ return out << (string)wl; } int main(){ WordList places; places.add("Orofino"); places.add("Lewiston"); places.add("Pullman"); places.add("Pullman"); places.add("Pullman"); places.add("Dworshak"); places.add("Behind the dumpster near the McDonalds"); places.add("MLH 310"); places.add("MLH 310"); places.add("MLH 310"); places.print(); /* This is ALL stuff we shouldn't do! // places.start = (WordListNode*)0; // This is an efficient way to reset places! WordListNode* the_start = places.getstart(); the_start->next = (WordListNode*)0; places.add("My house"); places.add("Your house"); *((size_t*)&places) = 0; places.add("The place my goat is right now"); */ cout << "Item 0: " << places[0] << endl; cout << "Item 3: " << places[3] << endl; cout << "Item 100: " << places[100] << endl; cout << GREEN((string)places) << endl; cout << RED("places = ") << BLUE(places) << endl; cout << places << endl; if(places.find("Pullman")){ cout << "We found Pullman in the list\n"; } if(places.find("Grangeville")){ cout << "We found Grangeville in the list\n"; } else cout << "We didn't find Grangeville\n"; return 0; }