关于线性表的顺序表示与基本操作的实现
结构体定义与函数命名
SqList.h
//
// Created by Tsing on 2020/6/9.
//
#include <stdio.h>
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
#include "Status.h"
/* 宏定义 */
#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始分配量
#define LISTINCREMENT 10 // 顺序表存储空间的分配增量
typedef int ElemType;
typedef struct {
ElemType *elem; // 存储的数据类型
int length; // 使用的容量
int listsize; // 分配容量
} SqList;
/**
* 初始化线性表
* @param L
* @return
*/
Status InitList(SqList *L);
/**
* 销毁线性表
* @param L 要销毁的线性表
* @return 是否成功销毁
*/
Status DestroyList(SqList *L);
/**
* 清空线性表里的内容
* @param L 要被操作的线性表
* @return
*/
Status ClearList(SqList *L);
/**
* 判断线性表是否为空
* @param L 要被操作的线性表
* @return 是空 true ,非空 false
*/
Status ListEmpty(SqList L);
/**
* 获取线性表的长度
* @param L
* @return
*/
int ListLength(SqList L);
/**
*
* @param L
* @param i 需要获取元素的位置
* @param e 第i个元素的值,用地址类型带出函数
* @return 找到返回OK,否则返回ERROR。
*/
Status GetElem(SqList L, int i, ElemType *e);
/**
* 返回顺序表中首个与e满足Compare关系的元素位序。
* @param L
* @param e 元素e
* @param Compare 要满足的关系,需要自己实现Compare(ElemType ElemType) 参数一个是元素e,另一个是顺序表中的元素
* @return
*/
int LocateElem(SqList L, ElemType e, Status (Compare)(ElemType ElemType));
/**
* 获取元素 cur_e 的前驱
* @param L
* @param cur_e
* @param pre_e 前驱存储位置
* @return 存在返回OK,不存在返回ERROR。
*/
Status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e);
/**
* 获取元素 cur_e的后继
* @param L
* @param cur_e
* @param next_e 后继存储位置
* @return 存在返回OK,不存在返回ERROR。
*/
Status NextElem(SqList L, ElemType cur_e, ElemType *next_e);
/**
* 在顺序表第i个位置上插入e
* @param L
* @param i 位置
* @param e 要插入的元素
* @return 成功返回OK,否则返回ERROR。
*/
Status ListInsert(SqList* L, int i, ElemType e);
/**
* 删除顺序表第i个位置上的元素,并用e返回其值,长度减一。
* @param L
* @param i 位置
* @param e
* @return
*/
Status ListDelete(SqList *L, int i, ElemType *e);
/**
* 用Visit()操作元素
* @param L
* @param Visit 操作函数Visit(ElemType) 参数被操作的元素
*/
void ListTraverse(SqList L, void (Visit)(ElemType));
实现代码
SqList.h
//
// Created by Tsing on 2020/6/9.
//
#include "SqList.h"
Status InitList(SqList *L){
L->elem = (ElemType *) malloc(sizeof(ElemType) * LIST_INIT_SIZE);
if(L->elem == NULL) exit(OVERFLOW); // 内存分配失败
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
Status DestroyList(SqList *L){
if(L == NULL || L->elem == NULL) {
return ERROR;
}
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
return OK;
}
Status ClearList(SqList *L){
if(L == NULL || L->elem == NULL) {
return ERROR;
}
L->length = 0;
return OK;
}
Status ListEmpty(SqList L){
return L.length == 0 ? TRUE : FALSE;
}
int ListLength(SqList L){
return L.length;
}
Status GetElem(SqList L, int i, ElemType *e){
if(i < 1 || i > L.length) {
return ERROR;
}
*e = L.elem[i];
return OK;
}
int LocateElem(SqList L, ElemType e, Status (Compare)(ElemType, ElemType)){
for (int i = 0; i < L.length; ++i) {
if (Compare(e,L.elem[i])) return 1;
}
return 0;
}
Status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e){
int i = 0;
if(L.elem == NULL || L.length < 2) {
return ERROR;
}
while (L.elem[i] != cur_e && i<L.length){
i++;
}
if(i == 0) {
return ERROR;
}
*pre_e = L.elem[i-1];
return OK;
}
Status NextElem(SqList L, ElemType cur_e, ElemType *next_e){
int i = 0;
if(L.elem == NULL || L.length < 2) {
return ERROR;
}
while (L.elem[i] != cur_e && i<L.length){
i++;
}
if(i >= L.length-1) {
return ERROR;
}
*next_e = L.elem[i+1];
return OK;
}
Status ListInsert(SqList* L, int i, ElemType e){
if(L == NULL || L->elem == NULL) {
return ERROR;
}
if(i < 0 || i > L->length) {
return ERROR;
}
return OK;
}
Status ListDelete(SqList *L, int i, ElemType *e){
if(L == NULL || L->elem == NULL) {
return ERROR;
}
if(i < 0 || i > L->length) {
return ERROR;
}
(*e) = L->elem[i];
for (int j = i; j < L->length; ++j) {
L->elem[j] = L->elem[j+1];
}
L->length--;
return OK;
}
void ListTraverse(SqList L, void (Visit)(ElemType)){
for (int i = 0; i < L.length; ++i) {
Visit(L.elem[i]);
}
}
int EQ(ElemType e1, ElemType e2){
return e1 == e2 ? TRUE: FALSE;
}
void Print(ElemType elemType){
printf("%3d",elemType);
}