线性表—顺序线性表的查找元素位置、合并

 

严蔚敏的《数据结构(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 }
View Code

欢迎留言一起讨论。

 

posted @ 2018-08-20 16:33  Dawn-bin  阅读(3059)  评论(1编辑  收藏  举报