#include<iostream>
#include<cstdlib>
#include<cstdio>
#define INIT_SQLIST 100
#define ERROR -1
#define OK 1
#define OVER_FLOW -2
typedef int Elemtype;
typedef int Status;
typedef struct {
Elemtype* elemList;
int length;
}SqList;
Status InitSqList(SqList& L);//fun_1
Status DestroyList(SqList& L);//fun_2
Status ClearList(SqList& L);//fun_3
Status ListEmpty(SqList& L);//fun_4
Status ListLength(SqList& L);//fun_5
Status GetElem(SqList& L, int i, Elemtype& e);//fun_6
Status LocateElem(SqList& L, int e);//fun_7
Status PriorElem(SqList& L, int cur_e, Elemtype& pre_e);//fun_8
Status NextElem(SqList& L, int cur_e, Elemtype& next_e);//fun_9
Status ListInsert(SqList& L, int i, Elemtype e);//fun_10
Status ListDelete(SqList& L, int i);//fun_11
Status TraverseList(SqList& L);//fun_12
Status CreateSqList(SqList& L);//fun_beta_1
//Status SearchElem(SqList& L, int i);
Status MERGE_SORT(SqList& a, int p, int r);//fun_beat_2
Status SqList_Merge(SqList& LA, SqList& LB);//fun_beta_3
Status AddList(SqList& L, Elemtype n);//fun_beta_4
int main() {
Elemtype e, x;
int i, k;
SqList L;
L.elemList = 0;
do {
std::cout << "\n=========顺序表的操作===========";
std::cout << "\n 1.初始化顺序表";
std::cout << "\n 2.创建顺序表";
std::cout << "\n 3.在第i个位置前插入元素x";
std::cout << "\n 4.删除元素";
std::cout << "\n 5.查找指定元素位置";
std::cout << "\n 6.查找指定位置元素";
std::cout << "\n 7.遍历当前顺序表";
std::cout << "\n 8.顺序表合并";
std::cout << "\n 0.结束程序运行";
std::cout << "\n================================";
std::cout << "\n 请输入您的选择(1,2,3,4,5,6,7,0)";
std::cin >> k;
switch (k)
{
case 1:
if (InitSqList(L) == OK)
std::cout << "初始化成功!";
else
std::cout << "初始化失败!";
break;
case 2:
if (CreateSqList(L) == OK)
std::cout << "创建成功!";
else
std::cout << "创建失败!";
break;
case 3:
std::cout << "请输入插入元素的位置i及值x:";
std::cin >> i >> x;
if (ListInsert(L, i, x) == OK)
std::cout << "插入成功!";
else
std::cout << "插入失败!";
TraverseList(L);
break;
case 4:
std::cout << "请输入要删除元素的位置i:";
std::cin >> i;
ListDelete(L, i);
TraverseList(L);
break;
case 5:
std::cout << "请输入要查找指定元素e:";
std::cin >> e;
std::cout << "该元素的位置是:" << LocateElem(L, e) << std::endl;
break;
case 6:
std::cout << "请输入要查找元素的位置:";
std::cin >> i;
GetElem(L, i, e);
std::cout << "第" << i << "个元素值是:" << e << std::endl;
break;
case 7:
std::cout << "当前线性表中的元素为:";
TraverseList(L);
break;
case 8:
SqList LA,LB;
LA.elemList = 0;
LB.elemList = 0;
InitSqList(LA);
InitSqList(LB);
std::cout << "请创建顺序表LA:";
CreateSqList(LA);
std::cout << "请创建顺序表LB:";
CreateSqList(LB);
std::cout << "LB合并到LA中(重复元素不合并进去):";
SqList_Merge(LA, LB);
TraverseList(LA);
break;
case 0:
break;
default:
std::cout << "无效选项,请重新输入!" << std::endl;
}
} while (k != 0);
system("pause");
return 0;
}
Status InitSqList(SqList& L) {//fun_1
if (L.elemList) {
std::cerr << "请误重复申请内存空间" << std::endl;
return(ERROR);
}
L.elemList = (Elemtype*)malloc(INIT_SQLIST * sizeof(Elemtype));
if (!L.elemList) {
std::cerr << "申请内存空间失败" << std::endl;
exit(ERROR);
}
L.length = 0;
return OK;
}//fun_1
Status DestroyList(SqList& L) {//fun_2
if (!L.elemList) {
std::cerr << "要销毁的线性表不存在。" << std::endl;
return ERROR;
}
free(L.elemList);
L.length = -1;
return OK;
}//fun_2
Status ClearList(SqList& L) {//fun_3
if (!L.elemList) {
std::cerr << "要重置的线性表不存在。" << std::endl;
return ERROR;
}
L.length = 0;
return OK;
}//fun_3
Status ListEmpty(SqList& L) {//fun_4
if (!L.elemList) {
std::cerr << "要判断的线性表不存在。" << std::endl;
return ERROR;
}
if (!L.length) {
return true;
}
else {
return false;
}
}//fun_4
Status ListLength(SqList& L) {//fun_5
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
return L.length;
}//fun_5
Status GetElem(SqList& L, int i, Elemtype& e) {//fun_6
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
if (i > L.length || 0 > i) {
std::cerr << "输入的数字不合法。" << std::endl;
return ERROR;
}
e = L.elemList[i - 1];
return OK;
}//fun_6
Status LocateElem(SqList& L, int e) {//fun_7
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
for (int i = 0; i < L.length; ++i) {
if (L.elemList[i] == e) {
return i+1;
}
}
return 0;
}//fun_7
Status PriorElem(SqList& L, int cur_e, Elemtype& pre_e) {//fun_8
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
int temp_0 = 0;
for (int i = 0; i < L.length; ++i) {
if (L.elemList[i] == cur_e) {
temp_0 = i;
break;
}
}
if (temp_0) {
pre_e = L.elemList[temp_0];
}
return OK;
}//fun_8
Status NextElem(SqList& L, int cur_e, Elemtype& next_e) {//fun_8
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
int temp_0 = 0;
for (int i = 0; i < L.length; ++i) {
if (L.elemList[i] == cur_e) {
temp_0 = i;
break;
}
}
if (temp_0 != L.length -1) {
next_e = L.elemList[temp_0];
}
return OK;
}//fun_8
Status ListInsert(SqList& L, int i, Elemtype e) {//fun_9
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
if (1 > i && L.length < i) {
std::cerr << "输入的数字不合法。" << std::endl;
return ERROR;
}
if (L.length + 1 > INIT_SQLIST) {
std::cerr << "超出线性表申请的内存空间。" << std::endl;
return OVER_FLOW;
}
for (int n = L.length; n > i - 1; --n) {
L.elemList[n] = L.elemList[n - 1];
}
L.elemList[i - 1] = e;
++L.length;
return OK;
}//fun_9
Status ListDelete(SqList& L, int i) {//fun_10
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
if (!L.length) {
std::cerr << "要查询的线性表为空。" << std::endl;
return ERROR;
}
if (1 > i && L.length < i) {
std::cerr << "输入的数字不合法。" << std::endl;
return ERROR;
}
for (int n = i - 1; n < L.length - 1; ++n) {
L.elemList[n] = L.elemList[n + 1];
}
--L.length;
return OK;
}//fun_10
Status TraverseList(SqList& L) {//fun_11
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
std::cout << "以下是存储在线性表的内容:" << std::endl;
for (int i = 0; i < L.length; ++i) {
std::cout << L.elemList[i] << " " << std::ends;
}
std::cout << std::endl;
return OK;
}//fun_11
Status CreateSqList(SqList& L) {//fun_beta_1
if (!L.elemList) {
L.elemList = (Elemtype*)malloc(INIT_SQLIST * sizeof(Elemtype));
if (!L.elemList) {
std::cerr << "申请内存空间失败" << std::endl;
exit(ERROR);
}
}
std::cout << "请输入线性表的长度n:";
int i;
std::cin >> i;
if (i <= 0) {
std::cerr << "输入的数字不合法。" << std::endl;
return ERROR;
}
if (i > INIT_SQLIST) {
std::cerr << "输入的数字超出线性表申请的内存空间大小。" << std::endl;
return ERROR;
}
L.length = i;
std::cout << "请输入创建的线性表的各个元素。\n"<<"共"<<i<<"个元素:" << std::endl;
for (int n = 0; n < L.length; ++n) {
std::cin >> L.elemList[n];
}
return OK;
}//fun_beta_1
/*Status SearchElem(SqList& L, int i) {//fun_beta_2
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
for (int n = 0; n < L.length; ++n) {
if (L.elemList[n] == i) {
return n+1;
}
}
return 0;
}//fun_beta_2
*/
Status _MERGE(SqList& a, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int* L = (int*)malloc(sizeof(int) * (n1 + 1));
int* R = (int*)malloc(sizeof(int) * (n2 + 1));
for (int i = 0; i < n1; i++) {
L[i] = a.elemList[p + i - 1];
}
for (int i = 0; i < n2; i++) {
R[i] = a.elemList[q + i];
}
L[n1] = TMP_MAX;
R[n2] = TMP_MAX;
for (int i = 0; i <= n1; i++) {
printf("%d ", L[i]);
}
printf("\n");
for (int i = 0; i <= n2; i++) {
printf("%d ", R[i]);
}
printf("\n");
int i = 0;
int j = 0;
for (int k = p - 1; k < r; k++) {
if (L[i] >= R[j] && j != n2) {
a.elemList[k] = R[j];
j++;
}
else if (i != n1) {
a.elemList[k] = L[i];
i++;
}
}
for (int i = 0; i < r; i++) {
printf("%d ", a.elemList[i]);
}
printf("\n");
for (int i = 0; i < a.length; i++) {
printf("%d ", a.elemList[i]);
}
printf("\n\n");
free(L);
free(R);
return OK;
}
Status MERGE_SORT(SqList& a, int p, int r) {//fun_beta_2
if (!a.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
if (!a.length) {
std::cerr << "要查询的线性表为空。" << std::endl;
return ERROR;
}
if (p < r) {
int q = (p + r) / 2;
printf("(MERGE_SORT(a, p:%d, q:%d))r:%d\n", p, q, r);
MERGE_SORT(a, p, q);
int temp = q + 1;
printf("(MERGE_SORT(a, q:%d, r:%d))p:%d\n", temp, r, p);
MERGE_SORT(a, q + 1, r);
printf("(MERGE(a, p:%d, q:%d, r:%d))\n", p, q, r);
_MERGE(a, p, q, r);
return OK;
}
return ERROR;
}//fun_beta_2
Status SqList_Merge(SqList& LA, SqList& LB) {//fun_beta_3
if (!LA.elemList|| !LB.elemList) {
std::cerr << "要合并的线性表不存在。" << std::endl;
return ERROR;
}
if (INIT_SQLIST < (LA.length + LB.length) ){
std::cerr << "超出线性表申请的内存空间。" << std::endl;
return OVER_FLOW;
}
int j = 0;
for (int i = 0; i <LB.length; ++i) {
if (!LocateElem(LA, LB.elemList[i])) {
LA.elemList[LA.length] = LB.elemList[i];
++LA.length;
}
}
return OK;
}//fun_beta_3
Status AddList(SqList& L, Elemtype n) {//fun_beta_4
if (!L.elemList) {
std::cerr << "要查询的线性表不存在。" << std::endl;
return ERROR;
}
L.elemList[L.length] = n;
++L.length;
return OK;
}//fun_beta_4
数据结构-线性结构-线性表-顺序表的初步实现
于 2023-09-30 10:51:32 首次发布