• Advertisement
Sign in to follow this  

Doubly-Linked List - Remove Function

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone! I was writing a doubly linked list and my program is crashing in this particular function. This is the code that is giving me problems. else if(index.prev == NULL && index.curr != NULL) { Node* temp = index.curr; index.curr = index.curr->pNext; index.curr->pPrev = index.prev; index.prev->pNext = index.curr; // point of interest delete temp; --m_nSize; return; } I'm pretty sure the reason it is crashing it because in this particular part of the code, index.prev is NULL, which means you can't access the next pointer? How i can rearrange my pointers if the previous is null? ANy help will be much appreciated! Sean M.

Share this post


Link to post
Share on other sites
Advertisement
Am I correct in assuming this is not a circularly-linked list? If so, the only time previous should ever be null is on the front node. If that's the case, then you need to set your head pointer to the second node, and set the previous pointer on the second node to null. Then delete, and be happy.

Share this post


Link to post
Share on other sites
This code only executes if index.prev is NULL, so it's hardly a surprise that the line you marked causes a crash. After all, you are dereferencing a NULL pointer...[smile]

It would help if you post the rest of the function (and any other code that's relevant), but I think that if you just remove that line, it should work.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement