Reputation: 325
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
Upvotes: 4
Views: 2077
Reputation: 325
Now since I have actually understood....for those who are still stuck I am writing this....
When I use (thead!=NULL) the thead pointer actually points to each and every element in the list.When it reaches the last element,it still traverses to the next element which is NULL unlike (thead->next!=NULL) which stops at the last element of the linked list.
In case of print we need to print all the values in the list so we use while(thead!=NULL) as we need to print the last element too.For traversing we simply need to make the pointer point towards the last node so we can stop at it and not traverse till we reach the NULL pointer.
We cant dereference NULL and so there was an error.
Upvotes: 1
Reputation: 153498
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.
Upvotes: 0