顺序表的12种基本操作
定义常量
#include
#include
#include
#include
#include
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE 0
typedef int Status;
结构体定义与函数声明
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef struct
{
ElemType* elem;
int length;
int listsize;
}SqList;
Status InitList(SqList* L);
Status DestoryList(SqList* L);
Status ClearList(SqList* L);
Status ListEmpty(SqList L);
int ListLength(SqList L);
Status GetElem(SqList L, int i, ElemType* e);
int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType));
Status PriorElem(SqList L, ElemType cur_e, ElemType* pre_e);
Status NextElem(SqList L, ElemType cur_e, ElemType* next_e);
Status ListInsert(SqList* L, int i, ElemType e);
Status ListDelete(SqList* L, int i, ElemType *e);
Status ListTraverse(SqList L, Status (*visit)(ElemType*));
Status compare(ElemType x, ElemType y);
Status visit(ElemType *);
函数定义
Status InitList(SqList* L)
{
L->elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L->elem) exit(-1);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
Status DestoryList(SqList* L)
{
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
return OK;
}
Status ClearList(SqList* L)
{
L->length = 0;
return OK;
}
Status ListEmpty(SqList L)
{
if (L.length == 0) return TRUE;
else return FALSE;
}
int ListLength(SqList L)
{
return L.length;
}
Status GetElem(SqList L, int i, ElemType* e)
{
if (i<1 || i>L.length) exit(ERROR);
*e = *(L.elem + i - 1);
return OK;
}
int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType))
{
int i = 1;
ElemType* p = L.elem;
while (i <= L.length && !(*compare)(e, *p))
{
p++;
i++;
}
if (i <= L.length) return i;
else return 0;
}
Status PriorElem(SqList L, ElemType cur_e, ElemType* pre_e)
{
int i = 2;
ElemType* p = L.elem + 1;
while (i <= L.length && !(*p == cur_e))
{
i++;
p++;
}
if (i > L.length)
return INFEASIBLE;
else
{
*pre_e = *(--p);
return OK;
}
}
Status PriorElem(SqList L, ElemType cur_e, ElemType* pre_e)
{
ElemType* p = L.elem;
ElemType* q;
while (p + 1)
{
q = p + 1;
if (*q == cur_e)
{
*pre_e = *p;
return OK;
}
p++;
}
return ERROR;
}
Status NextElem(SqList L, ElemType cur_e, ElemType* next_e)
{
int i = 1;
ElemType* p = L.elem;
while (i <= L.length - 1 && !(*p == cur_e))
{
i++;
p++;
}
if (i > L.length - 1)
return INFEASIBLE;
else
{
*next_e = *(++p);
return OK;
}
}
Status NextElem(SqList L, ElemType cur_e, ElemType *next_e)
{
ELemType *p = L.elem;
while(p+1)
{
if(*p == cur_e)
{
*next_e = *p;
return OK;
}
p++;
}
return ERROR;
}
Status ListInsert(SqList* L, int i, ElemType e)
{
if (i<1 || i>ListLength(*L) + 1) return ERROR;
if (L->length >= L->listsize)
{
ElemType* newbase;
newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
if (NULL == newbase)
{
printf("空间分配失败!\n");
exit(-1);
}
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
ElemType* q, * p;
q = L->elem+i-1;
p = L->elem+L->length-1;
for (; p >= q; p--)
*(p + 1) = *p;
*q = e;
L->length++;
return OK;
}
Status ListDelete(SqList* L, int i, ElemType *e)
{
if (i<1 || i>L->length) return ERROR;
ElemType *q, *p;
q = L->elem+i-1;
p = L->elem+L->length-1;
*e = *q;
for (q++; q <= p; q++)
* (q - 1) = *q;
L->length--;
return OK;
}
Status ListTraverse(SqList L, Status (*visit)(ElemType*))
{
ElemType* p = L.elem;
int i;
for(i=1; i<=ListLength(L); i++)
visit(p++);
printf("\n");
return OK;
}
测试
#include "SqList.cpp"
int main()
{
SqList L;
if (InitList(&L)) printf("创建成功!\n");
else printf("创建失败!\n");
printf("表长为:%d\n", ListLength(L));
if (ListEmpty(L)) printf("表空!\n");
else printf("非空!\n");
ElemType i;
for (i = 1; i <= 20; i++)
{
if (ListInsert(&L, 1, i)) printf("头插入成功:%d\n", i);
else printf("头插入失败!\n");
}
if (ListEmpty(L)) printf("空!\n");
else printf("非空!\n");
printf("表长为:%d\n", ListLength(L));
ListTraverse(L, visit);
if (GetElem(L, 2, &i)) printf("取第2个元素:%d\n", i);
else printf("获取失败!\n");
int n = LocateElem(L, 2, compare);
if (i != 0) printf("与2相关的元素是第%d个\n", n);
else printf("定位失败!\n");
n = LocateElem(L, 9, compare);
if (n != 0) printf("与9相关的元素是第%d个\n", n);
else printf("定位失败!\n");
if (PriorElem(L, 2, &i)) printf("2的前元素:%d\n", i);
else printf("获取失败!\n");
if (PriorElem(L, 8, &i)) printf("8的前元素:%d\n", i);
else printf("获取失败!\n");
if (NextElem(L, 2, &i)) printf("2的后元素:%d\n", i);
else printf("获取失败!\n");
if (NextElem(L, 1, &i)) printf("1的后元素:%d\n", i);
else printf("获取失败!\n");
if (ListDelete(&L, 1, &i)) printf("删除第%d个元素:%d\n", 1, i);
else printf("删除失败!\n");
if (ListDelete(&L, 3, &i)) printf("删除第%d个元素:%d\n", 3, i);
else printf("删除失败!\n");
printf("表长为:%d\n", ListLength(L));
ListTraverse(L, visit);
if (ClearList(&L)) printf("清空成功!\n");
else printf("清空失败!\n");
if (ListEmpty(L)) printf("空!\n");
else printf("非空!\n");
printf("表长为:%d\n", ListLength(L));
ListTraverse(L, visit);
if (DestoryList(&L)) printf("销毁成功!\n");
else printf("销毁失败!\n");
return 0;
}
Status compare(ElemType x, ElemType y)
{
if (x == y)
return OK;
else
return ERROR;
}
Status visit(ElemType *e)
{
printf("%d ", *e);
return OK;
}
运行结果