Arcath
Arcath

Reputation: 51

linked list print/delete function bombs program

I realize this might be a simple problem but I'm not catching it. My requirement is that I have to "4) Delete the linked list and print the list out again. (making sure its gone from memory)"

So I call the delete function and stepping through it, head should be set to NULL when the function finishes.

/*----------------------------------------------------------------------------
*  Function:  deleteList
*  Purpose:   delete a link list
*  Arguments: head - a pointer to the first node in the linked list
*  Returns:   N/A
------------------------------------------------------------------------------*/
void deleteList(node *head)
{
   struct node *temp;

   // Loop through the list, deleting one node at a time.
   while(head != NULL)
   {
      temp = head->next;
      delete(head);
      head = temp;
   }
}

So when I call the print function and send in head, it should be NULL and catch on the first if statement and return to main. But it is bombing out instead.

/*----------------------------------------------------------------------------
*  Function:  printList
*  Purpose:   prints a link list
*  Arguments: head - a pointer to the first node in the linked list
*  Returns:   N/A
------------------------------------------------------------------------------*/
void printList(node *head)
{
   if (head == NULL)
   {
       cout << "Empty List!\n";
      return;
   }

   struct node *temp;
   temp = head;

   // Loop through the list, printing one node at a time.
   while(temp->next != NULL)
   {
      cout << temp->next->element << endl;
      temp = temp->next;
   }
}

Now if I use the following in main it works fine. But I'd like to know what small thing im missing in the print function. Been banging my head on this for a while now so I'd thought I would step back and ask for a little guidance.

int main()
{
....
cout << "\n***************** DELETE LIST & PRINT ********************\n";
deleteList(head);

cout << head->element << endl; // This works and shows the list is empty.
//printList(head); // This bombs the program.
....
}

Thanks much.

Node is defined below:

struct node
{
   string   element;
   struct   node *next;
};

Declaring head in main:

struct node *head;

//creating head node.
if ((head=new(node)) == NULL)
{
   cout << "Error: Could not create head node.\n";
   return 1;
}
head->next = NULL;

Upvotes: 0

Views: 134

Answers (1)

davir
davir

Reputation: 942

When you use in main

deleteList(head);

A copy of the pointer "head", pointing to the same place, is passed as parameter. So, if you change the variable that "head" points to, e.g.:

delete(head);

this will be visible in main(). But when you update the pointer "head" itself, e.g.:

head = temp;

The only pointer updated is the one in the scope of the function, and not the one in main. So now your "head" pointer in main points to a deleted variable.

To solve the issue, you could return the new place that "head" should point to, like so:

node *deleteList(node *head)
{
   struct node *temp;

   // Loop through the list, deleting one node at a time.
   while(head != NULL)
   {
      temp = head->next;
      delete(head);
      head = temp;
   }
   return head;
}

And call it with

head = deleteList(head);

Upvotes: 3

Related Questions