Mail Archives: djgpp/1996/09/06/09:29:43
You original code should have been:
temp = head_of_list;
while (temp != NULL)
temp = temp->next;
If your linked list is empty, then temp will be NULL and temp->next could
be anything. If your linked list was empty, temp would be NULL and
temp->next would be garbage (most likely not NULL) and so the first while
loop test will be TRUE, even though the linked list is empty.
This is unlikely to be a bug with gcc. If have used gcc a fair bit with
linked lists and I never had any problems with it. I hope this clears the
matter up. If not e-mail me.
Your modified bit of code still does not cover the case where the linked
list is empty.
Your original code fails because you are setting temp to temp->next, and
then instead of testing to see whether temp is now NULL in the while
construct, you are testing to see whether temp->next is NULL. So in fact
you were testing (temp->next)->next.
You should modify your second bit of code to take into account an empty
list.
Martin
101641 DOT 317 AT compuserve DOT com
In article <199608140425 DOT OAA01240 AT gbrmpa DOT gov DOT au>,
Leath Muller <leathm AT gbrmpa DOT gov DOT au> wrote:
>I finally got my code to work, and I have to admit I think it's peculiar as
>to why...
>
>The origal code was something like:
>
> temp = head_of_list;
> while (temp->next != NULL)
> temp = temp->next;
>
>This died a horrible death, and yet when I changed the code to the following:
>
> temp = head_of_list;
> done = FALSE;
> while (!done)
> {
> if (temp->next == NULL)
> done = TRUE;
> else
> temp = temp->next;
> }
>
>It works fine...
>
>Is this a bug with gcc??? Or what??? :|
>
>Leathal.
- Raw text -