#include "stdio.h" #include "malloc.h" #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define OK 1 #define ERROR 0 typedef int Status; typedef int ElemType; typedef struct { ElemType* elem; int length; int listsize; }SqList; //順序表類型定義 int ListLength(SqList L);//返回表L的長度 void ListPrint(SqList L); //1 打印輸出表L中的各個元素 Status InitList_Sq(SqList& L); //1 初始化表L為空表,分配空間為100個元素的大小 Status ListEmpty(SqList L);//1 判斷表L是否為空 Status GetElem_Sq(SqList L, int i, ElemType& e); //e返回表L中位序為i的元素值 Status ListInsert_Sq(SqList& L, int i, ElemType e); //1 再表L中位序i之前插入元素e Status ListDelete_Sq(SqList& L, int i, ElemType& e);//1 刪除L中位序為i的元素,返回再e中 int LocateElem_Sq(SqList L, ElemType e); //1 在順序線性表L中查找第1個值與e相等的位置。若找到,則返回其在L中的位序,否則返回0。 void MergeList(SqList La, SqList Lb, SqList& Lc);//1 實現有序表La和Lb的合並,返回表Lc void Difference(SqList La, SqList Lb, SqList& Lc); int main()//通過主函數進行測試 { SqList La, Lb, Lc; int n, m, i,mid; ElemType x; InitList_Sq(La); scanf("%d",&n); for (i = 1; i <= n; i++) { scanf("%d",&x); ListInsert_Sq(La, La.length + 1, x); } InitList_Sq(Lb); scanf("%d",&m); for (i = 1; i <= m; i++) { scanf("%d",&x); ListInsert_Sq(Lb, Lb.length + 1, x); }//輸入函數的 MergeList (La,Lb,Lc); mid = (1 + Lc.length) / 2; printf("%d",Lc.elem[mid-1]); //打印整張表 return 0; } //利用已經實現的基本操作設計實現void MergeList(SqList La, SqList Lb, SqList &Lc) ;即可 Status InitList_Sq(SqList& L) { L.elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE); L.length = 0; if (L.elem == NULL) { return ERROR; } else { return OK; } }; //分配空間,初始化為空表。若空間分配成功,返回OK,否則返回ERROR Status ListEmpty(SqList L) { if (L.length == NULL) { return OK; } else { return ERROR; } };//判斷順序表是否為空,如果表空返回OK, 非空返回ERROR Status ListInsert_Sq(SqList& L, int i, ElemType e) { if (i <= 0 || i > L.length + 1) { printf("weixu Error\n"); return ERROR; } else { for (int j = L.length - 1; j >= i - 1; j--) { L.elem[j + 1] = L.elem[j]; } L.elem[i - 1] = e; L.length++; return OK; } } int LocateElem_Sq(SqList L, ElemType e) { for (int j = 0; j < L.length; j++) if (L.elem[j] == e) return j + 1; return 0; } Status ListDelete_Sq(SqList& L, int i, ElemType& e) { ElemType* p, * q; if (i<1 || i>L.length + 1) { printf("weixu Error\n"); return ERROR; } else { p = L.elem + i - 1; e = *p; q = L.elem + L.length - 1; for (++p; p <= q; ++p) *(p - 1) = *p; L.length--; return OK; } } void ListPrint(SqList L) //打印輸出順序表的所有元素 { int i; if (ListEmpty(L)) //如果表為空,則輸出NULL { printf("NULL"); return; } for (i = 1; i <= L.length; i++) //表非空,依次輸出各個元素值,用空格隔開。主要最后一個元素后面沒有空格 { if (i == 1) printf("%d", L.elem[i - 1]); else printf(" %d", L.elem[i - 1]); } } void MergeList(SqList La, SqList Lb, SqList& Lc) { int La_len, Lb_len; ElemType x, y; InitList_Sq(Lc); int i = 1; int j = 1; int k = 1; La_len = ListLength(La); Lb_len = ListLength(Lb); while (i <= La_len && j <= Lb_len) { GetElem_Sq(La, i, x); GetElem_Sq(Lb, j, y); if (x <= y) { ListInsert_Sq(Lc, k, x); i++; k++; } else { ListInsert_Sq(Lc, k, y); j++; k++; } } while (i <= La_len) { GetElem_Sq(La, i, x); ListInsert_Sq(Lc, k, x); i++; k++; } while (j <= Lb_len) { GetElem_Sq(Lb, j, y); ListInsert_Sq(Lc, k, y); j++; k++; } return; } int ListLength(SqList L) { return L.length; } Status GetElem_Sq(SqList L, int i, ElemType& e) { if (i <= 0 || i >= L.length + 1) { printf("weixu Error\n"); return ERROR; } else { e = L.elem[i - 1]; return OK; } } void Difference(SqList La, SqList Lb, SqList& Lc) { int i = 1, k = 1; int la_len; ElemType x; la_len = ListLength(La); InitList_Sq(Lc); while (i<=la_len) { GetElem_Sq(La, i, x); if (LocateElem_Sq(Lb,x)==0) { ListInsert_Sq(Lc, k, x); k++; } i++; } }