目录
一、顺序表的基本定义代码
#include<stdio.h>
#include<stdlib.h>
//初始化 插入 删除 逆转 输出 销毁 置空表 求表长 查找元素 判断线性表是否为空
//常量标识符定义
#define OK 1
#define ERROR 0 //函数结果状态代码
#define OVERFLOW -2 //OVERFLOW 溢出 定义为 -2
constexpr auto LIST_INIT_SIZE = 100; //宏定义 线性表存储空间的初始分配量
constexpr auto LISTINCREMENT = 10; //线性表存储空间的分配增量
typedef int Status; //将int 重新定义为 Status (状态)
typedef int ElemType; //将int 重新定义为 ElemType
typedef struct //结构体定义
{
ElemType* elem; //存储空间基址 element 元素
int length; //当前长度
int size; //当前存储容量 以sizeof(ElemType)为单位
}SqList;
二、顺序表的各个功能代码
程序要具在一定的健壮性,注意判断合法性。
//初始化
Status InitList(SqList& L) //初始化 构造一个空的List
{
//L.elem = new ElemType[LIST_INIT_SIZE]; //为顺序表分配空间
L.elem = (int*)malloc(sizeof(ElemType) * LIST_INIT_SIZE); //为顺序表分配空间
if (!L.elem) exit(OVERFLOW); //存储分配失败
L.length = 0; //空表长度为0
L.size = LIST_INIT_SIZE; //初始存储容量
return OK;
}
//插入
Status ListInsert(SqList& L, int i, ElemType e) //插入 在L中第i位插入e
{
if (i<1 || i>L.length + 1) 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; //表长加1
return OK;
}
//删除
Status ListDelete(SqList &L, int i,ElemType e) //删除 删除L中第i位置的元素,并用e返回
{
if (L.length == LIST_INIT_SIZE) return ERROR; //判断合法性
if (i<1 || i>L.length+1) return ERROR;
e = L.elem[i-1]; //把被删的对象保留在e中
for (int j = i; j <= L.length-1; j++) //从后往前检索
{
L.elem[j -1] = L.elem[j]; //删除位置及之后的元素前移
L.length -= 1; //表长减1
return OK;
}
}
//逆转
void Reverse(SqList& L) //逆转List
{
int n = L.length / 2;
for (int i = 0; i <= n; i++)
{
int temp = L.elem[i];
L.elem[i] = L.elem[L.length -i-1];
L.elem[L.length - 1 - i] = temp;
}
}
//输出元素
Status GetElem(SqList &L,int i, int &e) //输出 输出L中第i个位置的元素
{
if (i<1 || i>L.length + 1) return ERROR; //判断合法性
e = L.elem[i-1];
return OK;
}
//输出表
void PrintList(SqList L) //输出 输出整个顺序表
{
for (int i = 0; i < L.length; i++)
printf("%d\n", L.elem[i]);
}
//销毁
void DestroyList(SqList& L) //销毁List
{
if (L.elem)
{
delete[] L.elem;
L.elem = NULL;
}
L.length = 0;
L.size = 0;
}
//置空表
void ClearList(SqList& L) //置空表 将List重置为空表
{
L.length = 0; //将线性表的长度置为0
}
//求表长
int GetLength(SqList& L) //求表长
{
return L.length;
}
//查找元素
int LocateElem(SqList L, ElemType e)
{
for (int i = 0; i < L.length; i++)
if (L.elem[i] == e)
return i + 1;
return 0;
}
//判断线性表是否为空
int IsEmpty(SqList L) //判断List是否为空
{
if (L.length == 0)
return OK;
else
return ERROR;
}
三、主函数代码
主函数的代码可以按功能随意编写。
int main()
{
SqList L;
int e=0;
printf("初始化L\n");
InitList(L);
printf("ListEmpty(L)=%d\n", IsEmpty(L));
for (int i = 1; i < 11; i++) { //插入元素
printf("L的位置%d插入元素%d\n", i, 10 * i);
ListInsert(L, i, 10 * i);
}
printf("L: \n");
PrintList(L);
printf("ListEmpty(L)=%d\n", IsEmpty(L)); //判断线性表L是否为空
GetElem(L, 5, e);
printf("L的第5个元素:%d\n", e);
printf("删除第5个元素\n");
ListDelete(L, 5, e);
printf("L: \n");
PrintList(L);
printf("ListLength(L)=%d\n", GetLength(L)); //输出表长
printf("逆转顺序表\n"); //逆序输出
Reverse(L);
printf("L: ");
PrintList(L);
printf("取2和9位置上的元素的值并输出:\t");
printf("%d %d\n", L.elem[2], L.elem[9]);
printf("L: \n");
PrintList(L);
printf("第1个值为30的元素的逻辑序号:%d\n", LocateElem(L, 30));
printf("销毁L\n");
DestroyList(L);
return 0;
}
四、程序运行结果