oj题目顺序线性表的基本操作

news/2025/3/2 2:43:55/

8576 顺序线性表的基本操作
编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。本题目给出部分代码,请补全内容。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
int *elem;
int length;
int listsize;//最大长度length
}SqList;

int InitList_Sq(SqList &L)
{
// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
// 请补全代码
L.elem = new ElemType[LIST_INIT_SIZE];//new操作符不需要头文件
if(!L.elem)
//exit(ERROR);
return ERROR;
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}

int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
int i;
if(L.length == 0) printf(“The List is empty!”); // 请填空
else
{
printf(“The List is: “);
for(i = 0;i<L.length;i++) printf(”%d “,L.elem[i]); // 请填空
}
printf(”\n”);
return OK;
}

int ListInsert_Sq(SqList &L,int i,int e)//注意下标是从0开始的,第一个位置就是L.elem[0]
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
if( (i<1)||(i>L.length+1) )
return ERROR;
// if(L.length==LIST_INIT_SIZE)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;

}

int ListDelete_Sq(SqList &L,int i, int &e)//下标从0开始
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
if( (i<1)||(i>L.length) )//2注意不是i>L.length+1
return ERROR;
e = L.elem[i-1];
for(int j = i; j<=L.length-1; j++)//这个也可以通过
{
L.elem[j-1] = L.elem[j];
}
// for(int j = i-1; j<L.length; j++)//可以通过
// {
// L.elem[j] = L.elem[j+1];
// }
–L.length;
return OK;
}

int main()
{
SqList T;
int a, i;
ElemType e, x;
if(InitList_Sq(T)) // 判断顺序表是否创建成功
{
printf(“A Sequence List Has Created.\n”);
}
while(1)
{
printf(“1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n”);
scanf(“%d”,&a);
switch(a)
{
case 1: scanf(“%d%d”,&i,&x);
if( ListInsert_Sq(T,i,x) == ERROR ) printf(“Insert Error!\n”);
else printf(“The Element %d is Successfully Inserted!\n”, x);
break;
case 2: scanf(“%d”,&i);
if( ListDelete_Sq(T,i,e) == ERROR ) printf(“Delete Error!\n”);
else printf(“The Element %d is Successfully Deleted!\n”, e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}

输入格式 测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束

输入样例 1
1 2
1
1 3
2
1
3
0

输出样例 A Sequence List Has Created.
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 2 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The List is: 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:


http://www.ppmy.cn/news/1285041.html

相关文章

爬虫工作量由小到大的思维转变---<第三十三章 Scrapy Redis 23年8月5日后会遇到的bug)>

前言: 收到回复评论说,按照我之前文章写的: 爬虫工作量由小到大的思维转变---&#xff1c;第三十一章 Scrapy Redis 初启动/conn说明书)&#xff1e;-CSDN博客 在启动scrapy-redis后,往redis丢入url网址的时候遇到: TypeError: ExecutionEngine.crawl() got an unexpected …

Flink1.17实战教程(第三篇:时间和窗口)

系列文章目录 Flink1.17实战教程&#xff08;第一篇&#xff1a;概念、部署、架构&#xff09; Flink1.17实战教程&#xff08;第二篇&#xff1a;DataStream API&#xff09; Flink1.17实战教程&#xff08;第三篇&#xff1a;时间和窗口&#xff09; Flink1.17实战教程&…

既然前端工程师可以进腾讯字节阿里等大厂,为什么要高薪进小公司?

前言 前不久后台有一个粉丝给我留言&#xff1a;为什么很多人参加校招的时候&#xff0c;宁愿低薪也要进大厂&#xff0c;而不选择更高薪的小公司呢&#xff1f; &#xff08;文末有惊喜&#xff01;文末有惊喜&#xff01;&#xff09; 我想了一下&#xff0c;大概是有3个原因…

StarRocks-3.1.6升级

1、升级路径 1.1、小版本升级 您可以跨小版本升级您的 StarRocks 集群&#xff0c;例如&#xff0c;从 v2.2.6 直接升级到 v2.2.11。 1.2、大版本升级 从 StarRocks v2.0 开始&#xff0c;您可以跨大版本升级 StarRocks 集群&#xff0c;例如&#xff0c;从 v2.2.x 直接升级…

在uniapp中使用背景渐变色与背景图不生效问题

list上有文字详情以及背景图&#xff0c;从背景可以看出是渐变色和 背景图片的结合。 因为使用到渐变色&#xff0c;所以要结合 background-blend-mode 属性来实现与背景图片叠加显示&#xff0c;否则只通过 background: linear-gradient(); background-image: url(); 设置不会…

信息泄露总结

文章目录 一、备份文件下载1.1 网站源码1.2 bak文件泄露1.3 vim缓存1.4 .DS_Store 二、Git泄露2.1 git知识点2.1 log2.2 stash 三、SVN泄露3.1 SVN简介3.2 SVN的文件3.3 SVN利用 四、Hg泄露 一、备份文件下载 1.1 网站源码 常见的网站源码备份文件后缀&#xff1a; tartar.gz…

Matlab实时读取串口数据并实时画图方法

** Matlab实时读取串口数据并实时画图方法 ** 按照数据串口协议如&#xff1a;$KT2,1.80,88.18,39.54,42.86,LO[0.72,-1.04,0.35]&#xff0c;举例。 s serialport("COM12",115200,"Timeout",5); poszeros(100000,3); j1; data1 read(s,1,"uint8&…

组合[中等]

一、题目 给定两个整数n和k&#xff0c;返回范围[1, n]中所有可能的k个数的组合。你可以按 任何顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;n 4, k 2 输出&#xff1a; [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 示例 2&#xff1a; 输入&#xff1a;n 1…