仅供参考
#include <iostream>
using namespace std;
// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
// Status 是函数的类型 其值是函数结果状态代码
typedef int Status;
typedef char ElemType;
typedef struct {
ElemType *elem;
int length;
}Sqlist;
// 初始化操作,建立一个空的线性表L
Status InitList(Sqlist &L);
// 销毁已存在的线性表L
void DestroyList(Sqlist &L);
// 将线性表清空
void ClearList(Sqlist &L);
// 在线性表L中的第i个位置插入新元素e
Status ListInsert(Sqlist &L, int i, ElemType e);
// 删除线性表L中第i个位置元素,用e返回
Status ListDelete(Sqlist &L, int i, ElemType &e);
// 若线性表为空,返回true,否则false
int IsEmpty(Sqlist L);
// L中查找与给定值e相等的元素,若成功返回该元素在表中的序号,否则返回0;
int LocateElem(Sqlist L, ElemType e);
// 将线性表L中的第i个位置元素返回给e
Status GetElem(Sqlist L, int i, ElemType &e);
// 求线性表的长度
int GetLength(Sqlist L);
// 遍历线性表
void ListTraversals(Sqlist L);
///
int main() {
// 创建一个线性表L
int ret = 0;
Sqlist L;
ret = InitList(L);
if (ret == OK) cout << "创建线性表L成功" << endl;
// 插入线性表
ret = ListInsert(L, 1, 11);
if (ret == OK) cout << "插入线性表L成功" << endl;
ret = ListInsert(L, 2, 22);
ret = ListInsert(L, 3, 33);
// 遍历线性表
cout << "遍历线性表: ";
for (int i = 0; i < L.length; ++i) {
cout << (int)L.elem[i] << " ";
}
cout << endl;
// 求线性表长度
cout << "线性表的长度:" << GetLength(L) << endl;
// 删除线性表L中第i个位置元素,用e返回
ElemType e;
ret = ListDelete(L, 1 , e);
cout << "被删除的元素:" << (int)e << endl;
cout << "被删除后线性表的长度:" << GetLength(L) << endl;
// L中查找与给定值e相等的元素,若成功返回该元素在表中的序号,否则返回0;
e = 33;
ret = LocateElem(L, e);
cout << "该元素 " << (int)e << " 在表中序号: " << ret << endl;
// 将线性表L中的第i个位置元素返回给e
ret = GetElem(L, 1, e);
cout << "第一个位置元素:" << (int)e << endl;
// 将线性表清空
ClearList(L);
cout << "清空线性表" << endl;
// 若线性表为空,返回true,否则false
ret = IsEmpty(L);
if (ret == TRUE) cout << "线性表为空" << endl;
else cout << "线性表为非空" << endl;
// 销毁已存在的线性表L
DestroyList(L);
cout << "线性表已销毁" << endl;
return 0;
}
//
// 构造一个空的顺序表
Status InitList(Sqlist &L){
L.elem = new ElemType[MAXSIZE]; // 为顺序表分配空间
if (!L.elem) exit(OVERFLOW); // 存储分配失败
L.length = 0; // 空表长度0
return OK;
}
void DestroyList(Sqlist &L){
if (L.elem) delete L.elem; // 释放存储空间;
L.elem = nullptr;
L.length = 0;
}
// 将线性表清空
void ClearList(Sqlist &L){
L.length = 0; // 讲线性表的长度置0
}
// 求线性表的长度
int GetLength(Sqlist L){
return L.length;
}
// 判断线性表是否为空
int IsEmpty(Sqlist L){
if (L.length == 0) return TRUE;
else return FALSE;
}
// 将线性表L中的第i个位置元素返回给e
Status GetElem(Sqlist L, int i, ElemType &e){
if (i < 1 || i > L.length) return ERROR; // 判断是否合理
e = L.elem[i-1]; // 第i-1的单元存储着第i个数据
return OK;
}
// L中查找与给定值e相等的元素,若成功返回该元素在表中的序号,否则返回0;
int LocateElem(Sqlist L, ElemType e){
for (int i = 0; i < L.length; ++i) {
if (L.elem[i] == e) return i+1;
}
return 0;
}
// 在线性表L中的第i个位置插入新元素e
Status ListInsert(Sqlist &L, int i, ElemType e){
if (i < 1 || i > L.length + 1) return ERROR; // 判断i值是否合法
if (L.length == MAXSIZE) return ERROR; // 当前存储空间已满
for (int j = L.length -1 ; j >= i -1 ; --j) {
L.elem[j+1] = L.elem[j]; // 插入位置及之后的元素后移
}
L.elem[i-1] = e; // 将新元素e放入第i个位置
L.length++; // 表长+1
return OK;
}
// 删除线性表L中第i个位置元素,用e返回
Status ListDelete(Sqlist &L, int i, ElemType &e){
if (i < 1 || i > L.length) return ERROR;
e = L.elem[i-1];
for (int j = i; j <= L.length -1 ; ++j) {
L.elem[j-1] = L.elem[j];
}
L.length--;
return OK;
}
// 遍历线性表
void ListTraversals(Sqlist L){
for (int i = 0; i < L.length; ++i) {
cout << L.elem[i] << endl;
}
}