Answered You can hire a professional tutor to get the answer.

QUESTION

How do I delete the last node node of a linked list?

How do I delete the last node node of a linked list? This is the assignment http://voyager.deanza.edu/~bentley/cis22b/day19.html (Exercise #10 at the bottom of the page)

So far this is my function to delete the last node but it does not work.

bool List::remove_last()

{

  Node* ptr;

  Node* prev;

  if(top == NULL)

  {

    cout << "No contents in list" << endl;

  }

  else if(top -> next == NULL)

  {

    ptr = top;

    top = NULL;

    delete ptr;

  }

  else

  {

    ptr = top;

    while(ptr -> next != NULL)

    {

      prev = ptr;

      ptr = ptr -> next;

    }

    prev -> next = NULL;

    delete ptr;

  }

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question