dpeace8
dpeace8

Reputation: 33

node from linklist is not delete at the end

My function is suppose to delete the last node in my Linked_list, Each node have an item number and a age. but when I call my function,and display my Linked_list, the function only sets the item number to zero, and the age remains the same when it is suppose to be deleted.

void deleteLastNode(Record  * & head){
Record *temp=head;

while(temp->next!=NULL){

    temp=temp->next;
}
    delete temp;


return;

};

//my output                                    item number  item age
//before the calling the function: 1st node    5000         1
//                                2nd node     6753         8 

//after calling the delete function: 1st node  5000         1
//                                   2nd node     0         8


//desired output                               
//before the calling the function: 1st node    5000         1
//                                 2nd node    6753         8 

//after calling the delete function: 1st node  5000         1

Upvotes: 1

Views: 46

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522254

I think you should walk down the list until reaching the second to last node, then point that node to NULL and delete the last node. I also think it makes sense to change your function to return a reference to the new head of the list.

Record* deleteLastNode(Record* & head) {
    Record* temp = head;

    // for an empty list, just return NULL
    if (head == NULL) return NULL;

    // for a list one just one element, delete the head, and return NULL
    if (temp->next == NULL) {
        delete temp;
        head = NULL;
        return head;
    }

    // otherwise walk down the list until reaching the second to last element
    while (temp->next->next != NULL) {
        temp = temp->next;
    }

    delete temp->next;
    temp->next = NULL;

    // return the head of list with its final node removed
    return head;
}

Upvotes: 3

amin saffar
amin saffar

Reputation: 2041

When you wan`t to delete node from linkedlist you should update parent of last node to not point to the last node.

Save prev Node in other pointer And when the loop terminate the next of prev = null to not point to deleted node. And finaly it's important to release memory of deleted node

Like here

void delete_last(node *head)
  {
    node *current;
    node *previous;
    current=head;
    while(current->next!=NULL)
    {
      previous=current;
      current=current->next; 
    }
    tail=previous; // if your Linked_list have tail
    previous->next=NULL;
    delete current;
  }

Upvotes: 0

TrustworthySystems
TrustworthySystems

Reputation: 777

I see two details here,

1) When you delete the last Record whose next property point to NULL, you have to set NULL to the previous Record next property

2) If you send a head without next property, you will attempt to delete it (don't know if is the desire behavior) but you pass it by reference so you need to assign NULL to head

Upvotes: 0

Related Questions