0

I implement the following code to delete element at Head. When program runes to "delete p", it will impact the previous Head and Head gets to NULL. What happened?

    Node<T>* p;
    p = Head;
    Head = Head->next;
    delete p;
Bradley
  • 9
  • 1
  • 1
    If head was null you can't do `Head = Head->next;` You need to check first. – drescherjm Apr 30 '19 at 05:13
  • 1
    You may not have shown enough code to identify your real problem. Perhaps you called this in a function and passed `Head` by value and noticed that in the calling function `Head` was not updated. – drescherjm Apr 30 '19 at 05:14
  • 2
    Welcome to Stack Overflow. This code looks all right; maybe the problm is elsewhere. Without a [minimal complete example](https://stackoverflow.com/help/mcve) it's har to say more. – Beta Apr 30 '19 at 05:15
  • Yes. You are right. When I debug the code, Head = Head->next has succeeded. So, the next step "delete p" should not effect Head's data now. But When I check the Head data, I found it also changed and was set to Head "= 0x008f4a20 {data=-17891602 next=0xfeeefeee }" I cannot find the reason now. – Bradley Apr 30 '19 at 05:18
  • `0xfeeefeee` is an important code: https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – drescherjm Apr 30 '19 at 05:20
  • Perhaps you had some other undefined behavior in your code that caused `Head->next` to point to `Head`. – drescherjm Apr 30 '19 at 05:23
  • Please edit your question to add the additional code. – drescherjm Apr 30 '19 at 05:25
  • Thanks!I just found the reason. There is an error in the node's destructor ~node(). In it, I wrongly input "delete next"; it made an effect on the above code. So, when I delete p, because of the destructor, it influence Head->next. – Bradley Apr 30 '19 at 05:35

1 Answers1

1

This function will delete the 1st node (node pointed to by head). It returns true if the node was deleted else false if the list is empty.

Using double pointers for the input parameter (head) here because head will have to be updated and this value should reflect outside this function.

bool deleteHeadElement(Node** head)  
{
    if (*head == nullptr)
    {
        // List is empty, nothing to delete
        return false;
    }

    // Store the node that has to be deleted
    Node* nodeToDelete = *head;

    // Update the head to point to next nodeToDelete
    *head = nodeToDelete->next;

    delete nodeToDelete;

    // 1st element of node has been deleted.
    return true;
}

After the call to this function, you may subsequently call it and it will take care of the scenario in which the list becomes empty after the previous calls.

NOTE: Regarding the value 0xfeeefeee, it seems you are trying to somehow freeing already freed memory. Perhaps you should check if your head is properly getting updated.

Also, make sure that you are freeing memory of the node by using delete only if it was allocated using new. delete takes care if the memory to pointed is NULL.

If you had allocated memory to the node using malloc(), you should be deallocating using free().

Saket Sharad
  • 392
  • 2
  • 11