注意:要严格按照后缀名新建文件。
如果按.h创建文件,后来简单重命名为.cpp文件,编译会出错。
顺序表的实现 包含4个文件:
c1.h 是预处理指令;
c2-1.h 是SqList的数据结构;
bo2-1.cpp 是SqList的基本操作函数(basic operations 缩写为 bo);
main2-1.cpp 是测试函数。
//c1.h#include#include #include #define OK 1#define ERROR 0#define INFEASIBLE -1typedef int Status;
//c2-1.h#ifndef C2_1_H#define C2_1_Htypedef int ElemType;#define INIT_SIZE 10#define INCREMENT 2struct SqList{ ElemType *elem; int length; int listsize;};void InitList(SqList &L);int ListLength(SqList L);Status ListEmpty(SqList L);void ListTraverse(SqList L);Status ListInsert(SqList &L, int index, ElemType e);Status ListDelete(SqList &L, int index, ElemType &e);void ClearList(SqList &L);Status GetElem(SqList L, int index, ElemType &e);int LocateElem(SqList L, ElemType e);Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e);Status NextElem(SqList L, ElemType cur_e, ElemType &next_e);void DestroyList(SqList &L);#endif
//bo2-1.cpp#include "c1.h"#include "c2-1.h"using namespace std;void InitList(SqList &L){ if (!(L.elem = (ElemType *)malloc(INIT_SIZE*sizeof(ElemType)))) exit(OVERFLOW); L.length = 0; L.listsize = INIT_SIZE;}int ListLength(SqList L){ return L.length;}Status ListEmpty(SqList L){ if (L.length == 0) return OK; else return ERROR;}void ListTraverse(SqList L){ for (int i = 0; i < L.length; i++) { if (i % 10 == 0 && i != 0) cout << endl; cout << L.elem[i] << " "; } cout << endl;}Status ListInsert(SqList &L, int index, ElemType e){ if (index >= 1 && index <= L.length + 1) { if (L.listsize == L.length) { if (!(L.elem = (ElemType *)realloc(L.elem, (L.length + INCREMENT)*sizeof(ElemType)))) exit(OVERFLOW); L.listsize = L.length + INCREMENT; } //insert by index /* if (index == L.length + 1) { L.elem[index - 1] = e; } else { for (int i = L.length - 1; i >= index - 1; i--) { L.elem[i + 1] = L.elem[i]; } L.elem[index - 1] = e; } */ //insert by pointer ElemType *p, *q; p = L.elem + index - 1; for (q = L.elem + L.length - 1; q >= p; q--) *(q + 1) = *q; *p = e; L.length++; return OK; } else return ERROR;}Status ListDelete(SqList &L, int index, ElemType &e){ if (index >= 1 && index <= L.length) { ElemType *p, *q; p = L.elem + index - 1; e = *p; q = L.elem + L.length - 1; while (p < q) { *p = *(p + 1); p++; } L.length--; return OK; } else return ERROR;}void ClearList(SqList &L){ L.length = 0;}Status GetElem(SqList L, int index, ElemType &e){ if (index < 1 || index > L.length) return ERROR; e = *(L.elem + index - 1); return OK;}int LocateElem(SqList L, ElemType e){ if (L.length == 0) return 0; else { for (int i = 0; i < L.length; i++) { if (e == *(L.elem + i)) return i + 1; } return 0; } }Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e){ int location = LocateElem(L, cur_e); if (location == -1 || location == 1) return ERROR; pre_e = *(L.elem + location - 2); return OK;}Status NextElem(SqList L, ElemType cur_e, ElemType &next_e){ int location = LocateElem(L, cur_e); if (location == -1 || location == L.length) return ERROR; next_e = *(L.elem + location); return OK;}void DestroyList(SqList &L){ free(L.elem); L.elem = nullptr; L.length = 0; L.listsize = 0;}
//main2-1.cpp#include "c1.h"#include "c2-1.h"using namespace std;int main(){ SqList L; InitList(L); cout << "ListLength: " << ListLength(L) << endl; cout << "ListEmpty: " << ListEmpty(L) << endl; for (int i = 1; i <= 100; i++) ListInsert(L, i, i); cout << "ListLength: " << ListLength(L) << endl; ListTraverse(L); ElemType e; ListDelete(L, 1, e); cout << "ListLength: " << ListLength(L) << endl; ListTraverse(L); cout << "deleted element: " << e << endl; GetElem(L, 33, e); cout << "GetElem: " << e << endl; cout << "LocateElem: " << LocateElem(L, 2) << endl; PriorElem(L, 67, e); cout << "PriorElem: " << e << endl; NextElem(L, 67, e); cout << "NextElem: " << e << endl; ClearList(L); ListTraverse(L); cout << "ListLength: " << ListLength(L) << endl; DestroyList(L); cout << "ListLength: " << ListLength(L) << endl; cout << "ListSize: " << L.listsize << endl; cin.get(); return 0;}