/* ADT List{ 基本操作: 12个 InitList(&L) 操作结果 构造一个空的线性表L DestoryList(&L) 初始条件 线性表L已存在 操作结果 销毁线性表L ClearList(&L) 初始条件 线性表L已存在 操作结果 将L重置为空表 ListEmpyt(L) 初始条件 线性表L已存在 操作结果 若L为空表,则返回true,否则返回false ListLength(L) 初始条件 线性表L已存在 操作结果 返回L中数据元素的个数 GetElem(L,i,&e) 初始条件 线性表L已存在,且1<= i <= ListLength(L) 操作结果 用e返回L中第i个数据元素的值 LocateElem(L,e) 初始条件 线性表L已存在 操作结果 返回L中第1个值与e相同的元素再L中的位置。若这样的数据元素不存在,则返回值为0 PriorElem(L,cur_e,&pre_e) 初始条件 线性表L已存在 操作结果 若cur_e是L的数据元素,且不是第一个,则用pre_e返回其前驱,否则操作失败,pre_e无定义 NextElem(L,cur_e,&next_e) 初始条件 线性表L已存在 操作结果 若cur_e是L的数据元素,且不是最后一个,则用next_e返回其后继,否则操作失败,next_e无定义 ListInsert(&L,i,e) 初始条件 线性表L已存在,且1 <= i <= ListLength(L) + 1 操作结果 在L中第i个位置之前插入新的数据元素e,L长度加1 ListDelete(&L,i) 初始条件 线性表L已存在且非空,且1 <= i <= Listlength(L) 操作结果 删除L的第i个数据元素,L的长度减1 TraverseList(L) 初始条件 线性表L已存在 操作结果 对线性表L进行遍历,在遍历过程中对L的每个结点访问一次 }ADT List 注意 & 代表结构本身代入进去了 带这个&操作就是对这个结构本身操作 所以 要改变结构本身的方法都是带入了 & 不改变的话 就不能带 & 但是 c语言中没有 &方法 所以我们可以指针来实现 用 T() 代表时间复杂度 S() 代表空间复杂度 */ #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 #define Elemtype int #define Staues int #define OVERFLOW -2 #define ERROR -1 #define OK 0 #define TRUE 1 #define FALSE 0 // 数据结构定义 typedef struct { Elemtype *elem; int length; }SqList; // 构造一个空的顺序表 Staues InitList(SqList *L) { L->elem = (Elemtype *)malloc(sizeof(Elemtype) * MAXSIZE); if(!L->elem) exit(OVERFLOW); L->length = 0; return OK; } // 顺序表的取值 T(1) Staues GetElem(SqList L,int i,Elemtype *e) { if (i < 1 || i > L.length) return ERROR; *e = L.elem[i-1]; return OK; } // 顺序表的查找 在顺序表L中查找值为e的数据元素,返回其序号 T(n) int LocateElem(SqList L,Elemtype e) { for (int i = 0; i < L.length; i++) { if(L.elem[i] == e) return i+1; } return 0; } // 在顺序表L中第i个位置之前插入新的元素e i 合法范围 1 <= i <= L.length + 1 T(n) Staues ListInsert(SqList *L,int i,Elemtype e) { if(i < 1 || i > L->length + 1) return ERROR; if(L->length == MAXSIZE) return ERROR; 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; } // 顺序表的删除 在顺序表L中删除第i个元素 i值的合法范围是 1 <= i <= L.length T(n) Staues ListDelete(SqList *L,int i) { if(i < 1 || i > L->length) return ERROR; for (int j = i; j <= L->length-1; j++) { L->elem[j-1] = L->elem[j]; } --L->length; return OK; } // 销毁线性表 Staues DestoryList(SqList *L) { free(L->elem); L->elem = NULL; L->length = 0; return OK; } //将L重置为空表 Staues ClearList(SqList* L) { L->length = 0; return OK; } // 判断List是否存在 空表返回True 否则返回False Staues ListEmpyt(SqList L) { if(L.length == 0) return TRUE; else return FALSE; } // 判断List表长 Staues ListLength(SqList L) { return L.length; } // 若cur_e是L的数据元素,且不是第一个,则用pre_e返回其前驱,否则操作失败,pre_e无定义 T(n) Elemtype* PriorElem(SqList L,Elemtype cur_e,Elemtype* pre_e) { pre_e = L.elem; for (int i = 0; i < L.length; i++) { if (L.elem[i] == cur_e) { if (i != 0) { return pre_e+i-1; } } } pre_e = NULL; return pre_e; } // 操作结果 若cur_e是L的数据元素,且不是最后一个,则用next_e返回其后继,否则操作失败,next_e无定义 T(n) Elemtype* NextElem(SqList L,Elemtype cur_e,Elemtype* next_e) { next_e = L.elem; for (int i = 0; i < L.length; i++) { if (L.elem[i] == cur_e) { if (i != L.length-1) { return next_e+i+1; } } } next_e = NULL; return next_e; } // 操作结果 对线性表L进行遍历,在遍历过程中对L的每个结点访问一次 T(n) Staues TraverseList(SqList L) { for (int i = 0; i < L.length; i++) { printf("%d\t",L.elem[i]); } printf("\n"); } // 简单测试一下线性表的功能 int main() { SqList L; InitList(&L); for (int i = 0; i < 10; i++) //不断的在第一个位置添加元素 { ListInsert(&L,1,i); } TraverseList(L); printf("%d\n",ListLength(L)); int c; GetElem(L,2,&c); printf("c = %d\n",c); ListDelete(&L,2); TraverseList(L); int* next_e; int* pre_e; // pre_e = L.elem; L.elem 等价于 &L.elem[0] // printf("first %d\n",*(pre_e+1)); next_e = NextElem(L,5,next_e); printf("next_e = %d\n",*next_e); // pre_e = PriorElem(L,9,pre_e); // printf("pre_e = %d\n",*pre_e); pre_e = PriorElem(L,5,pre_e); printf("pre_e = %d\n",*pre_e); return 0; }
浙公网安备 33010602011771号