Sign in to follow this  
Palidorflame

Doubly-Linked List - Remove Function

Recommended Posts

Palidorflame    122
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
gekko    478
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
Gage64    1235
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this