#include <stdio.h>
#include <stdlib.h>
struct Node
{
int age;
struct Node *next;
};
struct Node *creat(int n)
{
struct Node *p,*pnew,*pold;
int i;
for(i=0;i<n;i++)
{
pnew=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&pnew->age);
pnew->next=NULL;
if(i==0)
p=pold=pnew;
else
{
pold->next=pnew;
pold=pnew;
}
return p;
}
}
int main()
{
struct Node *header=NULL;
header=creat(3);
printf("%d\n",header->age);
printf("%d\n",header->next->age);
printf("%d\n",header->next->next->age);
}
最后的大括号打错地方了
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int age;
struct Node *next;
};
struct Node *creat(int n)
{
struct Node *p,*pnew,*pold;
int i;
for(i=0;i<n;i++)
{
pnew=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&pnew->age);
pnew->next=NULL;
if(i==0)
p=pold=pnew;
else
{
pold->next=pnew;
pold=pnew; }
}
return p;
}
int main()
{
struct Node *header=NULL;
header=creat(3);
printf("%d\n",header->age);
printf("%d\n",header->next->age);
printf("%d\n",header->next->next->age);
}