您的位置:首页 > 理论基础 > 数据结构算法

数据结构(顺序表-实现)

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: