Shaurya Shaurya -4 years ago 68
C Question

Difference between linked list traversal **while(thead != NULL)** and while(thead->next !=NULL)

Can anyone tell me what is the difference between while(thead != NULL) and while(thead->next !=NULL) because for traversing the list thead != NULL is not working while thead->next works.
According to my understanding head node is just a pointer to the starting node and not the the starting node itself.

See this if u have doubt.Here head just stores address.

//thead means temp head variable to store the address head points to.

This is the code for insertion.

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};
struct node *head;
void insert(int x)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
struct node * thead;
thead=head;
while(thead->next!=NULL)
{
thead=thead->next;
}
thead->next=temp;
}
}

void print()
{
struct node *temp;
temp=head;
while(temp!=NULL)
{

printf("%d",temp->data);
temp=temp->next;
}
}
int main()
{
head=NULL;
int i,n,x;
printf("enter number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter no");
scanf("%d",&x);
insert(x);
}
print();
}


If we replace thead ->next != NULL by thead !=NULL then dev c++ stops working.Vice versa happens in the printf for traversal ...

So can someone answer the difference between the above two?

Also,Is head node the first node which contains both data and address or does it just stores addresses like in the diagram above?

Also if the head node is only a pointer which stores address then how are we able access thead->next ?

And when is a pointer to a structure NULL?

Thanks

Answer Source

With print(), code does not need to remember the last node address after the loop

temp=head;
while(temp!=NULL) {
    printf("%d",temp->data);
    temp=temp->next;
}
// temp == NULL at this point and code does not care what the last node was.

With insert(), code does need to remember the last node address after the loop.

// First handle special case where the head of the list is NULL
// otherwise ....
while(thead->next!=NULL) {
    thead = thead->next;  
}
// temp->next == NULL at this point
// Code can use `thead` to access the last node's members.
thead->next = temp;

Is head node the first node which contains both data and address or does it just stores addresses like in the diagram above?

struct node *head is a pointer. When head != NULL, it pointer the the frist node which contains data and a next pointer.

if the head node is only a pointer which stores address then how are we able access thead->next ?

thead is initialized with head. By de-referencing thead, code has access to that node's members including .next.

when is a pointer to a structure NULL?

Question is unclear. A pointer to a struct is NULL when it has a value equal to NULL. This is usually as the end in the linked-list code employs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download