Double Linked List - Removing Node

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.

Last edited on
describe your problem clearly notify the function in which you have any issue ...
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.
Last edited on
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
bump
As has been pointed out, you are not actually deleting anything. To delete (ie deallocate memory) you need to call the delete function.

http://en.cppreference.com/w/cpp/memory/new/operator_delete

Here is an example of deleting something:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
typename <class T>
T List::pop_front()
{
    if (head == nullptr)
        throw std::out_of_range("ERROR: List::pop_front() : List is empty.");
    else
    {
        T temp = head->value;
        if (head->next)
        {
            head = head->next;
            delete head->prev;
            head->prev = nullptr;
        }
        else
        {
            delete head;
            head = nullptr;
        }
        return temp;
    }
}
I can't figure it out for this particular case, I've seen code on how to delete a node.
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.
Last edited on
Topic archived. No new replies allowed.