数据结构(顺序表-实现)
2017-03-03 21:08
477 查看
#include "List.h" #include <stdlib.h> #include <stdio.h> #include <string.h> //memset #define RETURN_OK 0 #define RETURN_ERROR -1 /* 构造一个空的线性表 */ int InitList(SqList &L) { L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); if (NULL == L.elem) { return RETURN_ERROR; } (void)memset(L.elem, 0, LIST_INIT_SIZE * sizeof(ElemType)); L.length = 0; L.listSize = LIST_INIT_SIZE; return RETURN_OK; } /* 销毁线性表 */ void DestoryList(SqList &L) { if (NULL != L.elem) { L.length = 0; free(L.elem); L.elem = NULL; } } /* 将L重置为空表 */ int ClearList(SqList &L) { if (NULL == L.elem) { return RETURN_ERROR; } L.length = 0; return RETURN_OK; } /* 若L为空表,返回为false,否者返回true */ bool ListEmpty(SqList L) { if (0 == L.length) { return true; } return false; } /* 获取线性表中元素的个数 */ int ListLength(SqList L) { return (L.length); } /* 用e返回L中第i个元素的值 */ int GetElem(SqList L, int i, ElemType &e) { if (0 == L.length) { return RETURN_ERROR; } if (ListLength(L) < i || i < 0) { return RETURN_ERROR; } e = L.elem[i - 1]; return RETURN_OK; } /* 判定函数 */ bool compare(ElemType a, ElemType b) { return (a > b) ? true : false; } /* 返回第一个与e满足关系compare()的数据元素的位序,若这样的数据元素不存在,则返回值为0 */ int LocateElem(SqList L, ElemType e, void *fun(ElemType, ElemType)) { int i; for (i = 0; i < L.length; i++) { if (fun(e, L.elem[i])) { return i; } } return 0; } /* 若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义 */ int PriorElem(SqList L, ElemType cur_e, ElemType &pre_e) { int i; if (0 == L.length) { return RETURN_ERROR; } for (i = 0; i < L.length; i++) { if (cur_e == L.elem[i]) { if (0 != i) { pre_e = L.elem[i - 1]; return RETURN_OK; } } } return RETURN_ERROR; } /* 若cur_e是L的数据元素, 且不是最后一个,则next_e返回它的后继,否则操作失败,next_e无定义 */ int NextElem(SqList L, ElemType cur_e, ElemType &next_e) { int i; if (0 == L.length) { return RETURN_ERROR; } for (i = 0; i < L.length; i++) { if (cur_e == L.elem[i]) { if (i != L.length - 1) { next_e = L.elem[i + 1]; return RETURN_OK; } } } return RETURN_ERROR; } /* 在L中第i个位置之前插入新的数据元素e,L的长度加1 */ int ListInsert(SqList &L, int i, ElemType e) { ElemType *newBase = NULL; ElemType *q = NULL; ElemType *p = NULL; if ((i < 1) || (i > L.length + 1)) { return RETURN_ERROR; } if (L.length >= L.listSize) { newBase = (ElemType *)realloc(L.elem, (L.listSize + LISTINCREMENT) * sizeof(ElemType)); if (NULL == newBase) { return RETURN_ERROR; } L.elem = newBase; L.listSize += LISTINCREMENT; } q = &(L.elem[i - 1]); for (p = &(L.elem[L.length - 1]); p >= q; --p) { *(p + 1) = *p; } *q = e; ++L.length; return RETURN_OK; } /* 删除L的第i个数据元素,并且用e返回其值,L的长度减1 */ int ListDelete(SqList &L, int i, ElemType &e) { ElemType *p = NULL; ElemType *q = NULL; if (i < 1 || i > L.length) { return RETURN_ERROR; } p = &(L.elem[i - 1]); e = *p; q = L.elem + L.length - 1; for (++p; p <= q; ++p) { *(p - 1) = *p; } --L.length; return RETURN_OK; } int _tmain(int argc, _TCHAR* argv[]) { SqList L; int i; /* 位置通用变量 */ int ret = RETURN_ERROR; ElemType e = 0; /* 线性表初始化 */ ret = InitList(L); if (RETURN_ERROR == ret) { return RETURN_ERROR; } /* 线性表数据插入 */ for (i = 1; i <= LIST_INIT_SIZE; i++) { ret = ListInsert(L, i, i); if (RETURN_ERROR == ret) { return ret; } } /* 线性表遍历 */ for (i = 1; i <= LIST_INIT_SIZE; i++) { ret = GetElem(L, i, e); if (RETURN_ERROR == ret) { return RETURN_ERROR; } printf("%d ", e); if (0 == i % 5) { printf("\n"); } } printf("list length = %d \n", ListLength(L)); ret = PriorElem(L, 5, e); printf("5 prior elem = %d\n", e); ret = NextElem(L, 5, e); printf("5 next elem = %d\n", e); ret = ListDelete(L, 5, e); printf("delete pos 5 elem = %d\n", e); /* 线性表获取指定位置的数据 */ GetElem(L, 2, e); printf("%d \n", e); /* 销毁线性表 */ DestoryList(L); return 0; }
相关文章推荐
- 数据结构基础8_循环队列(顺序实现)
- 数据结构 学习笔记之:顺序栈的C语言实现
- 【数据结构】顺序队列的实现(c++)
- 【数据结构】顺序队列 如何实现栈增长
- 【数据结构作业一】写出顺序表的结构体类型定义及查找、插入、删除算法,并以顺序表作存储结构,实现线性表的插入、删除
- 数据结构基础(6) --顺序栈的设计与实现
- 数据结构课程设计--数组(顺序实现)
- 数据结构——栈—— 顺序栈(附java实现)
- 数据结构之队列的顺序实现
- 数据结构顺序表的代码实现
- 数据结构之---c语言实现栈的顺序存储
- 一、数据结构基础之顺序表C语言实现
- 线性表的顺序表示和实现 - 数据结构
- 数据结构中静态顺序表的实现和增删查改。。。
- 1-java数据结构顺序表的实现
- 基础数据结构:顺序表、链表——Python实现
- 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)
- 数据结构教程 第六课 线性表的顺序表示和实现
- 数据结构(1)——顺序栈的实现,以及括号匹配的应用
- 第三周 数据算法实现与顺序结构程序设计的程序(一、二)(北理)