/* * Katie Lewis * April 2004 * Note: In codeing this referred to code examples from * Professor Kuenning and Matt Gnaizda. */ #include "plantiterator.hh" #include "plant.hh" #include #include #include #include using namespace std; /* Constructor with no inputs */ PlantIterator::PlantIterator() : current(NULL), root(NULL), pending() { } /* Constructor w/ input */ PlantIterator::PlantIterator(Node* root_) : current(root_), root(root_), pending() { cout << "Enteriong iterator constructorI " << endl; if (root == NULL) { cout << "root case" << endl; return; } int L = root -> sizeLinksArray; if (L == 0) return; cout << "L: " << L << endl; for (int i = 0; i < L; ++i) { if (root-> links[i] != NULL) pending.push(root->links[i]); } } /* Constructor with a Plant as an input */ PlantIterator::PlantIterator(const Plant& plant) : current(plant.root), root(plant.root), pending() { int L = root -> sizeLinksArray; for (int i = 0; i < L; ++i) { if (root-> links[i] != NULL) pending.push(root->links[i]); } } /* Copy Constructor */ PlantIterator::PlantIterator(PlantIterator& iterator) : current(iterator.current), root(iterator.root) { while(!iterator.pending.empty()) { pending.push(iterator.pending.front()); iterator.pending.pop(); } } /* Destructor (Nothing to do) */ PlantIterator::~PlantIterator() { } /* Assignment operator */ PlantIterator& PlantIterator::operator=(PlantIterator& iterator) { current = iterator.current; root = iterator.root; while(!pending.empty()) { pending.pop(); } while(!iterator.pending.empty()) { pending.push(iterator.pending.front()); iterator.pending.pop(); } return *this; } /* True if there are more nodes left to be iterated over */ PlantIterator::operator bool() { return !pending.empty(); } /* Moves the pointer to the next node, thus allowing the iterator to cycle * through the plant. From content presented by Prof. Kuenning in cs70. */ PlantIterator& PlantIterator::operator++() { Node* currenty; assert(!pending.empty()); currenty = pending.front(); pending.pop(); if (!(currenty->isLeaf) && currenty->links != NULL) { int L = currenty -> sizeLinksArray; for (int i = 0; i < L; ++i) { if (currenty -> links[i] != NULL) pending.push(currenty->links[i]); } } current = currenty; return *this; } /* Returns the value (by reference) of the contents of the current node */ Node* PlantIterator::operator*() { return current; }