将一个数组的各元素插入到链表中,并以升序排列
1 #include <stdio.h> 2 #include <stdlib.h> 3 struct slist 4 { 5 int info; 6 struct slist *next; 7 }; 8 int a[8]={35,46,17,80,25,78,66,54}; 9 struct slist *head=NULL; 10 void inlist(struct slist*,int); 11 void outlist(); 12 void main() 13 { 14 int i; 15 struct slist *node; 16 for(i=0;i<8;i++){ 17 node=(struct slist*)malloc(sizeof(struct slist)); 18 inlist(node,a[i]); 19 } 20 outlist(); 21 return; 22 } 23 void inlist(struct slist *nd,int value) 24 { 25 nd->info=value; 26 if(head==NULL){ 27 head=nd; 28 head->next=NULL; 29 }else{ 30 struct slist *cp; 31 cp=head; 32 if(cp->info>value){ 33 head=nd; 34 nd->next=cp; 35 }else{ 36 while(cp->next!=NULL&&cp->next->info<value){ 37 cp=cp->next; 38 } 39 nd->next=cp->next; 40 cp->next=nd; 41 } 42 } 43 return ; 44 } 45 void outlist() 46 { 47 struct slist *cp=head; 48 while(cp!=NULL){ 49 printf("%d\n",cp->info); 50 cp=cp->next; 51 } 52 return ; 53 }