演算法與資料結構--實現線性表的合併操作(合併後按非遞減排列)
阿新 • • 發佈:2019-02-17
/*
檔名稱:實現線性表的合併操作(合併後按非遞減排列)
檔名稱:實現線性表的合併操作(合併後按非遞減排列)
*/
#include <bits/stdc++.h> using namespace std; #define LIST_INIT_SIZE 100 //線性表儲存空間的初始分配量 #define LISTINCREMENT 10 //線性表儲存空間的分配增量 typedef int ElemType; //定義別名 typedef int Status; //定義別名 typedef struct{ ElemType *elem; //儲存空間基址 int length; //當前長度 int listsize; //當前分配的儲存容量(以sizeof(ElemType)為單位) }SqList; Status InitList_Sq(SqList &L){ //構造一個空的線性表L L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); if(!L.elem) exit(1); //儲存分配失敗,z正常退出 L.length = 0; //空表長度為0 L.listsize = LIST_INIT_SIZE; //初始儲存容量 return true; } Status ListInsert_Sq(SqList &L,int i,ElemType e) { //在順序線性表L中第i個位置之前插入新的元素e //i的合法值為1<=i<=ListLength_Sq(L)+1 if(i <1 || i> L.length + 1) return false; //i值不合法 if(L.length >= L.listsize) //當前儲存空間已滿,增加分配 { ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT )* sizeof(ElemType)); if(!newbase) exit(1); //儲存分配失敗 L.elem = newbase;//新基址 L.listsize += LISTINCREMENT; //增加儲存容量 } ElemType *q = &(L.elem[i-1]);//q為插入位置 for(ElemType *p = &(L.elem[L.length-1]);p>=q;--p) *(p+1) = *p; //插入位置及之後的元素右移 *q = e; //插入e ++L.length; //表長增1 return true; } Status ListDelete_Sq(SqList &L,int i) { //在順序線性表L中刪除第i個元素,並用e返回其值 //i的合法值為 1<= i<=ListLength_Sq(L) if((i<1)||(i>L.length)) return false; //i值不合法 ElemType *p = &(L.elem[i-1]); //p為被刪除元素的位置 ElemType e = *p; //被刪除元素的值賦值給e ElemType *q = L.elem + L.length-1; //表尾元素的位置 for(++p;p<=q;++p) *(p-1) = *p; //被刪除元素之後的元素左移 --L.length; //表長減1 return e; } Status compare(ElemType e1,ElemType e2) { if(e1==e2) return true; return false; } int LocateElem_sq(SqList L,ElemType e,Status (*compare)(ElemType,ElemType)) { //在順序線性表L中查詢第1個值與e滿足compare()的元素的為序 //若找到,則返回其在L中的位序,否則返回0 int i = 1;//i的初值為第1個元素的位序 ElemType *p = L.elem; //p的初值為為第1個元素的儲存位置 while(i<=L.length &&!(*compare)(*p++,e)) { ++i; } if(i<=L.length) { return i; } else { return 0; } } void input(SqList L) { int i = 1;//i的初值為第1個元素的位序 ElemType *p = L.elem; //p的初值為為第1個元素的儲存位置 while(i<=L.length) { cout<<*(p++)<<" "; ++i; } cout<<endl; } void MergeList_Sq(SqList La,SqList Lb,SqList &Lc) { //已知順序線性表La和Lb的元素按值非遞減排列 //歸併La和Lb得到新的順序線性表Lc,Lc的元素也按值非遞減排列 ElemType *pa = La.elem; ElemType *pb = Lb.elem; Lc.listsize = Lc.length = La.length + Lb.length; ElemType *pc = Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType)); if(!Lc.elem) exit(1); //儲存分配失敗,非正常退出 ElemType *pa_last = (La.elem + La.length -1); ElemType *pb_last = (Lb.elem + Lb.length -1); while(pa<=pa_last&&pb<=pb_last)//歸併 { if(*pa<=*pb) { *pc++=*pa++; } else { *pc++=*pb++; } } while(pa<=pa_last)//插入La的剩餘元素 { *pc++=*pa++; } while(pb<=pb_last)//插入Lb的剩餘元素 { *pc++=*pb++; } } int main() { SqList La,Lb,Lc; InitList_Sq(La); InitList_Sq(Lb); InitList_Sq(Lc); ListInsert_Sq(La,1,2);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(La,2,3);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(La,3,4);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(La,4,5);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(Lb,1,6);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(Lb,2,7);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(Lb,3,8);//在順序線性表L中第i個位置之前插入新的元素e ListInsert_Sq(Lb,4,9);//在順序線性表L中第i個位置之前插入新的元素e //ListInsert_Sq(Lc,1,2);//在順序線性表L中第i個位置之前插入新的元素e //ListInsert_Sq(Lc,2,3);//在順序線性表L中第i個位置之前插入新的元素e //ListInsert_Sq(Lc,3,4);//在順序線性表L中第i個位置之前插入新的元素e //ListInsert_Sq(Lc,4,5);//在順序線性表L中第i個位置之前插入新的元素e //ElemType e = ListDelete_Sq(La,1); cout<<"線性表中La的所有元素為:"; input(La); cout<<"線性表中Lb的所有元素為:"; input(Lb); //int e = LocateElem_sq(La,3,(*compare));//在順序線性表L中查詢第1個值與e滿足compare()的元素的為序 //cout<<"線性表中與3相等的元素的位序為:"<<e<<endl; cout<<"La與Lb合併後,線性表中Lc的所有元素為:"<<endl; MergeList_Sq(La,Lb,Lc); input(Lc); system("PAUSE"); return 0; }