自己把順序表用C語言寫了出來,並小試牛刀,做了一題,將La與Lb合并。
#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define LIST_INST_SIZE 5#define LISTINCREMENT 1typedef int Status;typedef int ElemType;typedef struct { ElemType *elem; int length; int listsize;} sql;static ElemType element;/*函數名:InitList() 參數:sql L 初始條件:無 功能:構造一個空線性表 傳回值:儲存分配失敗:OVERFLOW 儲存分配成功:OK*/Status InitList(sql &L){ if((L.elem = (ElemType*)malloc(sizeof(ElemType)* LIST_INST_SIZE)) == NULL) return OVERFLOW; else { L.length = 0; L.listsize = LIST_INST_SIZE; return OK; } } /*函數名:DestroyList() 參數: sql L 初始條件:線性表L已存在 功能:銷毀線性表 傳回值:L.elem == NULL:ERRor L.elem != NUll:OK*/Status DestroyList(sql &L){ if(L.elem == NULL) return ERROR; else { free(L.elem); return OK; } } /*函數名:ClearList() 參數:sql L; 初始條件:線性已存在表L 功能:清空線性表 傳回值:L.elem == NULL :ERROR L.elem != NULL :OK*/Status ClearList(sql &L){ if (L.elem == NULL) return ERROR; else { L.length = 0; return OK; } } /*函數名:LIstEmpty() 參數:sql L; 初始條件:線性表存在 功能:判斷線性表是否為空白 返回:空:TRUE 非空:FALSE*/Status ListEmpty(sql &L){ if (L.length == 0) return true; else return false;} /*函數名:ListLength() 參數:sql L 初始條件:線性表已存在 功能:返回線性表長度 傳回值:線性表長度(L.length)*/Status ListLength(sql &L){ return L.length; } /*函數名:GetElem() 參數: SqList L, int i,ElemType *e(這裡面用了全域變數element) 初始條件:線性表已存在 功能:用e返回線性表第i個元素的值 傳回值: (i < 1)||(i > ListLength(L)) : OVERFLOW 1<= i <=ListLength(l) :OK*/Status GetElem(sql &L, int i,ElemType &e ){ if ((i < 1)||(i > ListLength(L)) ) return OVERFLOW; int j; ElemType *p; p = L.elem; for (j = 0; j < i - 1; j++) L.elem++; e = *L.elem; L.elem = p; return OK; } /*函數名:LocateElem() 參數:sql L, ElemType element 初始條件:線性表L已存在 功能:返回順序表中第一個與element相等的元素的位序 傳回值:若L中存在與element相等的元素,則返回 第一個合格元素位序 若不存在,則返回0*/Status LocateElem(sql &L, ElemType E){ int i; ElemType *p; p = L.elem; for (i = 1;i <= L.length; i++) { if (*L.elem == E) { L.elem = p; return i; } L.elem++; } L.elem = p; return 0; } /*函數名:PriorElem() 參數:sql L, Elemtype cur_e, ElemType *pre_e 初始條件:線性表已存在,i>1&&i<=L.length,LocationElem()存在 功能:用pre_e返回線性表中cur_e的前驅 傳回值: i<=1||i>L.length:OVERFLOW i>1&&i<=L.length:OK */Status PriorElem(sql &L, ElemType cur_e, ElemType *pre_e){ ElemType *p = L.elem; int i; i = LocateElem(L, cur_e); //為什麼 用&L不行 因為這是按引用調用 if (i == 0) return OVERFLOW; int j; for (j = 0;j < i - 2; j++) { L.elem++ ; } *pre_e = *L.elem; //注意 pre_e = L.elem 不行 L.elem = p; return OK; } /*函數名:ListInsert(); 參數:sql L, int i, ElemType e 初始條件:線性表L已存在,1 <= i <=L.length + 1; 功能:線上性表第i個元素前插入e 傳回值:失敗:ERROR 成功:OK */Status ListInsert(sql &L, int i, ElemType e){ int *q = &(L.elem[i - 1]); ElemType *newbase, *p; if (i < 1||i > (L.length)+1) return ERROR; if (L.length >= L.listsize) { newbase = (ElemType*)realloc(L.elem,L.listsize + LISTINCREMENT*sizeof(ElemType)) ; if (newbase == NULL) exit(OVERFLOW); L.elem = newbase; L.listsize+=LISTINCREMENT; } for (p = &(L.elem[L.length - 1]);p >= q; p--) *(p + 1) = *p; *q = e; ++L.length; return OK; } /*函數名:ListDelete() 參數:sql L, int i;ElemType *e; 初始條件:線性表L已存在。1<= i <= L.length 功能:刪除線性表中第i個元素,並用e返回被刪數的值 傳回值:失敗:ERROR 成功:OK*/Status ListDelete(sql &L, int i, ElemType &e){ if ((i < 1)||(i > L.length)) return ERROR; e = (L.elem[i - 1]); ElemType *q; for (q = &(L.elem[i - 1]); q < &(L.elem[L.length - 1]); q ++ ) { *q = *(q + 1); } L.length--; return OK; } Status ListSee(sql &L){ if ((L.elem == NULL)||(L.length==0)) return ERROR; else { int i; for (i = 0; i < L.length; i++) { printf("%d ",L.elem[i]); } printf("\n"); } return OK;}Status ListScan(sql &L){ int i, a; printf("enter numbers:"); for (i = 0;i < LIST_INST_SIZE ; i++) { scanf("%d",&a); ListInsert(L, L.length + 1, a); } }int main(){ sql La, Lb; int i, e; InitList(La); InitList(Lb); ListScan(La); ListScan(Lb); for (i = 0; i < Lb.length; i++) { e = Lb.elem[i]; if (!LocateElem(La, e)) ListInsert(La, La.length + 1, e); // Lb.elem++; } printf("La = "); ListSee(La); system ("pause"); return 0;}