/*creat a list*/ #include "stdlib.h" #include "stdio.h" //定义一个结点 struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; void main() {link ptr,head,s,pre; int num,i; head=(link)malloc(sizeof(node)); ptr=pre=head; printf("please input 5 numbers==>\n"); //从键盘上输入5个数据建立一个单链表 for(i=0;i<=4;i++) { scanf("%d",&num); ptr->data=num; s=(link)malloc(sizeof(node)); pre=ptr; ptr->next=s; ptr=s; } pre->next=NULL; //输出单链表的数据 ptr=head; while(ptr!=NULL) { printf("%4d",ptr->data); ptr=ptr->next; } return 0; }