Bryan
Bryan

Reputation: 117

Linked List pop_back() function issues

List.H

void List::pop_back()
{
    if (size == 0)
        cout << "The list is empty and there is no node to pop off the back    of the list" << endl;
    else if (size == 1)
    {
        delete tail;
        head = tail = iterator = NULL;
    }
    else
    {
        NodeRef temp = tail;
        while (iterator->next != NULL)
            iterator = iterator->next;
        tail = iterator;
        delete temp;
        size--;
    }

}
void List::begin() //Set the iterator to the head of the list
{
    iterator = head;
}

void List::push_front(int data)             //Inserting a new node in the front of the list
{
    if (size == 0)                          //If there is no nodes in the list, execute the if statement
    {
        head = new Node(data);              //create a new node, and have head point to it
        tail = head;                        //have tail point to the new node also.

    }
    else                                    //If there are nodes in the list, execute the else statement
    {
        NodeRef newNode = new Node(data);   //create a new node
        newNode->next = head;               //have the next pointer point to the head of the next node.
        head = newNode;                     //have the head pointer point to the new node inserted at the beginning of the list
    }
    size++;                                 //Increment the size counter
}

void List::print()
{
    iterator = head;                        //Have iterator point to the head
    if (size == 0)
        cout << "There is nothing in the list" << endl;
    else
    {
        while (iterator!= NULL)
        {
            cout << iterator->data << endl;         //Display contents in node
            iterator = iterator->next;              //Move to the next node;
        }
    }
}

List.cpp

int main()
{
    List B;

    B.push_front(5);
    B.push_front(4);
    B.push_front(3);
    B.begin();

    B.pop_back();
    B.print();

    return 0;
}

So the issue I am having is that after the pop_back() function is called and I call the print() function. It has the last node popped but there is a junk number at the end of the list. I believe what is going on is that it is displaying the final node Next* which is an address. I know it has something to do with the else portion of the pop_back() and iterator->next causing it to point to an address.

Upvotes: 0

Views: 7964

Answers (1)

John Drouhard
John Drouhard

Reputation: 1259

You have two issues:

  1. The else condition of your pop_back() function - You iterate through your list to get to the end, and you correctly release the memory of the last node, but you don't make the next pointer of the new tail point to NULL. The new tail's next pointer is still pointing to some random piece of unallocated memory and is actually a bug.
  2. You aren't actually moving the tail pointer. Your inner while() loop just gets iterator to point to the same tail as before.

Try changing your else statement in pop_back() to:

iterator = head;
while (iterator->next->next != NULL) {
    iterator = iterator->next;
}
tail = iterator;
delete tail->next;
tail->next = NULL;
--size;

This code assumes the size of your list is at least 2 (the above if statements take care of 0 and 1, so this should be fine for this code sample).

Upvotes: 2

Related Questions