69,382
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
struct student{
int age;
struct student *next;
};
void main()
{
void disp(struct student *head);
/*在这里删掉了一句*/
int age1;
struct student *head,*pnew,*tail;
head=(struct student *)malloc(sizeof(struct student));
if(head==NULL)
{
printf("error");
return;
}
head->next=NULL;
tail=head;
while(age1!=0)
{
printf("input age=");
scanf("%d",&age1); /*去掉这里面的\n*/
pnew=(struct student *)malloc(sizeof(struct student));
tail->next=pnew; /*这里写反了,应该是前面的next指向后面的节点*/
pnew->age=age1;
pnew->next=NULL;
tail=pnew;
}
disp(head);
}
void disp(struct student *head)
{
struct student *p=head->next;/*你并没有往第一个节点输入数据,而是存放指向有数据的第一个节点的指针*/
while(p!=NULL)
{
printf("%d ",p->age);
p=p->next;
}
}