Skip to main content
GameDev.net gamedev.net
🔒 Locked

getting a pointer to a structure back from a recursive function...

Started by Codejoy Feb 25, 2003 at 5:45 PM 0 replies 150+ views
Original Post
Codejoy
Codejoy
So i have this nifty little recursive function thats traversion a list (searching it) once i find the element (a char*) that i want, I want to return the pointer to the structure that the element was found in. So i could do this u say: xmlBlock *SearchBlock(xmlBlock *pNode, char *elementKey) { if(strcmp(pNode->element, elementKey)==0) return pNode; //retBlock=&pNode else { if(pNode->childrenHead!=NULL) { return SearchBlock(pNode->childrenHead,elementKey); } while(pNode->nextBlock!=NULL) { pNode=pNode->nextBlock; return SearchBlock(pNode, elementKey); } } //return NULL; } This works for going straight down a list to the final child, but if the root has more than one child it wont find those for some reason... here is the usage of search block: void Append(xmlBlock *pNode) { if(list.blockHead ==NULL) { list.blockHead = pNode; pNode->prevBlock =NULL; } else { list.blockTail ->nextBlock = pNode; pNode->prevBlock=list.blockTail ; } list.blockTail =pNode; pNode->nextBlock = NULL; } void AppendChildOf(char *key, xmlBlock *childNode) { xmlBlock *returnBlock; returnBlock = SearchBlock(list.blockHead, key); if (returnBlock!=NULL) { AppendChild(returnBlock, childNode); } } void AppendChild(xmlBlock *pNode, xmlBlock *childNode) { if( pNode->childrenHead == NULL) { pNode->childrenHead = childNode; childNode->prevBlock = NULL; } else { pNode->childrenTail->nextBlock = childNode; childNode->prevBlock = pNode->childrenTail; } pNode->childrenTail = childNode; childNode->nextBlock = NULL; } struct xmlBlock { char *element; char *attribute; char *attributeVal; char *parentElement; char *data; xmlBlock *prevBlock; xmlBlock *nextBlock; xmlBlock *childrenHead; xmlBlock *childrenTail; }; struct childList { xmlBlock *blockHead; xmlBlock *blockTail; }; struct blockList { xmlBlock *blockHead; xmlBlock *blockTail; } Any ideas, I thought i could pass the Searchblock a reference to the pointer (in this case retBlock) and that way not have to use a return inside the recursive calls to SearchBlock..but alas I had trouble doing that so I used the returns, and I think the reason why its not searching all the way down to where I want is do to some stack stuff (u know the returns popping at inconvienient times). If this is overly simplistic and a stupid question im sorry, its just that I would of done it differently but I know what I need in the code, and what I need to do, but my list and tree prowess is lacking (out of practice) so if someone can just give me a helper here id be greatly appreciated. Thanks for any help here...I hope ive included all relevant code (probably not) and sorry for not using the little command to format the text as I do not know what command that is in this forum. Thanks for bearing with it... -Shane

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.