首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

麻烦看一下错哪了,多谢

2012-04-06 
麻烦看一下哪里错了,谢谢#includestdio.h#includestdlib.hstruct student{int agestruct student *ne

麻烦看一下哪里错了,谢谢
#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;}} 

热点排行