/* * Benjamin Bertka * bbertka@ucsc.edu * tnode.cpp * Implementation of tree-node (tnode) class * */ #include #include #include "tnode.h" using namespace std; /* * printChildern() * print the children of this node to the specified output stream */ void tnode::printChildren(ostream & out) { tnode **curr; for(curr = &(this)->firstChild; *curr != NULL; curr = &(*curr)->sib){ out << (*curr)->getName() << " "; } out << endl; } /* * addChild() * Add a child to this node with the given name. If the node already * has a child with this name, then do not add it. * return true if successful or false if the name already exists */ bool tnode::addChild(const string & childName) { tnode* newp; tnode **curr; if( childName.length() > 0 && childName != ".."){ if( !this->findNode(childName) ){ newp = new tnode(childName); newp->parent_p = this; }else{ return 0; } if( this->isEmpty() ){ this->firstChild = newp; }else{ for(curr = &(this)->firstChild; *curr != NULL; curr = &(*curr)->sib){ //just itors until find last child } *curr = newp; } } return 1; } /* * getChild() * Return a pointer to the child with the given name or NULL if no such * child exists.Warning: This allows pointers to nodes that might eventually be * deleted as a result of a remove to be saved outside of the tree sturcture. */ tnode* tnode::getChild(const string & childName) { tnode **curr; if( !this->isEmpty() ){ curr = &(this)->firstChild; if( (*curr)->getName() == childName){ return &(**curr); }else{ for( ; *curr != NULL; curr = &(*curr)->sib){ if( (*curr)->getName() == childName ) { return &(**curr); } } } } //cout << "Warning: " << childName << " does not exist!" < 0 && findNode(childName) ) { if( firstChild->getName() == childName){ curr = firstChild->sib; tmp = firstChild; firstChild = curr; delete tmp; return 1; } for( curr = firstChild; curr != NULL; curr = curr->sib){ prev = curr; if( curr->sib->getName() == childName ) { tmp = curr->sib; prev->sib = curr->sib->sib; delete tmp; return 1; } } } return 0; } /* * parent() * Return a pointer to the parent. Warning: This allows pointers to nodes * that might eventually be deleted as a result of a remove to be saved * outside of the tree sturcture. */ tnode* tnode::parent() { if(this->parent_p == NULL){ return this; } return this->parent_p; } /* * getName() * get the name of this node */ const string & tnode::getName() { return this->name; } /* * ~tnode() - Deconstructor * free this node and all of its children */ tnode::~tnode() { tnode *curr; for(curr = this->firstChild; curr != NULL; curr = curr->sib) { //just itors to the last child delete curr; } //cout << this->name << endl; } /* * findNode() - utility function * searches for node in current list */ int tnode::findNode(const string & param){ tnode **curr; string name = param; if( this->firstChild != NULL){ if( this->firstChild->getName() == name) { return 1; }else{ for( curr = &(this)->firstChild; *curr != NULL; curr = &(*curr)->sib) { if( (*curr)->getName() == param) { return 1; } } } }else{ return 0; } return 0; } /* * isEmpty() - utility function * determines if the current list / directory is empty */ int tnode::isEmpty(){ if( this->firstChild == NULL ){ return 1; } return 0; }