读书人

麻烦看一下错哪了多谢

发布时间: 2012-04-06 12:22:24 作者: rapoo

麻烦看一下哪里错了,谢谢
#include<stdio.h>
#include<stdlib.h>
struct student{
int age;
struct student *next;
};

void main()
{
void disp(struct student *head);
struct student *list(){
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\n",&age1);
pnew=(struct student *)malloc(sizeof(struct student));
pnew=tail->next;
pnew->age=age1;
pnew->next=NULL;
tail=pnew;
}
}
disp(head);
}
void disp(struct student *head)
{
struct student *p=head;
while(p!=NULL)
{
printf("%d",p->age);
p=p->next;
}
}

[解决办法]

C/C++ code
#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;}} 

读书人网 >C语言

热点推荐