i am having a problem with a program of self referential structures.here is the code
Code:
# include<stdio.h>
# include<conio.h>
# include<string.h>
typedef struct stud {
char*name;
struct stud*next;
}gos;
gos*temp;
gos*cur;
gos*prev;
gos*root;
gos*flag;
char shame[20];
void main()
{ int i,sz;
clrscr();
temp=(gos*)malloc(sizeof(gos));
root=temp;
cur=temp;
for(i=0;i<3;i++)
{
printf("enter the size of the name");
scanf("%d",&sz);
temp->name=(char*)malloc(sz*1);
printf("enter the name");
scanf("%s",temp->name);
cur=cur->next;
temp=cur;
}
temp=root;
cur=root;
printf("entered names are");
for(i=0;i<3;i++)
{
printf("\n %s \t %u \t %u",temp->name,temp,temp->next);
cur=cur->next;
temp=cur;
}
/*insertion*/
flag=(gos*)malloc(sizeof(gos));
printf("enter the size of the name");
scanf("%d",&sz);
flag->name=(char*)malloc(sz*1);
printf("enter the name");
scanf("%s",flag->name);
printf("enter the name before which to enter");
gets(shame);
cur=root;prev=root;
while((strcmp(shame,cur->name)!=0)&&(cur!=NULL))
{
prev=cur;
cur=cur->next;
}
if(!cur)
{
printf("cannot insert");
exit();
}
if(cur==root)
{
root=temp;
temp->next=cur;
}
else
{
temp->next=cur;
prev->next=temp;
}
getch();
}/*end of main*/
suppose i am giving the input for three passes
i,p,q;
the output coming is:
the entered names are
i 2840 0
q 0 0
q 0 0
i don't know why 0 has been assigned to temp->name,it also not diplaying p.I have done the program in two parts firstly i have only diplayed the three linked lists,then it worked fine.When i am trying to insert a new linked list,the problem arises, i am very much confused,plz help.
code tags added by Salem - use them in future