Delete the last node of a linked list

南楼画角 提交于 2019-12-04 21:44:20

You are deleting the one after the last node, which should be NULL anyways.

void deleteNode(Node* firstNode)
{
    //first check if firstNode is NULL or last node.
    if(firstNode == NULL)
        return;

    if(firstNode->next == NULL)
    {
        delete firstNode;
        firstNode = NULL;
        return;
    }

    Node* currNode = firstNode;
    while (currNode->next && currNode->next->next != NULL)
    {
        currNode = currNode->next;
    }
    delete currNode->next;
    currNode->next = NULL;
}

You need to consider a few things:

  • Any operation that could potentially affect the head pointer must provide a mechanism for returning the updated head pointer (an in/out param i.e. pointer to pointer or reference-of-pointer, or by function return result; I prefer the former).
  • Any pointer holding the address of the last node in the list, be it the head pointer or any other, must be set to NULL.

That said,

Pointer To Pointer

void deleteLastNode(Node** firstNode)
{
    while (*firstNode && (*firstNode)->next)
        firstNode = &(*firstNode)->next;
    free(*firstNode);   /**/
    *firstNode = NULL;  /**/
}

Note if the pointer passed in by-address is already NULL, both lines above marked with /**/ need not be executed, but are harmless none-the-less, as free()-ing NULL is supported per the standard as a no-op.

Called by passing the address of the head pointer, the content of which must be NULL if the list is empty.

Node *head = NULL;
//... code to populate the list.

deleteLastNode(&head);

Reference of Pointer

With C++, you can also pass the head pointer by reference, for example:

void deleteLastNode(Node*& head)
{
    Node **firstNode = &head;
    while (*firstNode && (*firstNode)->next)
        firstNode = &(*firstNode)->next;
    free(*firstNode);   /**/
    *firstNode = NULL;  /**/
}

Invoked as:

Node *head = NULL;
//... code to populate the list.

deleteLastNode(head);

Which mechanism you choose is up to you.

You are deleting one after the final node.

delete currNode;

Try this:

void deleteNode(Node *firstNode)
{
    Node* currNode = firstNode;
    Node* nextNode = firstNode->next;
    while(nextNode != NULL)
    {
        currNode = nextNode;
        nextNode = nextNode->next;
    }
    delete currNode;
}

The problem with your code is, at the last instance curNode points to the last node of the list. When you delete the curNode, you will no longer be left with node, ie, the data and next part. So you now you cannot access next part of curNode. This code will lead to runtime error:

currNode->next = NULL;

Instead try this:

If the list contains only one element, then firstNode points to NULL and the only node is deleted. If there are more than two items in your list, your have to iterate to the last second element, set its next value to NULL, and delete the last node with the help of a temp pointer.

void pop_back(struct node** firstNode)
{
    struct node* p = *firstNode;

    if(p == NULL) 
        cout << "List empty." << endl;

    else if(p->next == NULL){
        cout << "Element " << p-> data << " deleted." << endl;
        delete *firstNode;
        *firstNode = NULL;
    }

    else
    {
        while(p->next->next != NULL){
            p= p->next;
        }

    struct node* temp = p-> next;
    p->next = NULL;
    cout << "Element " << temp->data << " deleted." << endl;
    delete temp; 
    }
}
Khadija
void delBottom()
{
    clrscr();
    Node *nb=H;              //current
    Node *nn=(H->getNext()); //next
    if(NULL==H)
    {
        cout<<"List Empty";
    }
    else if(H==T)
    {
        delete H;  //H-Head
        H=T=NULL;
    }
    else
    {
        while(NULL!=(nn->getNext()))
        {
            nb=nn;
            nn=nn->getNext();
        }
        delete nn;
        nb->setNext(NULL);
        T=nb;  //T-Tail
    }
}

actually, you should write like this:

void deleteNode(Node* firstNode)
{
    Node* currNode = firstNode;
    while (currNode->next != NULL)
    {
     currNode = currNode->next;
    }
     ***delete currNode;
     currNode = NULL;***
 }

cause, currNode is the last node.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!