顺序表:用一组地址连续的存储单元一次存储线性表的数据元素,这种表示也称作线性表的顺序存储结构或顺序映像
其特点是逻辑上相邻的数据元素,其物理次序也是相邻的
谨以此文记录顺序表的实现:
#include <iostream>
using namespace std;
typedef int ElemType;
const int MAXSIZE = 10000;
typedef struct
{
ElemType *elem;
int length;
bool isInit = false;
} SqList;
void printUI()
{
cout << "1----初始化一个线性表" << endl;
cout << "2----销毁线性表" << endl;
cout << "3----清空线性表" << endl;
cout << "4----判断线性表是否为空" << endl;
cout << "5----求线性表长度" << endl;
cout << "6----获取线性表指定位置元素" << endl;
cout << "7----求前驱" << endl;
cout << "8----求后继" << endl;
cout << "9----在线性表指定位置插入元素" << endl;
cout << "10---删除线性表指定位置元素" << endl;
cout << "11---显示线性表" << endl;
cout << "12---查询指定元素首次出现位置" << endl;
cout << " 退出,输入一个负数!" << endl;
}
/**
* @brief 初始化线性表,成功返回true,否则false
*
* @param L
* @return true
* @return false
*/
bool InitList(SqList &L)
{
L.elem = new ElemType[MAXSIZE];
L.isInit = true;
if (!L.elem)
return false;
L.length = 0;
return true;
}
/**
* @brief 销毁线性表,成功返回true,否则false
*
* @param L
* @return true
* @return false
*/
bool DestroyList(SqList &L)
{
if (L.elem)
delete[] L.elem;
else
return false;
L.length = 0;
L.elem = nullptr;
return true;
}
/**
* @brief 清空线性表
*
* @param L
*/
void ClearList(SqList &L)
{
L.length = 0;
}
/**
* @brief 判断线性表是否为空
*
* @param L
* @return true
* @return false
*/
bool IsEmpty(SqList L)
{
if (L.length == 0)
return true;
else
return false;
}
/**
* @brief 获取线性表长度
*
* @param L
* @return int
*/
int GetLength(SqList L)
{
return L.length;
}
/**
* @brief 获取线性表L位置i的元素保存到e
*
* @param L
* @param i
* @param e
* @return true
* @return false
*/
bool GetElem(SqList L, int i, ElemType &e)
{
if (i < 1 || i > L.length || !L.isInit)
return false;
e = L.elem[i - 1];
return true;
}
/**
* @brief 在线性表L的i位置插入元素e
*
* @param L
* @param i
* @param e
* @return true
* @return false
*/
bool ListInsert_Sq(SqList &L, int i, ElemType e)
{
if (i < 1 || i > L.length + 1 || !L.isInit)
return false;
if (L.length == MAXSIZE)
return false;
for (int j = L.length - 1; j >= i - 1; j--)
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;
L.length++;
return true;
}
/**
* @brief 删除线性表L位置i的元素
*
* @param L
* @param i
* @return true
* @return false
*/
bool ListDelete_Sq(SqList &L, int i)
{
if (i < 1 || i > L.length || !L.isInit)
return false;
for (int j = i; j < L.length; j++)
L.elem[j - 1] = L.elem[j];
L.length--;
return true;
}
/**
* @brief 显示线性表L所有元素
*
* @param L
*/
void ShowSq(SqList L)
{
if (!L.isInit)
return;
for (int i = 0; i < L.length; i++)
cout << L.elem[i] << " ";
cout << endl;
}
/**
* @brief 定位元素e,返回其位置,不存在返回0
*
* @param L
* @param e
* @return int
*/
int LocateElem(SqList L, ElemType e)
{
if (!L.isInit)
return 0;
for (int i = 0; i < L.length; i++)
if (L.elem[i] == e)
return i + 1;
return 0;
}
/**
* @brief 交集
*
* @param a
* @param b
* @param res
*/
void JoinSq(SqList a, SqList b, SqList &res)
{
for (int i = 1; i <= a.length; i++)
{
ElemType e;
if (GetElem(a, i, e))
{
if (LocateElem(b, e))
ListInsert_Sq(res, res.length + 1, e);
}
}
}
/**
* @brief 并集
*
* @param a
* @param b
* @param res
*/
void UnionSq(SqList a, SqList b, SqList &res)
{
ElemType e;
for (int i = 1; i <= a.length; i++)
{
if (GetElem(a, i, e))
ListInsert_Sq(res, res.length + 1, e);
}
for (int i = 1; i <= b.length; i++)
{
if (GetElem(b, i, e))
{
if (!LocateElem(res, e))
ListInsert_Sq(res, res.length + 1, e);
}
}
}
/**
* @brief 补集
*
* @param a
* @param b
* @param res
*/
void DiffSq(SqList a, SqList b, SqList &res)
{
SqList unionList, joinList;
InitList(unionList);
InitList(joinList);
UnionSq(a, b, unionList);
JoinSq(a, b, joinList);
for (int i = 1; i <= unionList.length; i++)
{
ElemType e;
if (GetElem(unionList, i, e))
{
if (!LocateElem(joinList, e))
ListInsert_Sq(res, res.length + 1, e);
}
}
}
int main()
{
int mode, i;
ElemType e;
SqList L;
printUI();
while (true)
{
cout << "请输入操作代码:";
cin >> mode;
if (mode < 0)
break;
if (mode != 1 && !L.isInit)
{
cout << "线性表未初始化" << endl;
continue;
}
switch (mode)
{
case 1:
if (InitList(L))
cout << "初始化成功" << endl;
else
cout << "初始化失败" << endl;
break;
case 2:
if (DestroyList(L))
cout << "销毁成功" << endl;
else
cout << "销毁失败" << endl;
break;
case 3:
ClearList(L);
cout << "清空成功" << endl;
break;
case 4:
if (IsEmpty(L))
cout << "线性表为空" << endl;
else
cout << "线性表不为空" << endl;
break;
case 5:
cout << "线性表长度为" << GetLength(L) << endl;
break;
case 6:
cout << "请输入指定位置:";
cin >> i;
if (GetElem(L, i, e))
cout << "位置" << i << "的元素为" << e << endl;
else
cout << "下标不合法" << endl;
break;
case 7:
cout << "请输入指定位置:";
cin >> i;
if (GetElem(L, i - 1, e))
cout << "位置" << i << "的前驱为" << e << endl;
else
cout << "下标不合法" << endl;
break;
case 8:
cout << "请输入指定位置:";
cin >> i;
if (GetElem(L, i + 1, e))
cout << "位置" << i << "的后继为" << e << endl;
else
cout << "获取失败" << endl;
break;
case 9:
cout << "请输入插入位置:";
cin >> i;
cout << "请输入插入元素:";
cin >> e;
if (ListInsert_Sq(L, i, e))
cout << "插入成功" << endl;
else
cout << "线性表已满或下标不合法" << endl;
break;
case 10:
cout << "请输入删除位置:";
cin >> i;
if (ListDelete_Sq(L, i))
cout << "删除成功" << endl;
else
cout << "删除失败" << endl;
break;
case 11:
ShowSq(L);
break;
case 12:
cout << "请输入所查元素:";
cin >> e;
i = LocateElem(L, e);
if (i)
cout << "该元素首次出现位置为" << i << endl;
else
cout << "元素不存在" << endl;
break;
}
}
// SqList a, b, res;
// InitList(a);
// InitList(b);
// InitList(res);
// for (int i = 1; i < 5; i++)
// ListInsert_Sq(a, i, i);
// for (int i = 3; i < 9; i++)
// ListInsert_Sq(b, i - 2, i);
// ShowSq(a);
// ShowSq(b);
// UnionSq(a, b, res); //验证并集操作
// JoinSq(a, b, res); //验证交集操作
// DiffSq(a, b, res); //验证补集操作
// ShowSq(res);
return 0;
}