当前位置: 首页 > news >正文

顺序表的实现(2)

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
#define MAXSIZE 100 //顺序表可能达到的最大长度
typedef struct {
ElemType* elem; //存储空间的基地址
int length; //当前长度
} SqList;
Status InitList(SqList& L)
{
//构造一个空的顺序表L
L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
if (!L.elem)
exit(OVERFLOW); //存储分配失败退出
L.length = 0; //空表长度为0
return OK;
}
void DestroyList(SqList& L)
{
if (L.elem)
delete[]L.elem; //释放存储空间
}
int ListLength(SqList L)
{
return L.length;
}
bool ListEmpty(SqList L)
{
if (L.length == 0)
return true;
else
return false;
}
Status GetElem(SqList L, int i, ElemType& e) //用e返回L中第i个数据元素的值
{
if (i<1 || i>L.length) //顺序表的位置在1~L.length,那么不符合条件的范围就是
//i<1||i>L.length
return ERROR;
e = L.elem[i - 1]; //数组的第i-1个位置放的就是线性表第i个位置的元素
return OK;
}
int LocateElem(SqList L, ElemType e) //返回L中第1个值与e相同的元素在L中的位置,不存在返回0
{
for (int i = 0; i < L.length; i++)
{
if (L.elem[i] == e)
return i + 1;
}
return 0;
}
Status ListInsert(SqList& L, int i, ElemType e) //在顺序表L中第i个位置插入新的元素e
{
if (i<1 || i>L.length + 1)
{
return ERROR;
}
if (L.length == MAXSIZE) {
return ERROR;
}
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 OK;
}
Status ListDelete(SqList& L, int i) //在顺序表L中删除第i个元素
{
if (i<1 || i>L.length)
return ERROR;
for (int j = i; j <= L.length - 1; j++)
{
L.elem[j - 1] = L.elem[j];
}
--L.length;
return OK;
}
void ListPrint(SqList L)
{
for (int i = 0; i < L.length; i++)
cout << L.elem[i] << ((i == L.length - 1) ? '\n' : ' ');
}
int main()
{
int i;
ElemType e;
SqList L;
string op;
InitList(L);
while (cin >> op) {
if (op == "Empty")
cout << (ListEmpty(L) ? "Empty" : "Not empty") << endl;
else if (op == "Insert") {
cin >> i >> e;
if (ListInsert(L, i, e) == ERROR)
cout << "Insert failed" << endl;
else
ListPrint(L);
}
else if (op == "Length") {
cout << "List length is " << ListLength(L) << endl;
}
else if (op == "GetElem") {
cin >> i;
if (GetElem(L, i, e) == ERROR)
cout << "Out of index" << endl;
else
cout << "The elem at position " << i << " is " << e << endl;
}
else if (op == "LocateElem") {
cin >> e;
i = LocateElem(L, e);
if (i == 0)
cout << e << " is not found in list" << endl;
else
cout << e << " is found at the position " << i << endl;
}
else if (op == "Delete") {
cin >> i;
if (ListDelete(L, i) == ERROR)
cout << "Delete failed" << endl;
else
ListPrint(L);
}
}
DestroyList(L);
return 0;
}

//代码来源:湖南科技大学oj题目

http://www.cnnetsun.cn/news/399998.html

相关文章:

  • 稳压管选型及应用电路解析
  • 揭秘Java结构化并发中的超时陷阱:5种常见错误及最佳实践方案
  • 从零搭建Java传感器校准模块:6个步骤打造企业级稳定系统
  • lora-scripts训练周期缩短秘诀:增量学习机制深度解析
  • 如何实现毫秒级响应?Java Serverless异步调用优化的5个核心步骤
  • 揭秘Java Serverless异步调用陷阱:99%开发者忽略的3个关键问题
  • 社区贡献指南:如何为lora-scripts开源项目提交PR
  • 为什么90%的Java系统在跨境支付中加密失败?这4个坑你一定要避开
  • huggingface镜像网站助力模型加载:提升lora-scripts训练速度
  • Java向量API深度剖析:如何在x64架构下实现计算性能翻倍
  • 百考通AI你的智能学术助手,让毕业论文写作化繁为简
  • CogVideo技术突破:如何用AI将2D视频重塑为立体3D内容
  • Markdown文档写作好帮手:用lora-scripts训练专属文案生成LoRA
  • 深度剖析STLink引脚图:系统学习SWD与JTAG引脚定义
  • 模块化项目中第三方库引入的10大坑,你踩过几个?
  • vue+uniapp微信小程序的 体育用品商城论坛 商城小程序
  • STM32CubeMX下载安装步骤解析:一文说清初始配置要点
  • 手把手教你使用lora-scripts进行古风水墨风格图像生成
  • Java工业传感器校准全解析(精准控制与数据可靠性提升)
  • Flutter应用路由管理新选择:go_router的完整指南
  • 跨模态注意力机制:视频生成技术的革命性突破
  • AI视频立体化技术:重塑2D转3D的智能新范式
  • 地下停车位处遗失物品遗留物检测数据集VOC+YOLO格式700张7类别
  • Kafka Streams聚合操作深度解析(从入门到生产级实战)
  • 揭秘Java在工业传感器校准中的应用:3个你必须知道的优化技巧
  • Keil uVision5安装驱动注意事项:通俗解释必备知识
  • TimelineJS时间轴工具终极指南:5分钟打造专业级交互体验
  • FastAPI物联网数据处理架构深度解析:构建高并发传感器数据平台
  • Godot热更新终极指南:实现无需重启的游戏内容动态更新
  • lora-scripts + Stable Diffusion WebUI 实现动态LoRA调用全记录