一个编译出问题的链表
#include<stdio.h>#include<stdlib.h>
struct node
{
int num;
char name[20];
int age;
struct node *next;
};
void main()
{
struct node *creat();
void print();
struct node *head;
head=NULL;
head=creat(head);
print(head);
}
/*****************************************/
struct node *creat(struct node *head)
{
int n;
struct node *p1=NULL,*p2=NULL;
printf("please input a number:\n");
scanf("%d",&n);
for(;n>0;n--)
{
p1=(struct node *) malloc(sizeof(struct node));
printf("input num,name,age\n");
scanf("%d",p1->num);
gets(p1->name);
scanf("%d",p1->age);
p1->next=NULL;
if(head==NULL)
head=p1;
else
{
p2=p1;
p2->next=p1;
}
}
return head;
}
/*******************************************/
void print(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%d, %s, %d\n",temp->num,temp->name,temp->age);
temp=temp->next;
}
}
谢谢