1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef struct node{ 4 int data; 5 struct node *next; 6 }Slist; 7 8 Slist *creatlist(Slist *L , int a [],int n) 9 { int i; 10 Slist *s; 11 L=(Slist *)malloc(sizeof(Slist)); 12 L->next=NULL; 13 for(i=0;i<n;i++) 14 { 15 s=(Slist *)malloc(sizeof(Slist)); 16 s->data=a[i]; 17 s->next=L->next; 18 L->next=s; 19 } 20 return L; 21 } 22 23 24 main() 25 { 26 Slist *L,*p; 27 int n,i; 28 L=(Slist *)malloc(sizeof(Slist)); 29 printf("请输入链表长度 "); 30 scanf("%d",&n); 31 int b[n]; 32 printf("请输入数%d个元素 ",n); 33 for(i=0;i<n;i++) 34 { 35 scanf("%d",b+i); 36 } 37 L=creatlist(L,b, n); 38 p=L->next; 39 printf("链表为: "); 40 for(i=0;i<n;i++){ 41 printf("%d ",p->data); 42 p=p->next; 43 } 44 }
想起一句话磨刀不误砍柴工