Advertisement

Can anyone help me out here? I'm having trouble with my link list, heh

Started by July 20, 2002 01:49 PM
2 comments, last by Balaam 22 years, 3 months ago
Basically it''s something to do with my findempty function but I''m not sure what. The textfile I''m reading from has only one line. I''ve tested that and it''s reading the line in correctly. In fact I''ve even moved my if TriggerNode == Null bit so it has to create the new class in the trigger node and that works fine. I intialize TriggerNode to NULL. So it shouldn''t be pointing to anything. For some reason though the else part is called in the AddNode function. I''m not sure why. It I comment out the middle of the AddNode my program runs as it should. Otherwise it gives me a big black screen and drops out. When I run the debugger I get what I would expect but with a busy cursor (windows cursor not mine) and it hangs in such a way that forces me to reboot. I''d be grateful if some could point out any obvious I''m doing wrong. Or anything I''m doing wrong at all really. Thanks void CTrigLoader::ReadInTriggers(char *path) { ifstream fin(path); while(!fin.eof()) { //read in one trigger line to temp fin >> Temp.name ; fin >> Temp.x >> Temp.y ; fin >> Temp.scriptpath; AddNode(Temp); // cout << Temp.name << Temp.x << Temp.y << Temp.scriptpath; } fin.close(); } void CTrigLoader::AddNode(CTrigLoader::TriggerInfo node) { CTrigger * temp = new CTrigger(node.scriptpath, p_map, node.x, node.y, p_animll); (*temp).next = TriggerNode; if (TriggerNode == NULL) { TriggerNode = new CTrigger(node.scriptpath, p_map, node.x, node.y, p_animll); } else { CTrigger * Empty = FindEmpty(TriggerNode); (*Empty).next = new CTrigger(node.scriptpath, p_map, node.x, node.y, p_animll); } } CTrigger * CTrigLoader::FindEmpty(CTrigger * node) { if ((*node).next == NULL) { return(node); } else { FindEmpty((*node).next); } return(NULL); }

I'm writing a book about How To Make An RPG, check it out! | Blog

I''m not sure of this answer because I''m a very beginner Hope I can help you anyway

Check this function:
1: if (TriggerNode == NULL)2: {3: TriggerNode = new CTrigger(node.scriptpath, p_map, node.x, 4: 5: node.y, p_animll);6: }7: else8: {9: CTrigger * Empty = FindEmpty(TriggerNode);10: (*Empty).next = new CTrigger(node.scriptpath, p_map, node.x, 11: node.y, p_animll);12: }13:14: }15: CTrigger * CTrigLoader::FindEmpty(CTrigger * node)16: {17: 18: if ((*node).next == NULL)19: {20: return(node);21: }22: else23: {*** 24:    FindEmpty((*node).next); // Isn''t this wrong? ***25: }26: return(NULL);27: } 

It''s a little modified, but it''s just to make it easier to read and explain. Check line 24, it will just run the function (it returns a node huh?), exit the if-statement and reach line 26 - it will return NULL! Correct this by chaning line 24 to

return FindEmpty(node->next);

Think this should solve the problem, hope I could help!

-----------------
No way! I wont add any stinky profile-signature!!
No way! I will never write a profile signature! :p
Advertisement
Thanks for the help. I had a little mess this morning and got it to compile with the following code:

void CTrigLoader::AddNode(CTrigLoader::TriggerInfo node)
{

if (TriggerNode == NULL)
{
TriggerNode = new CTrigger(node.scriptpath, p_map, node.x, node.y, p_animll);
}
else
{
CTrigger * temp = TriggerNode;
while ((*temp).next != NULL)
{
temp = (*temp).next;
}
(*temp).next = new CTrigger(node.scriptpath, p_map, node.x, node.y, p_animll);

CTrigger * TempPrev = temp;
temp = (*temp).next;
(*temp).prev = TempPrev;
}

}

I think the problem was with my shoddy recursion. It now displays at least the first item in the node to the screen. So I'm happy and can forge on.

Thank you for taking the time to have a look.
Cheers :D

[edited by - Balaam on July 21, 2002 6:35:00 AM]

I'm writing a book about How To Make An RPG, check it out! | Blog

Yeah, I think your recursion made the problem, that was the first that snapped my eye. Check your other functions too, you never know were a bug hides.

EDIT: Deleted some ugly ascii-art

[edited by - the Chef on July 23, 2002 11:54:17 AM]
No way! I will never write a profile signature! :p

This topic is closed to new replies.

Advertisement