嚴蔚敏的《數據結構(C語言版)》 紫色書
碰到的問題:
1.在LocationElem_Sq函數里面有這樣一個形參 Status(* compare)(ElemType, ElemType)
為函數指針作為參數,就是調用時把(* 函數名)做實參傳入👉 LocateElem_Sq(L, e, *compare);
這里只是解釋書上的代碼,實際用的時候完全可以直接在LocationElem_Sq函數里面調用compare函數
傳入的函數指針在調用的時候 (* compare)(參數,參數)
1 int LocateElem_Sq(SqList L, ElemType e, Status(* compare)(ElemType, ElemType)) 2 { 3 //在順序線性表L中查找第1個值與e滿足compare()的元素的位序 4 //若找到,則返回其在L中的位序,否則返回0; 5 int i = 1; //i的初值為第1個元素的位序 6 ElemType *p = L.elem; //p的初值為的1個元素的儲存位置 7 while(i <= L.length && !(*compare)(*p++, e)) 8 { 9 i++; 10 } 11 if(i <= L.length) 12 return i; 13 else return 0; 14 }//LocateElem_Sq
1 void MergeList_Sq(SqList La, SqList Lb, SqList *Lc) 2 { 3 //已知順序線性表La和Lb的元素按值非遞減排列 4 //歸並La和Lb得到新的順序線性表Lc,Lc的元素也按值非遞減排列 5 ElemType *pa = La.elem, *pb = Lb.elem, *pc = Lc->elem; 6 Lc->length = La.length + Lb.length; 7 ElemType *pa_last = La.elem + La.length - 1; 8 ElemType *pb_last = Lb.elem + Lb.length - 1; 9 while(pa <= pa_last && pb <= pb_last) //歸並 10 { 11 if(*pa <= *pb) 12 *pc++ = *pa++; 13 else 14 *pc++ = *pb++; 15 } 16 17 while(pa <= pa_last) //插入La的剩余元素 18 *pc++ = *pa++; 19 while(pb <= pb_last) //插入Lb的剩余元素 20 *pc++ = *pb++; 21 }//MergeList_Sq
下面是完整代碼,可以試一試👇

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define LIST_INIT_SIZE 100 5 #define LISTINCREMENT 10 6 #define ElemType int 7 8 #define OK 1 9 #define OVERFLOW -2 10 #define ERROR -1 11 #define Status int 12 13 14 typedef struct 15 { 16 ElemType *elem; 17 int length; 18 int listsize; 19 }SqList; 20 21 Status InitList_Sq(SqList *L) 22 { 23 //構造一個空的線性表L 24 L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); 25 if (!L->elem) 26 { 27 exit(OVERFLOW); 28 } 29 L->length = 0; 30 L->listsize = LIST_INIT_SIZE; 31 return OK; 32 } 33 34 Status compare(ElemType a, ElemType b) 35 { 36 if(a==b) 37 return OK; 38 else 39 return 0; 40 } 41 42 int LocateElem_Sq(SqList L, ElemType e, Status(* compare)(ElemType, ElemType)) 43 { 44 //在順序線性表L中查找第1個值與e滿足compare()的元素的位序 45 //若找到,則返回其在L中的位序,否則返回0; 46 int i = 1; //i的初值為第1個元素的位序 47 ElemType *p = L.elem; //p的初值為的1個元素的儲存位置 48 while(i <= L.length && !(*compare)(*p++, e)) 49 { 50 i++; 51 } 52 if(i <= L.length) 53 return i; 54 else return 0; 55 }//LocateElem_Sq 56 57 void MergeList_Sq(SqList La, SqList Lb, SqList *Lc) 58 { 59 //已知順序線性表La和Lb的元素按值非遞減排列 60 //歸並La和Lb得到新的順序線性表Lc,Lc的元素也按值非遞減排列 61 ElemType *pa = La.elem, *pb = Lb.elem, *pc = Lc->elem; 62 Lc->length = La.length + Lb.length; 63 ElemType *pa_last = La.elem + La.length - 1; 64 ElemType *pb_last = Lb.elem + Lb.length - 1; 65 while(pa <= pa_last && pb <= pb_last) //歸並 66 { 67 if(*pa <= *pb) 68 *pc++ = *pa++; 69 else 70 *pc++ = *pb++; 71 } 72 73 while(pa <= pa_last) //插入La的剩余元素 74 *pc++ = *pa++; 75 while(pb <= pb_last) //插入Lb的剩余元素 76 *pc++ = *pb++; 77 }//MergeList_Sq 78 79 int main() 80 { 81 SqList L; 82 SqList La, Lb, Lc; 83 int a[ ] = {12, 13, 21, 24, 28, 30, 42, 77}; 84 85 InitList_Sq(&L); 86 for(int i=0; i<8; i++) 87 { 88 L.elem[i] = a[i]; 89 L.length++; 90 } 91 printf("~~~~~~~~~\n"); 92 for(int i=0; i<L.length; i++) 93 printf("%d ", L.elem[i]); 94 printf("\n~~~~~~~~~\n"); 95 int e; 96 scanf("%d",&e); 97 printf("%d\n",LocateElem_Sq(L, e, *compare)); 98 printf("\n~~~~~~~~~\n"); 99 100 101 InitList_Sq(&La); 102 InitList_Sq(&Lb); 103 InitList_Sq(&Lc); 104 int la[] = {3, 5, 8, 11}; 105 int lb[] = {2, 6, 8, 9, 11, 15, 20}; 106 107 for(int i = 0; i < 4; i++) 108 { 109 La.elem[i] = la[i]; 110 La.length++; 111 } 112 for(int i = 0; i < 7; i++) 113 { 114 Lb.elem[i] = lb[i]; 115 Lb.length++; 116 } 117 118 MergeList_Sq(La, Lb, &Lc); 119 120 printf("Lc.length = %d\n",Lc.length); 121 122 printf("Lc == "); 123 for(int i=0; i < Lc.length; i++) 124 printf("%d ", Lc.elem[i]); 125 126 return 0; 127 }
歡迎留言一起討論。