当前位置:网站首页>顺序表的操作,你真的学会了吗?
顺序表的操作,你真的学会了吗?
2022-04-23 14:11:00 【白衣折扇y】
新人小白的博客
️希望大家多多关注
以后会经常更新哒~
️个人主页: 收藏加关注,永远不迷路~ ️
前言
编程实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
Tips:文章有点长,小主请耐心观看嗷~~
一、目的
深入理解顺序表的逻辑结构、物理结构等概念,掌握顺序表基本操作的编程实现,注意顺序表插入、删除等操作过程中数据元素的移动现象。编写程序时,要考虑程序的健壮性,熟练掌握通过函数参数返回函数结果的办法。ヾ(◍°∇°◍)ノ゙
二、步骤
1.定义存储表示
例如顺序表的最大长度,存储空间基址,表长等。
//定义存储表示
typedef int ElemType; //定义ElemType类型为int
# define MAXSIZE 100 // 顺序表的最大长度
typedef struct //若后面不再用,可省略结构名
{
ElemType *elem; //存储空间基址
int length; //当前表长(特指元素个数)
} SqList;
2. 定义操作函数
对函数进行初始化,构造销毁线性表的函数DestoryList,清空线性表的函数ClearList,求线性表长度的函数GetLength,判断线性表是否为空的函数ListEmpty,获取线性表中的指定位置元素内容GetElem,求前驱、后继的函数,在线性表指定位置插入元素的函数ListInsert,删除线性表指定位置元素ListDelete,显示线性表函数和退出的操作。
//定义操作函数
typedef int Status;
Status InitList(SqList &L)
{
//构造一个空的线性表L。
L.elem = new ElemType[MAXSIZE];
L.length = 0; // 空表长度为0
return 1;
}
void DestroyList(SqList &L)
{
if (L.elem) delete[ ] L.elem; //释放存储空间
L.length=0;
L.elem=NULL;
}
int GetLength(SqList L)
{
return L.length;
}
bool ListEmpty(SqList L)
{
if (L.length==0)
return true;
else
return false;
}
Status ListInsert(SqList &L,int i,ElemType e)
{
if(i<1 || i>L.length+1) return -1; //i值不合法
if(L.length==MAXSIZE) return -1;
//当前存储空间已满
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 1;
}
Status ListDelete(SqList &L,int i, ElemType &e)
{
if((i<1)||(i>L.length)) return -1; //i值不合法
e=L.elem[i-1];
for (int j=i; j<=L.length-1; j++)
L.elem[ j-1]=L.elem[j];//被删除元素之后的元素前移
--L.length; //表长减1
return 1;
}
Status ListPrior(SqList &L,int i,ElemType &e)
{
if((i<2)||(i>L.length)) return -1; //i值不合法
else cout<<L.elem[i-2]<<endl;
return 1;
}
Status ListNext(SqList &L,int i,ElemType &e)
{
if((i<1)||(i>L.length-1)) return -1; //i值不合法
else cout<<L.elem[i]<<endl;
return 1;
}
void DisplayList(SqList L)
{
for(int i=1; i<=L.length; ++i)
cout<<L.elem[i-1]<<" ";
cout<<endl;
}
void ClearList(SqList &L)
{
L.length=0; //将线性表的长度置为0
}
Status GetElem(SqList L,int i,ElemType &e)
{
//判断i值是否合理,若不合理,返回ERROR
if (i<1||i>L.length) return -1;
e=L.elem[i-1]; //第i-1个单元存储第i个数据
return 1;
}
3.采用菜单样式让操作更加方便清楚。
此功能用来实现一个小菜单哦
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<<" 退出,输入0"<<endl;
}
4. 调试实验代码并进行相关测试,得出实验结果。
完整代码来啦 :
#include <iostream>
using namespace std;
//定义存储表示
typedef int ElemType; //定义ElemType类型为int
# define MAXSIZE 100 // 顺序表的最大长度
typedef struct //若后面不再用,可省略结构名
{
ElemType *elem; //存储空间基址
int length; //当前表长(特指元素个数)
} SqList;
//定义操作函数
typedef int Status;
Status InitList(SqList &L)
{
//构造一个空的线性表L。
L.elem = new ElemType[MAXSIZE];
L.length = 0; // 空表长度为0
return 1;
}
void DestroyList(SqList &L)
{
if (L.elem) delete[ ] L.elem; //释放存储空间
L.length=0;
L.elem=NULL;
}
int GetLength(SqList L)
{
return L.length;
}
bool ListEmpty(SqList L)
{
if (L.length==0)
return true;
else
return false;
}
Status ListInsert(SqList &L,int i,ElemType e)
{
if(i<1 || i>L.length+1) return -1; //i值不合法
if(L.length==MAXSIZE) return -1;
//当前存储空间已满
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 1;
}
Status ListDelete(SqList &L,int i, ElemType &e)
{
if((i<1)||(i>L.length)) return -1; //i值不合法
e=L.elem[i-1];
for (int j=i; j<=L.length-1; j++)
L.elem[ j-1]=L.elem[j];//被删除元素之后的元素前移
--L.length; //表长减1
return 1;
}
Status ListPrior(SqList &L,int i,ElemType &e)
{
if((i<2)||(i>L.length)) return -1; //i值不合法
else cout<<L.elem[i-2]<<endl;
return 1;
}
Status ListNext(SqList &L,int i,ElemType &e)
{
if((i<1)||(i>L.length-1)) return -1; //i值不合法
else cout<<L.elem[i]<<endl;
return 1;
}
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<<" 退出,输入0"<<endl;
}
void DisplayList(SqList L)
{
for(int i=1; i<=L.length; ++i)
cout<<L.elem[i-1]<<" ";
cout<<endl;
}
void ClearList(SqList &L)
{
L.length=0; //将线性表的长度置为0
}
Status GetElem(SqList L,int i,ElemType &e)
{
//判断i值是否合理,若不合理,返回ERROR
if (i<1||i>L.length) return -1;
e=L.elem[i-1]; //第i-1个单元存储第i个数据
return 1;
}
int main()
{
char operate_code;
show_help();
//定义线性表变量,如SqList L;
SqList L;
//调用初始化线性表函数,如Init_List(L);
InitList(L);
ElemType e;
int i;
while(1)
{
cout<<"请输入操作代码:";
cin>>operate_code;
if(operate_code=='1')
{
cout<<"The list has been cleared."<<endl;
ClearList(L);//调用操作函数
}
else if (operate_code=='2')
{
if(ListEmpty(L))
cout<<"The list is empty."<<endl;
else
cout<<"The list is not empty."<<endl;
}
else if (operate_code=='3')
{
cout<<"The length of list is:"<<GetLength(L)<<endl;
}
else if (operate_code=='4')
{
cout<<"请输入指定的位置:"<<endl;
cin>>i;
if(GetElem(L,i,e) == 1) cout<<"这个位置的数据是:"<<e<<endl;
else cout <<"error"<<endl;
}
else if (operate_code=='5')
{
cout<<"请输入你想查找第几个元素的前驱:"<<endl;
cin>>i;
if(ListPrior(L,i,e) == -1) cout<<"error"<<endl;
}
else if (operate_code=='6')
{
cout<<"请输入你想查找第几个元素的后继:"<<endl;
cin>>i;
if(ListNext(L,i,e)==-1) cout<<"error"<<endl;
}
else if (operate_code=='7')
{
cout<<"请输入插入元素及其位置:"<<endl;
cin>>e>>i;
ListInsert(L,i,e);
}
else if (operate_code=='8')
{
cout<<"请输入你想要删除哪个位置的元素:"<<endl;
cin>>i;
if(ListDelete(L,i,e)==-1) cout<<"error"<<endl;
}
else if (operate_code=='9')
{
cout<<"The contents of the list are:"<<endl;
DisplayList(L);
}
else if (operate_code=='0')
{
break;
}
else
{
cout<<"\n操作码错误!!!"<<endl;
show_help();
}
}
//调用销毁线性表函数,如Destroy_List(L);
DestroyList(L);
return 0;
}
Tips:马上就结束啦~~
三、实验数据记录及运行结果
通过菜单调用各个操作:
1️⃣ 插入数据(数据,位置),当输入位置不合法的时候,线性表不会显示。如:(1,0)、(1 ,2)。然后正确插入3个数据(2,1)、(1,1)、(3,3);
2️⃣ 显示顺序表中的数据,屏幕输出1, 2, 3;
3️⃣ 判空,屏幕输出顺序表非空;
4️⃣ 输出顺序表长度,屏幕输出3;
5️⃣ 获取指定位置元素,要测指定位置在【1,3】范围之外的情况和之内的情况;
6️⃣ 定位,输入:4, 输出:不存在,输入2,输出位置为2;
7️⃣ 求直接前驱,要测求第一个元素的前驱、不存在顺序表中的元素的直接前驱,其他元素的直接前驱;
8️⃣ 求直接后继,要测最后一个元素的后继、不存在顺序表中的元素的直接后继,其他元素的直接后继;
9️⃣ 删除,要测位置在【1,3】范围之外的情况和之内的情况;
清空操作后再测长度;
结语
本文用来介绍数据结构中顺序表的代码实现过程及运行结果示例。用菜单样式实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
版权声明
本文为[白衣折扇y]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_54439023/article/details/124283603
边栏推荐
- VMware installation 64 bit XP Chinese tutorial
- 获取线程返回值Future接口与FutureTask类使用介绍
- 在Clion中给主函数传入外部参数
- 解决ssh配置文件优化以及连接慢的问题
- Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
- 处理 mkdir:无法创建目录“aaa“:只读文件系统
- std::map 和 std::vector 内存释放
- API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
- MySQL数据库讲解(七)
- js 递归(1)
猜你喜欢
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
MySQL数据库讲解(九)
XX project structure notes
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
setcontext getcontext makecontext swapcontext
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
字节面试编程题:最小的K个数
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
使用Executors类快速创建线程池
Man man notes and @ reboot usage of crontab
随机推荐
std::map 和 std::vector 内存释放
逻辑卷创建与扩容
OpenStack如何跨版本升级
json date时间日期格式化
Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
XX project structure notes
SSH 通过跳板机连接远程主机
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
openstack理论知识
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
常见存储类型和FTP主被动模式解析
Web page, adaptive, proportional scaling
Pass in external parameters to the main function in clion
Use the executors class to quickly create a thread pool
js 抛物线运动方法封装
mysql 5.1升级到5.67
API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
KVM学习资源