17

I was reading about a few basic operations on linked list and I saw two types of loops being used predominantly


struct node {
   int data;
   struct node *next;
}*start=NULL,*tmp;

The first loop was of the form

for(tmp=start;tmp->next!=NULL;tmp=tmp->next);

Using the above loop, now the tmp pointer points towards the last node in the list

The second loop was of the form

tmp=start;
while(tmp!=NULL)
{
     // do something
}

I think that both of them do the same work, but I'm not sure. Is there any difference?

3
  • 1
    you're missing tmp=tmp->next; in the while loop Commented Jul 13, 2012 at 3:03
  • 4
    @Musa: // do something covers it. Commented Jul 13, 2012 at 3:11
  • Q: Is the "for loop" idiom equivalent to the corresponding "while loop"? A: Yes. Q: Is this example flawed because this particular "for() loop" happens to have a bug in it? A: Yes too :) Commented Jul 13, 2012 at 4:52

4 Answers 4

20

I suppose your while loop is something like this.

temp=start;
while(temp!=NULL)
{
     // do something
     temp= temp->next;
}

In your code of for loop, When you are out of the for loop, temp is not pointing to NULL. temp is pointing to end of the linked list. But in case of while loop, your temp is pointing to NULL after you exit the while loop and you don't have tail(Unless you assign temp to any other temporary variable to change the logic of program) with you if you want to use it in the further steps. That is the only difference. Except that there isn't much difference.

You could have checked it by writing a small program and printing the results. I recommend you do it.

Sign up to request clarification or add additional context in comments.

2 Comments

If I change the temp variable, will it change the original linked list?
@User No, temp is just a pointer variable. Changing its value does not affect the list.
8

The loops aren't identical. In fact, your for loop has a problem. Consider what happens when start==NULL before you enter the for loop.

for(tmp=start;tmp->next!=NULL;tmp=tmp->next);

You assign start to tmp and then dereference tmp, a NULL pointer. I think you want the following instead.

for(tmp=start;tmp!=NULL;tmp=tmp->next);

That change makes the for and while loops the same.

Comments

0

Q: Effectively, "no". There isn't any substantive difference; they both do the same work.

You can always code a "for()" loop with an equivalent "while()".

4 Comments

i Want to know that if at the end of the loop, the "tmp" pointer points at the last node in both cases ??
@CSSS As you've currently defined it, the only way for the while loop to terminate is when tmp == NULL. In contrast, the only way for the for loop to terminate is when tmp->next == NULL. So it seems like only the for loop has a chance at leaving tmp pointing to the last node. That said, every for loop has an equivalent while loop (and vice-versa), so you could make their behaviors the same.
The for loop exits when tmp->next!=NULL is false, so it exits when tmp->next is NULL, which is when tmp is pointing to the last node. The while loop exits when tmp!=NULL is false, so it exits when tmp is NULL, so tmp is not pointing to the last node (it is not pointing to anything). Note that the body of the for loop is never executed with tmp pointing to the last node, because that is when the loop exits.
To be clear this answer is incorrect. There is a major difference between the loops - see Blastfurnace's answer for details.
0

I use while loop when I need to change the linked list. For e.g.

while (root->next)
{
    if(0 == strcmp(root->data,root->next->data))
    {
        temp = root;
        root = root->next;
        free(temp)
    }
    else
    {
        root = root->next;
    }
}

I use for loop when I need a read only access to linked list.

1 Comment

Can you give some rationale for why you chose to do this? (Probably not since you're not an active user and I'm 6 years late to the party).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.