复习一下数据结构,希望以后每周能坚持发一章。
第一章 顺序表
- /*****************************SqList**********************************
- Date:2008.10.22
- Author:ZORO
- WebSite:http://blog.youkuaiyun.com/sevenhu
- ======================================================================
- 参考资料:
- BOOK:数据结构(C语言版) 严蔚敏 清华大学出版社
- Page:19
- ======================================================================
- ADT SqList {
- InitList(&L)
- DestroyList(&L)
- ListPush(&L,i)
- ClearList(&L)
- ListEmpty(L)
- ListLength(L)
- GetElem(L,i,&e)
- LocateElem(L,e)
- PriorElem(L,cur_e,&pre_e)
- NextElem(L,cur_e,&next_e)
- ListInsert(&L, i, e)
- ListDelete(&L, i, e)
- ListTraverse(L)
- }ADT SqList
- ======================================================================
- note:VC++ 6.0测试通过。
- *********************************************************************/
- #include <iostream>
- #define OK 1
- #define FALSE 0
- #define ERROR 0
- #define OVERFLOW -1
- #define NULL 0
- #define LIST_INIT_SIZE 100 //顺序表存储空间的初始分配量
- #define LISTINCREMENT 10 //顺序表存储空间的分配增量
- using namespace std;
- typedef int Status;
- typedef bool BOOL;
- typedef struct SQ{
- int * elem;
- int length; //顺序表当前大小
- int listsize; //顺序表容量
- } SqList;
- Status InitList(SqList &L)
- {
- //构造表
- L.elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
- if (!L.elem) { //空间分配失败
- return OVERFLOW;
- }
- L.length = 0; //初始化长度为0
- L.listsize = LIST_INIT_SIZE; //初始存储容量
- return OK;
- } //InitList_Sq
- Status ListPush(SqList &L, int i)
- {
- //增加元素
- if (!L.elem) {
- return ERROR; //表不存在
- }
- if (L.length == L.listsize) { //空间不足,重新分配
- L.elem = (int *)realloc(L.elem,sizeof(int) * (LISTINCREMENT + L.listsize));
- if (!L.elem) {
- return OVERFLOW; //分配失败
- }
- L.listsize += LISTINCREMENT; //新空间容量
- }
- L.elem [L.length] = i;
- ++L.length;
- return OK;
- }
- Status ListInsert(SqList &L, int i, int e)
- {
- //在顺序表L中第i个位置前插入元素e
- if (!L.elem) {
- return ERROR; //表不存在
- }
- if (i > L.length - 1 || i < 1) { //i值不合法
- return ERROR;
- }
- if (L.length == L.listsize) {
- int * newbase; //空间不足,重新分配
- newbase = (int *)realloc(L.elem,sizeof(int) * (LISTINCREMENT + L.listsize));
- L.elem = newbase;
- if (!L.elem) {
- return OVERFLOW; //分配失败
- }
- L.listsize += LISTINCREMENT; //新空间容量
- }
- int * q = &L.elem[i-1]; //标记插入位置
- for (int * p = &L.elem[L.length-1]; p >= q; --p) {
- *(p+1) = *p; //插入位置及其后元素后移一位
- }
- *q = e; //插入e
- ++L.length; //长度加1
- q = p = NULL;
- return OK;
- }//ListInsert
- Status ListDelete(SqList &L, int i, int &e)
- {
- //在顺序表L中删除第i个值并用e返回这个值
- if (!L.elem) {
- return ERROR; //表不存在
- }
- if (i < 1 || i > L.length - 1) {
- return ERROR; //i值不合法
- }
- int *p = &L.elem[i-1]; //i位置的元素地址
- e = *p;
- int *q = L.elem + L.length - 1; //L的最后一个元素地址
- for (++p ; p <= q; ++p) {
- *(p-1) = *p; //把i位置后的元素向前移一位
- }
- --L.length; //L的长度减1
- return OK;
- }//ListDelete
- Status DestroyList(SqList &L)
- {
- //销毁L表
- if (!L.elem) {
- return ERROR; //表不存在
- }
- free(L.elem); //释放内存
- return OK;
- }//DestroyList
- Status ClearList(SqList &L)
- {
- //清空表
- if (!L.elem) {
- return ERROR; //表不存在
- }
- L.length = 0; //表长清零
- return OK;
- }//ClearList
- BOOL ListEmpty(SqList L)
- {
- //判断表是否为空
- if (!L.elem) {
- return ERROR; //表不存在
- }
- if (L.length == 0) {
- return true;
- }
- return false;
- }//ListEmpty
- int ListLength(SqList L)
- {
- //表元素的个数
- if (!L.elem) {
- return ERROR; //表不存在
- }
- return L.length;
- }//ListLength
- Status GetElem(SqList L, int i, int &e)
- {
- //用e返回L表中第i个元素
- if (!L.elem) {
- return ERROR; //表不存在
- }
- if (i < 1 || i > L.length - 1) {
- return ERROR; //i值不合法
- }
- int * q = L.elem ;
- e = *(q + i -1); //把第i个元素给e
- return OK;
- }//GetElem
- int LocateElem(SqList L, int e)
- {
- //返回与e相等的第一个元素的位置
- if (!L.elem) {
- return ERROR; //表不存在
- }
- int i = 1;
- int * p = L.elem ;
- while (i < L.length && *p++ != e ) {//查找与e相等的值的位置
- ++i;
- }
- if (i <= L.length ) {
- return i;
- }
- return 0;
- }
- Status PriorElem(SqList L, int cur_e, int & pre_e)
- {
- //若cur_e是L的数据元素,且不是第一个,用pre_e返回它的前驱,否则操作失败,pre_e无定义
- if (!L.elem) {
- return ERROR; //表不存在
- }
- int i = LocateElem(L,cur_e);
- if (i == 1) {
- return FALSE;
- }
- else if(i == 0) {
- return FALSE;
- }
- pre_e = L.elem [i-2]; //前驱值给pre_e
- return OK;
- }//PriorElem
- Status NextElem(SqList L, int cur_e, int & next_e)
- {
- //若cur_e是L的数据元素,且不是最后一个,用next_e返回它的后继,否则操作失败,next_e无定义
- if (!L.elem) {
- return ERROR; //表不存在
- }
- int i = LocateElem(L,cur_e);
- if (i == L.length ) {
- return FALSE;
- }
- else if (i == 0) {
- return FALSE;
- }
- next_e = L.elem [i]; //后继值给next_e
- return OK;
- }//NextElem
- Status ListTraverse(SqList L)
- {
- //遍历L表
- if (!L.elem) {
- return ERROR; //表不存在
- }
- for (int i = 0 ; i < L.length ; ++i) {
- cout << L.elem[i] << " "; //输出
- }
- cout << endl;
- return OK;
- }//ListTraverse
- int main()
- {
- SqList L;
- if (InitList(L) == OK) {
- cout << "成功创建顺序表!" << endl;
- }
- else {
- cout << "创建顺序表失败!" << endl;
- return FALSE;
- }
- for (int i = 0; i < 15; ++i) {
- if (ListPush(L,i) != OK) {
- cout << "录入数据失败!" << endl;
- return FALSE;
- }
- }
- cout << "成功录入数据!" << endl;
- cout << "遍历顺序表!" << endl;
- ListTraverse(L);
- cout << "共" << ListLength(L) << "个元素" << endl;
- cout << "清空顺序表!" << endl;
- ClearList(L);
- cout << "是否是空表?" << endl;
- cout << ListEmpty(L) << endl;
- for (i = 15; i > 0; --i) {
- if (ListPush(L,i) != OK) {
- cout << "录入数据失败!" << endl;
- return FALSE;
- }
- }
- cout << "成功录入数据!" << endl;
- cout << "遍历顺序表!" << endl;
- ListTraverse(L);
- int e;
- GetElem(L,6,e);
- cout << "第6个元素的值:"<< e << endl;
- PriorElem(L,7,e);
- cout << "元素7的前驱元素:" << e << endl;
- NextElem(L,7,e);
- cout << "元素7的后继元素:" << e << endl;
- cout << "在第4个位置插入元素100" << endl;
- ListInsert(L,4,100);
- ListTraverse(L);
- cout << "删除第6个位置的元素" << endl;
- ListDelete(L,6,e);
- cout << "删除的元素为:" << e << endl;
- ListTraverse(L);
- cout << "元素4的位置是" << LocateElem(L,4) << endl;
- if (DestroyList(L) == OK) {
- cout << "成功销毁顺序表!" << endl;
- return 0;
- }
- else
- {
- cout << "销毁顺序表失败!" << endl;
- return FALSE;
- }
- }