1. 基础操作
/** * 初始化顺序表 */void initList(SqList *L) { L->length=0;}/** * 销毁顺序表 */void destroyList(SqList *L) { L->length = -1;}/** * 置空顺序表 */void clearList(SqList *L) { L->length = 0;}/** * 如果顺序表为空表,则返回真,否则返回假 */int emptyList(SqList *L) { if (0 == L->length) { return OK; } else { return FALSE; }}/** * L存在,且i值合法,即1 <= i <= length, 则返回第i个元素的值 */int getList(SqList *L, int i) { if (L->length <= 0 || i > L->length) { return FALSE; } return L->r[i];}
2. 插入操作
线性表的插入运算是指在表的第i个位置,插入一个新元素e,使长度为n的线性表变成长度为n + 1的线性表。
/** * 表L存在,e为合法元素值,且1 <= i <= length + 1 * 在表L中第i个位置插入新的元素e,L的长度加1 * 注意:本例子中,默认从下标1开始,如顺序表[0,1]的长度为1 */int insList(SqList *L, int i, int e) { int j; if (L->length == MAXSIZE) { return FALSE; } if (i <= 0 || i > L->length + 1) { return FALSE; } if (i <= L->length) { for (j = L->length + 1; j >= i ; j--) { L->r[j + 1] = L->r[j]; } } L->r[i] = e; L->length++; return OK;}
3. 删除操作
线性表的删除操作时指将表的第i个元素删去,使长度为n的线性表变成长度为n-1的线性表。
/** * 表L存在且非空,1 <= i <= length * 删除L的第i个数据元素,并用e返回其值,L的长度减1 */void delList(SqList *L, int i, int *e) { int j; *e = L->r[i]; if (i < L->length) { for (j = i; j <= L->length; j++) { L->r[j] = L->r[j + 1]; } } L->length--;}
4. 合并操作
两个有序线性表,使新线性表也有序
方法一:从线性表尾开始比较,指到其中一个线性表的表长为0,然后将另一线性表的剩余元素移动到新线性表上
/** * 合并LA、LB两个顺序表 */int unionList(SqList *L, SqList *LA, SqList *LB) { if (LA->length + LB->length > MAXSIZE) { return FALSE; } L->length = LA->length + LB->length; int i; for (i = LA->length + LB->length; LA->length > 0 && LB->length > 0; i--) { if (LA->r[LA->length] >= LB->r[LB->length]) { L->r[i] = LA->r[LA->length]; LA->length--; } else { L->r[i] = LB->r[LB->length]; LB->length--; } } if (LA->length > 0) { for (i; i > 0; i--) { L->r[i] = LA->r[LA->length]; LA->length--; } } else { for (i; i > 0; i--) { L->r[i] = LB->r[LB->length]; LB->length--; } } return OK;}
方法二:设两个指针i,j,k分别指向待合并的线性表LA、LB、新线性表,如果LA->r[i] >= LB->r[j],则将LA->r[i]插入到新线性表中,i++,k++;否则反之,j++,k++。
/** * 合并LA、LB两个顺序表 */int unionList2(SqList *L, SqList *LA, SqList *LB) { if (LA->length + LB->length > MAXSIZE) { return FALSE; } int i = 1, j = 1, k = 1, n; L->length = LA->length + LB->length; while (i <= LA->length && j <= LB->length) { if (LA->r[i] >= LB->r[j]) { L->r[k] = LA->r[i]; i++; k++; } else { L->r[k] = LB->r[j]; j++; k++; } } while (i <= LA->length) { L->r[k] = LA->r[i]; i++; k++; } while (i <= LB->length) { L->r[k] = LB->r[j]; j++; k++; } return OK;}
完整实例:
#include#include #define MAXSIZE 101#define OK 1#define FALSE 0typedef struct { int r[MAXSIZE]; int length;} SqList;void print(SqList L);void initList(SqList *L);void destroyList(SqList *L);void clearList(SqList *L);int emptyList(SqList *L);int locateList(SqList *L, int e);int getList(SqList *L, int e);int insList(SqList *L, int i, int e);void delList(SqList *L, int i, int *e);int unionList(SqList *L, SqList *LA, SqList *LB);int unionList2(SqList *L, SqList *LA, SqList *LB);int main(int argc, char *argv[]) { SqList list, list1, list2; int result, deleteNum; printf("初始化顺序表:\n"); initList(&list); initList(&list1); printf("长度为:%d\n", list.length); for (int i = 1; i <= 6; i++) { list.r[i] = i * 3; list.length++; } for (int i = 1; i <= 4; i++) { list1.r[i] = i * 2; list1.length++; } printf("顺序表的内容:\n"); print(list); if (insList(&list, 6, 2)) { printf("插入成功\n"); printf("插入元素后的顺序表的内容:\n"); print(list); } else { printf("插入失败\n"); } delList(&list, 6, &deleteNum); printf("删除的数字是:%d\n", deleteNum); printf("删除后的顺序表的内容:\n"); print(list); printf("顺序表LB的内容:\n"); print(list1); printf("顺序表合并后的内容:\n"); unionList(&list2, &list, &list1); print(list2); destroyList(&list); return 0;}/** * 打印函数 */void print(SqList L) { for (int i = 1; i <= L.length; i++) { printf("%d\t", L.r[i]); } printf("\n");}/** * 初始化顺序表 */void initList(SqList *L) { L->length=0;}/** * 销毁顺序表 */void destroyList(SqList *L) { L->length = -1;}/** * 置空顺序表 */void clearList(SqList *L) { L->length = 0;}/** * 如果顺序表为空表,则返回真,否则返回假 */int emptyList(SqList *L) { if (0 == L->length) { return OK; } else { return FALSE; }}/** * L存在,且i值合法,即1 <= i <= length, 则返回第i个元素的值 */int getList(SqList *L, int i) { if (L->length <= 0 || i > L->length) { return FALSE; } return L->r[i];}/** * 表L存在,e为合法元素值,且1 <= i <= length + 1 * 在表L中第i个位置插入新的元素e,L的长度加1 * 注意:本例子中,默认从下标1开始,如顺序表[0,1]的长度为1 */int insList(SqList *L, int i, int e) { int j; if (L->length == MAXSIZE) { return FALSE; } if (i <= 0 || i > L->length + 1) { return FALSE; } if (i <= L->length) { for (j = L->length + 1; j >= i ; j--) { L->r[j + 1] = L->r[j]; } } L->r[i] = e; L->length++; return OK;}/** * 表L存在且非空,1 <= i <= length * 删除L的第i个数据元素,并用e返回其值,L的长度减1 */void delList(SqList *L, int i, int *e) { int j; *e = L->r[i]; if (i < L->length) { for (j = i; j <= L->length; j++) { L->r[j] = L->r[j + 1]; } } L->length--;}/** * 合并LA、LB两个顺序表 */int unionList(SqList *L, SqList *LA, SqList *LB) { if (LA->length + LB->length > MAXSIZE) { return FALSE; } L->length = LA->length + LB->length; int i; for (i = LA->length + LB->length; LA->length > 0 && LB->length > 0; i--) { if (LA->r[LA->length] >= LB->r[LB->length]) { L->r[i] = LA->r[LA->length]; LA->length--; } else { L->r[i] = LB->r[LB->length]; LB->length--; } } if (LA->length > 0) { for (i; i > 0; i--) { L->r[i] = LA->r[LA->length]; LA->length--; } } else { for (i; i > 0; i--) { L->r[i] = LB->r[LB->length]; LB->length--; } } return OK;}/** * 合并LA、LB两个顺序表 */int unionList2(SqList *L, SqList *LA, SqList *LB) { if (LA->length + LB->length > MAXSIZE) { return FALSE; } int i = 1, j = 1, k = 1, n; L->length = LA->length + LB->length; while (i <= LA->length && j <= LB->length) { if (LA->r[i] >= LB->r[j]) { L->r[k] = LA->r[i]; i++; k++; } else { L->r[k] = LB->r[j]; j++; k++; } } while (i <= LA->length) { L->r[k] = LA->r[i]; i++; k++; } while (i <= LB->length) { L->r[k] = LB->r[j]; j++; k++; } return OK;}