test.c
#include "SequenceList.h"
Status compare(LPElemType elem, LPElemType other); //元素比较函数,other > elem 返回 TRUE 否则FALSE
void PrintElem(LPElemType elem);
void main(void)
{
SequenceList list;
int i,ERR_CODE;
LPElemType elem;
printf("函数初始化测试 InitList...\n");
{
printf("初始化线性表...\n");
ERR_CODE = InitList(&list);
if (ERR_CODE != OK) {
printf("初始化失败\n");
}
else
{
printf("线性表容量:%d,现有元素个数:%d\n", list.capacity, list.count);
}
printf("\n\n");
}
PressEnter();
printf("函数ListEmpty判空测试...\n");
{
if (ListEmpty(list))
{
printf("线性表为空!!!\n");
}
else
{
printf("非空线性表!\n");
}
printf("\n\n");
}
PressEnter();
printf("函数ListInsert测试...\n");
{
for (i = 1; i < 6; i++)
{
printf("在线性表位序 %d 插入\"%d\"...\n", i, 2 * i);
ListInsert(&list, i, 2 * i);
}
printf("\n\n");
}
PressEnter();
printf("函数ListTraverse测试...\n");
{
printf("list中的元素为: list = ");
ListTraverse(list,PrintElem);
printf("\n\n");
}
PressEnter();
printf("函数ListLength测试...\n");
{
i = ListLength(list);
printf("list的长度为 %d\n", i);
printf("\n\n");
}
PressEnter();
printf("函数ListDelete测试...\n");
{
ListDelete(&list,6,&elem);
printf("删除 list 中第 6 个元素 \"%d\"\n", elem);
printf("list 中的元素为:list = ");
ListTraverse(list, PrintElem);
printf("\n\n");
}
PressEnter();
printf("函数GetElem测试...\n");
{
GetElem(list,4 ,&elem);
printf("list 中第 4个位置的元素为 \"%d\" \n", elem);
printf("\n\n");
}
PressEnter();
printf("函数LocateElem测试...\n");
{
i = LocateElem(list, 7 , compare);
printf("list 中第一个元素值大于\"7\"的元素的位置为 %d\n", i);
printf("\n\n");
}
PressEnter();
printf("函数PriorElem测试...\n");
{
PriorElem(list , 6 , &elem);
printf("元素 \"6\"的前驱为\"%d\"\n", elem);
printf("\n\n");
}
PressEnter();
printf("函数NextElem测试...\n");
{
NextElem(list, 6, &elem);
printf("元素 \"6\" 的后继为 \"%d\"\n", elem);
printf("\n\n");
}
PressEnter();
printf("函数ClearList测试...\n");
{
printf("清空list之前:");
if(ListEmpty(list))
{
printf("list为空!\n");
}else
{
printf("list不为空!\n");
}
ClearList(&list);
printf("清空 list 后:");
if(ListEmpty(list))
{
printf("list为空!\n");
}else{
printf("list不为空!\n");
}
printf("\n\n");
}
PressEnter();
printf("函数DestroyList测试...\n");
{
printf("销毁list前:\n");
if(list.elem)
{
printf("list存在!\n");
DestroyList(&list);
printf("销毁list后:");
if(list.elem)
{
printf("list存在\n");
}else
{
printf("list不存在\n");
}
}else
{
printf("list不存在!\n");
}
}
PressEnter();
}
Status compare(LPElemType elem, LPElemType other)
{
return other > elem ? TRUE : FALSE;
}
void PrintElem(LPElemType elem)
{
printf("%d ", elem);
}
SequenceList.h
//Capacity 容器容量
//Count 当前实际个数
#ifndef SEQUENCELIST_H
#define SEQUENCELIST_H
#include "stdio.h"
#include "stdlib.h" //提供malloc,realloc,free,exit原型 zmalloc
#include "Status.h"
/* 宏定义 */
#define LIST_INIT_SIZE 4 //顺序表存储空间的初始分配量
#define LIST_INCREMENT 2 //顺序表存储空间的分配增量
/* 顺序表类型定义 */
#ifndef LELEMTYPE_SQ
#define LELEMTYPE_SQ
typedef int LPElemType;
#endif
typedef struct
{
LPElemType *elem; //存储空间基址(指针首地址)
int count; //当前顺序表长度 count
int capacity; //当前分配的存储容量 capacity
} SequenceList,tag_List;
//初始化空序列表
Status InitList(SequenceList *list);
//清空顺序表
void ClearList(SequenceList *list);
//销毁顺序表
void DestroyList(SequenceList *list);
//判断顺序表是否为空
int ListEmpty(SequenceList list);
//返回顺序表的元素个数
int ListLength(SequenceList list);
//返回顺序表中第i个元素
Status GetElem(SequenceList list, int i, LPElemType *elem);
//返回中首个与e满足Compare关系的元素位序
int LocateElem(SequenceList list, LPElemType elem, Status(Compare)(LPElemType, LPElemType));
//返回cur_elem的前驱
Status PriorElem(SequenceList list, LPElemType cur_elem, LPElemType *pre_elem);
//返回cur_elem的后继
Status NextElem(SequenceList list, LPElemType cur_elem, LPElemType *next_elem);
//在顺序表的第i个位置插入e
Status ListInsert(SequenceList *list, int i, LPElemType elem);
//删除顺序表上第i个位置的元素,并用e返回
Status ListDelete(SequenceList *list, int i, LPElemType *elem);
//用visit函数访问顺序表
Status ListTraverse(SequenceList list, void(Visit)(LPElemType));
//union
//Merge
#endif
SequenceList.c
#ifndef SEQUENCELIST_C
#define SEQUENCELIST_C
#include "SequenceList.h"
Status InitList(SequenceList *list)
{
list->elem = (LPElemType*)malloc(LIST_INIT_SIZE * sizeof(LPElemType));
if (!list->elem)
{
exit(OVERFLOW); //分配内存失败
}
list->count = 0; //初始化顺序表长度为0
list->capacity = LIST_INIT_SIZE; //顺序表初始内存分配量
return OK; //初始化成功
}
void ClearList(SequenceList *list)
{
list->count = 0;
}
void DestroyList(SequenceList *list)
{
free(list->elem);
list->elem = NULL;
list->count = 0; //释放内存后置空指针
list->capacity = 0;
}
Status ListEmpty(SequenceList list)
{
return list.count == 0 ? TRUE : FALSE;
}
int ListLength(SequenceList list)
{
return list.count;
}
Status GetElem(SequenceList list, int i, LPElemType *elem)
{
if (i < 1 || i > list.count/*如果检索的索引超过实际元素的索引长度*/)
{
return ERROR; //i值不合法
}
else
{
*elem = list.elem[i - 1];
}
return OK;
}
int LocateElem(SequenceList list, LPElemType elem, Status(Compare)(LPElemType, LPElemType))
{
int i = 1; //i的初值为第一个元素的位序
while ( i < list.count && !Compare(elem, list.elem[i -1]) )
{
++i;
}
if (i <= list.count)
{
return i;
}
else
{
return 0;
}
}
Status PriorElem(SequenceList list, LPElemType cur_elem, LPElemType *pre_elem)
{
int i = 1;
if (list.elem[0] != cur_elem) //第一个节点无前驱
{
while ( i < list.count && list.elem[i] != cur_elem)
{
++i;
}
if (i < list.count)
{
*pre_elem = list.elem[i - 1];
return OK;
}
}
return ERROR;
}
Status NextElem(SequenceList list, LPElemType cur_elem, LPElemType *next_elem)
{
int i = 0;
while (i < list.count && list.elem[i] != cur_elem)
{
++i;
}
if (i < list.count - 1)
{
*next_elem = list.elem[i + 1];
return OK;
}
return ERROR;
}
Status ListInsert(SequenceList *list, int i, LPElemType elem)
{
LPElemType *newbase;
LPElemType *p, *q;
if (i < 1 || i > list->count + 1)
{
return ERROR;
}
if (list->count > list->capacity) //存储空间以满,需要扩容
{
newbase = (LPElemType*)realloc(list->elem, (list->capacity + LIST_INCREMENT) * sizeof(LPElemType));
if (!newbase)
{
exit(OVERFLOW);
}
list->elem = newbase;
list->capacity += LIST_INCREMENT;
}
q = &(list->elem[i - 1]); //q为插入位置
for (p = &(list->elem[list->count - 1]); p >= q; --p)
{
*(p + 1) = *p; //插入位置及之后的元素右移
}
*q = elem;
list->count++;
return OK;
}
Status ListDelete(SequenceList *list, int i, LPElemType *elem)
{
LPElemType *p = NULL, *q = NULL;
if (i < 1 || i > list->count)
{
return ERROR;
}
*p = list->elem[i - 1]; //p为被删除元素的位置
*elem = *p;
q = (*list).elem + (*list).count - 1; //表示元素的位置
for (++p; p <= q; ++p)
{
*(p - 1) = *p; //被删除元之后的元素左移
}
list->count--; //表长减去1
return OK;
}
Status ListTraverse(SequenceList list, void(Visit)(LPElemType))
{
int i;
for (i = 0; i < list.count; i++)
{
Visit(list.elem[i]);
}
return OK;
}
#endif
Status.h
#ifndef STATUS_H
#define STATUS_H
/*状态码*/
#define TRUE 1 //真
#define FALSE 0 //假
#define YES 1 //是
#define NO 0 //否
#define OK 1 //通过
#define ERROR 0 //错误
#define SUCCESS 1 //成功
#define UNSUCCESS 0 //失败
#define INFEASIBLE -1 //不可行
#ifndef _MATH_H_ //系统中已有此状态码定义,要避免冲突
#define OVERFLOW -2 //堆栈上溢
#define UNDERFLOW -3 //堆栈下溢
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
/* 状态码失败类型 */
typedef int Status;
/* 宏函数 */
//函数暂停一段时间
#define Wait(x)\
{\
double _Loop_Num_;\
for(_Loop_Num_=0.01; _Loop_Num_<=100000.0*x; _Loop_Num_+=0.01)\
;\
}//设立一个空循环
#define PressEnter()\
{\
fflush(stdin);\
printf("Press Enter...");\
getchar();\
fflush(stdin);\
}
#endif
vc++6.0编译运行效果