给你看这个程序吧:
# include<stdio.h>
# include<malloc.h>
struct list//结构体
{
int data;
struct list *next;
};
struct list *creat();//创建链表并初始化
void print(struct list *head);//打印链表
void main()
{
struct list *head;
head=creat();
return;
}
struct list *creat()
{
struct list *head,*p,*rear;
int x;
head=(struct list*)malloc(sizeof(struct list));
rear=head;
scanf("%d",&x);
puts("input the list end with '0':\n");
while(x)
{
p=(struct list*)malloc(sizeof(struct list));
p->data=x;
rear->next=p;
rear=p;
scanf("%d",&x);
}
rear->next=NULL;
puts("the list you input is: ");
print(head->next);
return head->next;
}
void print(struct list *head)
{
struct list *p;
p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
就是这个了。