Thread: DeleteLast Function of a SinglyLinkedLast

  1. #1
    Registered User
    Join Date
    Aug 2009

    DeleteLast Function of a SinglyLinkedLast

    I can't figure out how to fix this. I'm trying to move the pointer all the way to the end of the list but it won't even run. It builds fine, no compiler errors, just doesn't run. It freezes execution.

    		void SinglyLinkedList::DeleteLast( ){
    			Node* newNode1 = new Node;
    			Node* newNode2 = new Node;
    			Node* deleteNode = new Node;
    			newNode1 = first;
    			if(Length() == 0)
    				cout << "List is empty." << endl;
    			else if(Length() == 1)
    				while(newNode1->next != NULL || newNode2->next != NULL){
    					newNode2->next = newNode1->next;
    					newNode1->next = newNode2->next;
    				if (newNode1 == NULL){
    					deleteNode = newNode1;
    					newNode2->next = NULL;
    					delete deleteNode;
    					deleteNode = newNode2;
    					newNode1->next = NULL;
    					delete deleteNode;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well you shouldn't be calling new at all, you're trying to delete a node, not create three more!

    Then it is
    while ( temp->next ) {
      prev = temp;
      temp = temp->next;
    // here, temp is the node to be deleted
    // prev is the node before it (you want to set prev->next to NULL)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM