Linked lists and file i/o, and some other stuff
Hereīs the deal: Iīve been working on a project recently that heavily depends on linked lists and to be able to save and load them. What makes the matter more complex is the fact that I want to load the list in to an allready existing list, that is first to be deleted (I donīt really know too much about lists and how to optimize them). All of the code fragments below are parts of different functions.
This is how I delete the list and make it ready to be reloaded (probably filled with stuff that does not need to be there and perhaps even some real errors):
Node *tmp=head;
Node *tmp2;
if(tmp->nxt!=NULL)
{
while(1)
{
while(tmp->nxt!=NULL)
tmp=tmp->nxt;
tmp2=tmp;if(tmp2==first)goto enddelete;
tmp=first;
while(tmp->nxt!=tmp2)
tmp=tmp->nxt;
delete tmp2;
tmp->nxt=NULL;
}
}
enddelete:
delete head;
head=new Node;
head->nxt=NULL;
tmp=head;
This is what I use to load the list:
head=new Node;
head->nxt=NULL;
Node *tmp=head;
while(!level.eof())
{
level>>tmp->var1;level>>tmp->var2;
tmp->nxt=new Node;
tmp=tmp->nxt;
tmp->nxt=NULL;
}
And this is how I use the list (where i KNOW there are errors, just canīt figure out how to fix them):
Node *tmp=head;
temp=bignning;
while(tmp!=NULL)
{
while(tmp->var1!=temp->int1||temp->var!=temp->int2)
{
temp=temp->next;
}
/* here is quite a bit of code that is not relevant, I know it does what itīs
supposed to, so it doesnīt really matter. */
tmp=tmp->nxt;
}
temp, int1 and int2 are parts of another linked list, which I know to work. This entire sequense of code is used to get to the matching spot in the two linked lists.
Something in that last code fragment makes the .exe crash. Please help me fix this!
Any help will be welcomed, both help with the real problem and optimizing of code.
//Ninja
Iīve tried to modify my code to what was suggested,
but the program crashes when I start it.
It uses a linked list successfully once, but then it isnīt reseted to the start when next I use it, so I get a nasty error where the program forever tries in vain to raech a node it has allready past.
So how do I reset to the beginning of the list? I assumed from the example above that... ...did just that.
Am I wrong and thatīs whatīs wrong with my code?
Or am I right and thereīs something completely different thatīs wrong?