[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>顺序表的插入操作 (10 分)
本题要求实现一个函数,在顺序表的第i个位置插入一个新的数据元素e,插入成功后顺序表的长度加1,函数返回值为1;插入失败函数返回值为0;
<>函数接口定义:
int ListInsert(SqList &L,int i,ElemType e);
其中SqList结构定义如下:
typedef struct{ ElemType *elem; int length; }SqList;
<>裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 5 typedef int ElemType;
typedef struct{ ElemType *elem; int length; }SqList; void InitList(SqList
&L);/*细节在此不表*/ int ListInsert(SqList &L,int i,ElemType e); int main() { SqList
L; InitList(L); ElemType e; int i; scanf("%d%d",&i,&e); int
result=ListInsert(L,i,e); if(result==0){ printf("Insertion Error.The value of i
is unlawful or the storage space is full!"); }else if(result==1){
printf("Insertion Success.The elements of the SequenceList L are:"); for(int
j=0;j<L.length;j++){ printf(" %d",L.elem[j]); } } return 0; } /* 请在这里填写答案 */
<>输入格式:
输入数据有1行,首先给出以-1结束的顺序表元素值(不超过100个,-1不属于顺序表元素),然后是插入位置和被插入元素值。所有数据之间用空格分隔。
<>输入样例:
2 6 4 -1 2 100
<>输出样例:
Insertion Success.The elements of the SequenceList L are: 2 100 6 4
<>【代码】
int ListInsert(SqList &L, int i, ElemType e) { if (i >= 0 && i <= L.length+1
&& (L.length + 1) <= MAXSIZE) { 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 1; } return 0;
}
<>【解析】
在第一个if的判断语句中,应是 i <= L.length+1,做题的时候写的是 i <= L.length,所以导致一个测试点一直错。