数据结构C++实现,线性表顺序存储结构,后期不断更新
头文件如下
#pragma once
#define OK 1 // 操作成功
#define ERROR 0 // 操作失败
#define MAXSIZE 10 // 线性表能达到的最大长度
typedef int Status; // 返回操作的执行状态
typedef int ElemType; // 线性表中的元素类型
// 线性表声明
typedef struct
{
ElemType* elem; // 存储空间的基地址
int length; // 当前长度
} SqList;
bool Expand(SqList&, int = 2); // 扩容操作声明
// 线性表的初始化,空表但已经分配地址空间
Status InitSqList(SqList& L)
{
L.elem = new ElemType[MAXSIZE]; // 为线性表分配一个大小为MAXSIZE的数组空间
if (!L.elem) // 空间分配失败
{
cout << "存储分配失败,结束运行" << endl;
exit(-1);
}
L.length = 0; // 空表长度为0
return OK;
}
// 用数组填充线性表
Status Create(SqList& L, ElemType source[], int n)
{
int m = 1;
while (m * MAXSIZE < n) {
m *= 2;
if (!Expand(L, m)) {
return ERROR;
}
} // 数组的长度可能大于maxsize要先扩容,直到满足要求;
for (int i = 0; i < n; i++) {
L.elem[i] = source[i];
}
L.length = n;
return OK;
}
// 销毁线性表
void Destory(SqList& L)
{
if (L.elem)
{
delete[]L.elem;
}
}
// 清空线性表
void Clear(SqList& L)
{
L.length = 0;
}
//判断线性表是否为空
bool isEmpty(SqList& L)
{
if (L.length == 0)
{
return true;
}
else
{
return false;
}
}
// 获取第i个元素的前驱节点
Status PriorElem(SqList& L, int i, ElemType& e)
{
if (i <= 1 || i > L.length)
{
return ERROR; // 没有前驱节点或者越界
}
e = L.elem[i - 2];
return OK;
}
// 获取第i个元素的后继节点
Status NextElem(SqList& L, int i, ElemType& e)
{
if (i < 1 || i >= L.length)
{
return ERROR; // 没有后继节点或者越界
}
e = L.elem[i];
return OK;
}
// 获取第i个元素
Status GetElem(SqList& L, int i, ElemType& e)
{
if (i < 1 || i > L.length)
return ERROR; // 判断i值是否合理,若不合理,返回ERROR
e = L.elem[i - 1]; // elem[i-1]单元存储第i个数据元素
return OK;
}
// 查找元素e的位置(第一次出现)
int Inquire(SqList& L, ElemType e)
{
//线性表的查找
for (int i = 0; i < L.length; i++) {
if (L.elem[i] == e) {
return i + 1; //查找成功,返回序号i+1
}
}
return -1; //查找失败,返回-1
}
// 在线性表i处插入元素
Status Insert(SqList& L, int i, ElemType e)
{
//在线性表L中第i个位置之前插入新的元素e
// i值的合法范围是1<=i<=L.length+1
if ((i < 1) || (i > L.length + 1))
{
return ERROR; // i值不合法,越界
}
if (L.length == MAXSIZE)
{
Expand(L); //当前存储空间已满,进行扩容
}
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;
}
// 线性表的删除
Status Delete(SqList& L, int i)
{
//在线性表L中删除第i个元素,并用e返回其值
// i值的合法范围是1<=i<=L.length
if ((i < 1) || (i > L.length))
{
return ERROR; // i值不合法
}
for (int j = i; j < L.length; j++)
L.elem[j - 1] = L.elem[j]; //被删除元素之后的元素前移
--L.length; //表长减1
return OK;
}
// 线性表倒置
Status Reverse(SqList& L)
{
for (int i = 0; i < L.length / 2; i++) // 颠倒元素
{
ElemType temp = L.elem[i];
L.elem[i] = L.elem[L.length - i - 1];
L.elem[L.length - i - 1] = temp;
}
return OK;
}
// 加倍式扩容操作
bool Expand(SqList& L, int m)
{
ElemType* new_L = new ElemType[MAXSIZE * m];
if (!new_L)
{
cout << "扩容失败" << endl;
return false; // 扩容失败
}
for (int i = 0; i < L.length; i++)
{
new_L[i] = L.elem[i];
}
delete[] L.elem;
L.elem = new_L;
return true;
}
// 遍历所有元素
void Traverse(SqList& L)
{
for (int i = 0; i < L.length; i++) {
cout << L.elem[i] << '\t';
}
cout << endl;
}
// 冒泡排序
void BubbleSort(SqList& L) {
int m = L.length - 1;
bool flag = true;
while (m > 0 && flag)
{
flag = false; //判断是否已经排好,方便提前退出。
for (int j = 0; j < m; j++)
if (L.elem[j] > L.elem[j + 1])
{
flag = true;
int temp = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = temp;
}
m--;
}
}
main函数
#include <iostream>
using namespace std;
#include "SqList.h"
int main()
{
ElemType test[] = { 3,2,7,56,81,42,95,63,88,74,15,22 };
SqList L;
ElemType e=0;
InitSqList(L);// 初始化线性表
Create(L, test, sizeof(test) / sizeof(ElemType));//用数组给线性表赋值
Traverse(L);// 遍历所有元素
cout << L.length << endl; // 获取线性表长度
cout << "------------------------------------------------------\n";
ElemType a = 2;
ElemType b = 3;
Insert(L, 1, a);
Insert(L, L.length + 1, b);
Traverse(L);// 遍历所有元素
cout << L.length << endl; // 获取线性表长度
cout << "------------------------------------------------------\n";
BubbleSort(L);// 插入排序
Traverse(L);// 遍历所有元素
cout << "------------------------------------------------------\n";
PriorElem(L, 1, e);// 查找前驱节点
cout << e << endl;
PriorElem(L, 2, e);
cout << e << endl;
cout << "------------------------------------------------------\n";
NextElem(L, L.length, e);// 查找后继节点
cout << e << endl;
NextElem(L, L.length-1, e);
cout << e << endl;
cout << "------------------------------------------------------\n";
Delete(L, 1);// 删除第一个元素
Traverse(L);// 遍历所有元素
cout << L.length << endl; // 获取线性表长度
cout << "------------------------------------------------------\n";
Reverse(L); //倒置;
Traverse(L);// 遍历所有元素
return 0;
}
参考书目《数据结构与算法第二版 严蔚敏》