/* * This Software is the original work of Daniel TUNG * This Software is submitted in partial fulfillment of the * requirements for the degree of BCSE. * Dept. Computer Science, Monash University 2000 * * Copyright (c) 2000 beetung@cs.monash.edu.au * beetung@geocities.com */ #ifndef CCC_LLIST #define CCC_LLIST //template class LListHolder; ///// ///// ///// ///// template /////////////////////////////////////////// class LListElem { /////////////////////////////////////////// // friend class LListHolder; protected: public: ElemType& data; LListElem *next; int id; //---------------------- LListElem*& GetNext(void) { return next; } //---------------------- LListElem(int& i, ElemType& d) : data(d), next(0),id(i) { cout << "LListElem ctor() : " << d << endl; } ~LListElem() {;} }; ///// ///// My simple implementation of a LinkList, ///// couldve use the STL... hmmm.... ///// template /////////////////////////////////////////// class LListHolder { /////////////////////////////////////////// private: LListElem *head; LListElem *last; int nodeCount; public: LListHolder() : head(0), last(0),nodeCount(0) {;} ~LListHolder() { DeleteAll(); } //------------------------------------------------------------- int& getNodeCount (void) { return nodeCount; } LListElem* GetHead (void) { return head; } //------------------------------------------------------------- /// /// Append an element into this holder! /// //---------------------- void Append(ElemType *n){ //---------------------- if (last) { last->next = new LListElem(nodeCount,*n); last = last->next; } else { last = new LListElem(nodeCount,*n); head = last; } nodeCount++; } /// /// find edge /// //---------------------- void findEdge() { //---------------------- if (last ==0) return; LListElem *curr = head; LListElem *aNext; do { aNext = curr->next; // set next // cout << "Applying Func : " << curr->id << endl; curr->data.findEdge(); // delete this one curr = aNext; // increment current to next } while ( curr != 0 ); } //---------------------- void setBaseFrame(int* fb){ //---------------------- if (last !=0) { cout << " LListHolder::setBaseFrame() success" << endl; last->data.setBaseFrame( fb ); } else cout << " LListHolder::setBaseFrame() failed!!! " << endl; return; } /// /// /// //---------------------- void setFrameActive(int *fbp) { //---------------------- if (last !=0) { cout << " LListHolder::setFrameActive() success" << endl; last->data.setBaseFrame( fb ); } else cout << " LListHolder::setFrameActive() failed!!! " << endl; return; } /// /// draw /// //---------------------- void draw() { //---------------------- if (last ==0) return; LListElem *curr = head; LListElem *aNext; do { aNext = curr->next; // set next // cout << "Applying Func : " << curr->id << endl; curr->data.draw(); // delete this one curr = aNext; // increment current to next } while ( curr != 0 ); } /// /// Delete all Elements insider holder! /// //---------------------- void DeleteAll (void){ //---------------------- if (last ==0) return; LListElem *curr = head; LListElem *aNext; do { aNext = curr->next; // set next cout << " LListHolder delete: " << curr->id << endl; delete curr; // delete this one curr = aNext; // increment current to next } while ( curr != 0 ); } /// /// Show all elements insider holder /// //---------------------- void ShowList() { //---------------------- if (last ==0) return; LListElem *curr =head; LListElem *aNext; do { aNext = curr->next; // set next cout << "ID : " << curr->id << endl; curr = aNext; // increment current to next } while ( curr != 0 ); } /// /// find edge /// //---------------------- int AllCenterX() { //---------------------- if (last ==0) return 0; LListElem *curr = head; LListElem *aNext; int center = 0; int j=0; do { aNext = curr->next; // set next // cout << "Applying Func : " << curr->id << endl; int i=curr->data.getCenter(); if ( i != 0 ) { center += i; // delete this one j ++; } curr = aNext; // increment current to next } while ( curr != 0 ); if (j!=0) // we have at least 1 node, working return (center/j); else return 0; } }; #endif