顺序表的操作
实验性质
设计性实验
实验要求
- 编程实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
- 采用顺序表结构编程实现:两个集合的运算:交集/并集/差集。(注:此处未实现)
实验目的
通过该实验,深入理解顺序表的逻辑结构、物理结构等概念,掌握顺序表基本操作的编程实现,注意顺序表插入、删除等操作过程中数据元素的移动现象,学生编写程序时,要考虑程序的健壮性,熟练掌握通过函数参数返回函数结果的办法。
实验内容
编程实现顺序表下教材第二章定义的线性表的基本操作,最好用菜单形式对应各个操作,使其变成一个完整的小软件。
参考界面
验收/测试用例
通过菜单调用各个操作,测试点:
- 插入数据(位置, 数据),要测插入位置不合法的情况(0,1)、(2,1),正确插入4个数据(1,2)、(1,1)、(3,3);
- 显示顺序表中的数据,屏幕输出1, 2, 3;
- 判空,屏幕输出顺序表非空;
- 显示顺序表长度,屏幕输出3;
- 获取指定位置元素,要测指定位置在【1,3】范围之外的情况和之内的情况;
- 定位,输入:4, 输出:不存在,输入2,输出位置为2;
- 求直接前驱,要测求第一个元素的前驱、不存在顺序表中的元素的直接前驱,其他元素的直接前驱;
- 求直接后继,要测最后一个元素的后继、不存在顺序表中的元素的直接后继,其他元素的直接后继;
- 删除,要测位置在【1,3】范围之外的情况和之内的情况;
- 清空操作后再测长度;
参考代码
#include <iostream>
using namespace std;
#define ElemType int //顺序表类型
#define InitSize 50 //初始大小
typedef struct {
ElemType *data;
int MaxSize;
int length;
} SqList;
//初始化顺序表
void InitList(SqList &L) {
L.data = new ElemType[InitSize];
L.length = 0;
L.MaxSize = InitSize;
}
//判断顺序表是否为空
bool Empty(SqList L) {
return L.length == 0;
}
//求顺序表的长度
int Length(SqList L) {
return L.length;
}
//获取顺序表指定元素的位置的函数实现
int LocateElem(SqList L, ElemType e) {
for (int i = 0; i < L.length; i++) {
if (L.data[i] == e) {
return i + 1;
}
}
return 0; // 未找到
}
//获取顺序表指定位置的元素的函数实现
int GetElem(SqList L, int i) {
if (i < 1 || i > L.length) {
cout << "位数索引有问题!" << endl;
return -1; // 使用-1表示错误
} else {
return L.data[i - 1];
}
}
//在线性表指定位置插入元素的实现函数
void ListInsert(SqList &L, int i, ElemType e) {
if (i < 1 || i > L.length + 1) {
cout << "插入位置错误!" << endl;
return;
}
if (L.length >= L.MaxSize) { // 动态扩容
ElemType *newData = new ElemType[L.MaxSize * 2];
for (int j = 0; j < L.length; j++) {
newData[j] = L.data[j];
}
delete[] L.data;
L.data = newData;
L.MaxSize *= 2;
}
for (int j = L.length; j >= i; j--) {
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;
L.length++;
}
//删除顺序表指定位置元素的函数实现
void ListDelete(SqList &L, int i) {
if (i < 1 || i > L.length) {
cout << "删除位置有问题!" << endl;
return;
}
for (int j = i; j < L.length; j++) {
L.data[j - 1] = L.data[j];
}
L.length--;
}
//打印顺序表中的元素
void PrintList(SqList L) {
cout << "顺序表为:" << endl;
for (int i = 0; i < L.length; i++) {
cout << L.data[i] << " ";
if ((i + 1) % 10 == 0) {
cout << endl;
}
}
cout << endl;
}
//销毁顺序表
void DestroyList(SqList &L) {
delete[] L.data;
L.data = nullptr;
L.length = 0;
L.MaxSize = 0;
}
//修改顺序表中指定位置元素的函数实现
void ModifyElem(SqList &L, int i, ElemType e) {
if (i < 1 || i > L.length) {
cout << "位置错误!" << endl;
return;
}
L.data[i - 1] = e;
}
//求顺序表指定位置直接前驱的函数实现
ElemType Predecessor(SqList L, int i) {
if (i <= 1 || i > L.length) {
cout << "没有前驱元素!" << endl;
return -1; // 使用-1表示错误或不存在
}
return L.data[i - 2];
}
//求顺序表指定位置直接后继的函数实现
ElemType Successor(SqList L, int i) {
if (i < 1 || i >= L.length) {
cout << "没有后继元素!" << endl;
return -1; // 使用-1表示错误或不存在
}
return L.data[i];
}
//提示窗口函数
void show_help() {
cout << "******* Data Structure ******" << endl;
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 << "A----删除顺序表指定位置元素" << endl;
cout << "B----清空顺序表" << endl;
cout << " 退出,输入0" << endl;
}
int main() {
//创建操作码
char operate_code;
//创建并初始化顺序表
SqList L;
InitList(L);
//显示提示窗口
show_help();
while (true) {
cout << "请输入操作码:";
cin >> operate_code;
int i, e;
switch (operate_code) {
case '1':
cout << "请输入插入位置和元素值:";
cin >> i >> e;
ListInsert(L, i, e);
break;
case '2':
cout << "请输入位置和新值:";
cin >> i >> e;
ModifyElem(L, i, e);
break;
case '3':
PrintList(L);
break;
case '4':
cout << (Empty(L) ? "顺序表为空。" : "顺序表非空。") << endl;
break;
case '5':
cout << "顺序表长度为:" << Length(L) << endl;
break;
case '6':
cout << "请输入位置:";
cin >> i;
e = GetElem(L, i);
if (e != -1) {
cout << "位置" << i << "的元素为:" << e << endl;
}
break;
case '7':
cout << "请输入元素:";
cin >> e;
i = LocateElem(L,e);
if (i != 0) {
cout << "元素" << e << "的位置为:" << i << endl;
}
break;
case '8':
cout << "请输入元素位置:";
cin >> i;
e = Predecessor(L, i);
if (e != -1) {
cout << "位置" << i << "的直接前驱为:" << e << endl;
}
break;
case '9':
cout << "请输入元素位置:";
cin >> i;
e = Successor(L, i);
if (e != -1) {
cout << "位置" << i << "的直接后继为:" << e << endl;
}
break;
case 'A':
cout << "请输入删除位置:";
cin >> i;
ListDelete(L, i);
break;
case 'B':
L.length = 0; // 清空顺序表
break;
case '0':
DestroyList(L);
return 0;
default:
cout << "\n操作码错误!!!" << endl;
show_help();
break;
}
}
return 0;
}