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
thead==NULL
condition means in this scope? Whatthead->next==NULL
condition means in this scope? - ArturFHwhile(thead != NULL) { ... }
, than after the loop, what should get set totemp
? What needs to get set totemp
is some.next
member, but of what pointer? - chux - Reinstate Monica