//函数fun功能是将带头节点的单向链表结点域中的数据从小到大排序。
//相当于数组的冒泡排序。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 6 4 typedef struct node { 5 int data; 6 struct node *next; 7 } NODE; 8 void fun(NODE *h) 9 { NODE *p, *q; int t; 10 /**********found**********/ 11 p = h->next;//头结点的指向赋值 12 while (p) { 13 /**********found**********/ 14 q = p->next ; 15 while (q) { 16 /**********found**********/ 17 if (p->data > q->data) 18 { t = p->data; p->data = q->data; q->data = t; } 19 q = q->next;//相当于数组第一个数与之后所有数进行一次比较。 20 } 21 p = p->next; 22 } 23 } 24 NODE *creatlist(int a[]) 25 { NODE *h,*p,*q; int i; 26 h = (NODE *)malloc(sizeof(NODE)); 27 h->next = NULL; 28 for(i=0; i<N; i++) 29 { q=(NODE *)malloc(sizeof(NODE)); 30 q->data=a[i]; 31 q->next = NULL; 32 if (h->next == NULL) h->next = p = q; 33 else { p->next = q; p = q; } 34 } 35 return h; 36 } 37 void outlist(NODE *h) 38 { NODE *p; 39 p = h->next; 40 if (p==NULL) printf("The list is NULL! "); 41 else 42 { printf(" Head "); 43 do 44 { printf("->%d", p->data); p=p->next; } 45 while(p!=NULL); 46 printf("->End "); 47 } 48 } 49 void main() 50 { NODE *head; 51 int a[N]= {0, 10, 4, 2, 8, 6 }; 52 head=creatlist(a);//创建链表 53 printf(" The original list: "); 54 outlist(head); 55 fun(head); 56 printf(" The list after sorting : "); 57 outlist(head); 58 }
//建立一个带头节点的单向链表,并用随机函数为各个结点数据域赋值,函数fun作用求出单向链表结点(不包括头节点)数据域中的最大值。
1 #include <stdio.h> 2 #include <conio.h> 3 #include <stdlib.h> 4 typedef struct aa 5 { int data; 6 struct aa *next; 7 } NODE; 8 int fun (NODE *h) 9 { int max=-1; 10 NODE *p; 11 /*************found**************/ 12 p=h->next; 13 while(p) 14 { if(p->data>max) 15 max=p->data; 16 /*************found**************/ 17 p=p->next; 18 } 19 return max; 20 } 21 void outresult(int s, FILE *pf) 22 { fprintf(pf, " The max in link :%d ",s); 23 } 24 NODE *creatlink(int n, int m) 25 { NODE *h,*p,*s; 26 int i; 27 h=p=(NODE *)malloc(sizeof(NODE)); 28 h->data=9999; 29 for(i=1;i<=n;i++) 30 { s=(NODE *) malloc(sizeof(NODE)); 31 s->data=rand()%m; s->next=p->next; 32 p->next=s; p=p->next; 33 } 34 p->next=NULL; 35 return h; 36 } 37 void outlink(NODE *h,FILE *pf) 38 { NODE *p; 39 p=h->next; 40 fprintf(pf, " The LIST : HEAD"); 41 while(p) 42 { fprintf(pf, "->%d",p->data); 43 p=p->next;} 44 fprintf(pf, " "); 45 } 46 void main() 47 { NODE *head; int m; 48 system("CLS"); 49 head=creatlink(12,100);//创建链表 50 outlink(head,stdout); 51 m=fun(head); 52 printf(" The RESULT : "); 53 outresult(m,stdout); 54 }
//函数功能:将M*N的二维数组中的数据,按行依次放入一维数组,个数存储在形参n所指的存储单元。
1 #include <stdio.h> 2 void fun (int (*s)[10], int *b, int *n, int mm, int nn) 3 { 4 int i=0; 5 for (int m = 0; m < mm; m++) 6 { 7 for (int n = 0; n < nn; n++) 8 { 9 b[i++] = s[m][n]; 10 } 11 } 12 *n = i; 13 } 14 void main() 15 { 16 FILE *wf; 17 int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}}, i, j; 18 int a[100]={0},n=0 ; 19 printf("The matrix: "); 20 for (i=0; i<3; i++) 21 {for (j=0;j<4;j++) 22 printf("%3d",w[i][j]); 23 printf(" "); 24 } 25 fun(w,a,&n,3,4); 26 printf("The A array: "); 27 for(i=0; i<n; i++) 28 printf("%3d",a[i]); 29 printf(" "); 30 /******************************/ 31 wf=fopen("out.dat","w"); 32 for(i=0; i<n; i++) 33 fprintf(wf,"%3d",a[i]); 34 fclose(wf); 35 /*****************************/ 36 }