Okay, So I'm having a few issues here with deleting a node in a doubly linked list. The structures and every thing have really confused me at this point. I feel like there is something wrong here with the memory (memory leak), I think? Like the temporary nodes are the ones holding the items, and they're just getting replaced or something. Where I should actually be delete(); deleting the nodes. I guess at this point it's just confusion from looking at it wrong. Here is part of the program. The error should be contained within the function.
Before line 91 songList->firstElement=temp; you should make sure to delete the object pointed to by songList->firstElement.
Something is wrong about line 96-100. If songList is null accessing songList->firstElement is an error.
Line 111 temp2->previousNode=temp->nextNode; is incorrect because temp->nextNode is the same as temp2, so you get temp2->previousNode pointing back at *temp2. You should also don't forget to delete the object pointed to by deletedNode.
What would something like this look like corrected? I've tried fixing it based off of your comment but can't see to get anything to work with the function correctly. I'm not sure when to its save to delete the object and which object I'm actually deleting lol. Not to mention where the temp objects go.. Help would be fantastic
Then I don't see what your problem is. Peter87 already pointed out the problems in your code and told you how to fix it. Do you not know the exact syntax to do it?
Peter87 wrote:
Before line 91 songList->firstElement=temp; you should make sure to delete the object pointed to by songList->firstElement.
Add delete songList->firstElement; somewhere before line 91. (Just like line 12 in the example I gave you.)
Peter87 wrote:
Something is wrong about line 96-100. If songList is null accessing songList->firstElement is an error.
This states the problem clearly enough; you can't access songList->firstElement when songList==NULL. You can just throw an error like I showed in the example on line 5. (Somewhat related is the problem with line 86. You're returning an int when you're supposed to return something of type SongNode.)
Peter87 wrote:
Line 111 temp2->previousNode=temp->nextNode; is incorrect because temp->nextNode is the same as temp2, so you get temp2->previousNode pointing back at *temp2.
The correct code here would be temp2->previousNode=temp;.
Peter87 wrote:
You should also don't forget to delete the object pointed to by deletedNode.
Remember to use delete.
By the way, you forgot to make sure that index is less than the number of elements in your linked list.