数据结构C

来源:百度知道 编辑:UC知道 时间:2024/05/16 12:46:06
#include <stdio.h>
#include <malloc.h>
typedef struct student
{
int data;
struct student *next;
}students,*student1;
void addlist(student1 head)
{
student *p;
int i,temp;
for(i=0;i<10;i++)
{
p=(students*)malloc(sizeof(student));
scanf("%d",&temp);
p->data=temp;
p->next=head->next;
head->next=p;
}
}

printlist(student1 head)
{
student1 p;
int i=0;
p=head->next;
while(p!=NULL)
{
i++;
printf("第%d个数据:",i);
printf("%d",head->data);
p=head->next;
}
}

main()
{
student *head;
head=(students*)malloc(sizeof(student));
head->next=NULL;
addlist(head);
printlist(head);
}
高手们帮我看下,他会无限循环,

没仔细看,但这样就不会死循环了
while(p!=NULL)
{
i++;
printf("第%d个数据:",i);
printf("%d",head->data);
p=p->next; //这里要修改
}
而且你的main 函数没有return 0,printlist()不想有反回值的话定义成void的