C++ removing node from back --singly linked list -


why won't code delete last element of linked list? create current pointer transverse through list , break out of loop..(next point within struct called card_node). should simple answer, not sure why won't delete last node in list"

    card_node *current;      current = front;     while ( current->next->next != null){     {            current = current-> next;     }        card = current->next->card;     return a;     delete current->next;     current->next = null; } 

return current->next->card;   // return !! delete current->next;         // never executed current->next = null; 

update

as comment below ask further input, here update tried keep original principles.

if (front == nullptr)  // special handling of empty list {     // nothing return - add error handling - throw exception perhaps     // or:     return ???; // default card perhaps } if (front->next == nullptr)  // special handling of list 1 element {     // 1 element     card = front->card;     delete front;     front = nullptr;     return a; }  card_node *current;  current = front; while ( current->next->next != null)  // iterate find last element {        current = current-> next; }  // current->next last element, i.e. 1 remove    card = current->next->card; delete current->next; current->next = null; return a; 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -