|
Need help with doubly linked lists
quote:
Original post by NerdJNerdBird
Wait, how can I post this code so that it spaces correctly? I tryed manually spacing it without tabs and it doesn''t work. Sorry, i''ve never done it before.
To preserve spacing wrap with [code&] [/code]
To put code in a highlighted block wrap with [source] [/source]
|
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Thank you! Now more details on my problem. If I shoot, like 10 times, the first 9 bullets delete correctly but the last bullet crashes my program. If I shoot 1 bullet when it is off the screen and supposed to be deleted it crashes. So if there is only 1 bullet in the linked list and it is trying to be deleted it crashes.
if (todel->next == NULL){todel->prev->next = NULL;
todel == todel->prev->next
you might be wiping the memory too soon
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
else if (todel-> next == NULL){
todel->prev->next = NULL;
Correct me if I''m wrong but isn''t that just like saying, "If it''s at the end of the list (signifyed by the ->next being NULL) then you cut it off the list (by setting the previous node''s next pointer to be NULL). Thats the way I see it but I could definitely be wrong since my program crashes.
It only crashes on the last bullet though, which should be the head. So i''m thinking its a problem with the first conditional.
todel->prev->next = NULL;
Correct me if I''m wrong but isn''t that just like saying, "If it''s at the end of the list (signifyed by the ->next being NULL) then you cut it off the list (by setting the previous node''s next pointer to be NULL). Thats the way I see it but I could definitely be wrong since my program crashes.
It only crashes on the last bullet though, which should be the head. So i''m thinking its a problem with the first conditional.
Here is my suggestion :
Hope that helps.
|
Hope that helps.
Hey darqtiger, your code works great! Thanks!
One thing I don't understand though is that for loop you have in the delete function. The end result is temp = todel right? Well if you delete that for loop and just do bullet *temp = todel; it crashes the program when you it trys to delete!
I think that is what is going wrong in my program maybe.
*update*
No, Thats not whats wrong. I have no idea! I tryed putting a forloop like you did but my program still crashes.
Edited by - NerdJNerdBird on March 2, 2002 11:10:27 AM
One thing I don't understand though is that for loop you have in the delete function. The end result is temp = todel right? Well if you delete that for loop and just do bullet *temp = todel; it crashes the program when you it trys to delete!
I think that is what is going wrong in my program maybe.
*update*
No, Thats not whats wrong. I have no idea! I tryed putting a forloop like you did but my program still crashes.
Edited by - NerdJNerdBird on March 2, 2002 11:10:27 AM
Well you are right that you don''t actually "need" the for loop.
I put that there out of force of habit. It basicly goes through the list to make sure that the node you want to delete is actually in the list.
If the node isn''t in the list then at the end of the list temp will be zero so all of the tests should fail and delete won''t actually get called on todel.
Are you only adding and deleting with those two functions? And do you only have one list?
I put that there out of force of habit. It basicly goes through the list to make sure that the node you want to delete is actually in the list.
If the node isn''t in the list then at the end of the list temp will be zero so all of the tests should fail and delete won''t actually get called on todel.
Are you only adding and deleting with those two functions? And do you only have one list?
Yes, those are the only 2 functions I have to deal with the linked list and right now that is the only linked list. I will have a linked list of enemies later in development though.
Actually, now its crashing even with your code. The first time it worked so I just thought it would work all the time but occasionally it will work with my code. So if you know your code is right and I know my code is right then perhaps it was another section of code?
|
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement