My code is about ordered insertion in linked list & I don't understand where the ambiguity lies. But when I run the program it SEEMS to be working fine. Is it the compiler's fault or mine? I am new in this so please help me out.
#include<stdio.h>
#include<stdlib.h>
struct node{
int i;
struct node *link;
};
int main()
{
struct node *head,*p,*temp,*q;
int data;
char c;
printf("Do you want to enter data? Y/N");
scanf(" %c",&c);
fflush(stdin);
if((c=='Y')||(c=='y'))
{
head=(struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf(" %d",&data);
head->i=data;
head->link=NULL;
}
printf("Do you wan to enter more data? Y/N");
scanf(" %c",&c);
fflush(stdin);
q=head;
while((c=='y')||(c=='Y'))
{
printf("Enter the data: ");
scanf(" %d",&data);
temp=(struct node *)malloc(sizeof(struct node));
temp->i=data;
temp->link=NULL;
if(q->i>=temp->i)
{
temp->link=q;
head=temp;
}
else
{
while((q->link!=NULL)&&(temp->i>q->link->i))
{
q=q->link;
}
temp->link=q->link;
q->link=temp;
}
q=head;
fflush(stdin);
printf("Do you want to enter more data? Y/N");
scanf(" %c",&c);
}
p=head;
while(p!=NULL)
{
printf("%d ",p->i);
p=p->link;
}
return 0;
}