当前位置:网站首页>8576 Basic operations of sequential linear tables
8576 Basic operations of sequential linear tables
2022-08-02 14:18:00 【weixin_50862344】
8576 Basic operations on sequential linear tables
- 题干
时间限制:1000MS 代码长度限制:10KB
提交次数:9027 通过次数:2456
题型: 编程题 语言: G++;GCC
Description 编写算法,Create initialized capacity as LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作.This topic gives some code,请补全内容.
- 答案
#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;
}SqList;
int InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
// 算法2.3,构造一个空的线性表L,The linear table has a predefined size of LIST_INIT_SIZE
// 请补全代码
}
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-1;i++) printf("%d ",L.elem[i]); // 请填空
}
printf("\n");
return OK;
}
int ListInsert_Sq(SqList &L,int i,int e)
{
if(i<1||i>L.length+1) return ERROR;
int j;
if(L.length==LIST_INIT_SIZE) return ERROR;//???
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
++L.length;
return OK;
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
}
int ListDelete_Sq(SqList &L,int i, int &e)
{
if(i<1||i>L.length) return ERROR;
int j;
e=L.elem[i-1];
for(j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return OK;//如何返回
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
}
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"); // 判断i值是否合法,请填空
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"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}
The best thing to think about before copying the answer
The following questions are questions that I have had doubts about before,If you have a better explanation, you can also comment to let more people know
int *elem; 为啥要在elem前加 *?
数组长度 length 和 i范围 之间的关系:
(a)长度length从1开始
(b)The sequence table is essentially an array,因此i从0开始L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));用法的理解:
(类型 *)malloc(数字 *sizeof(类型));
e.g. (int *)malloc(10 *sizeof(int));
使用realloc,free,malloc,callocThe header file must have one#include<stdlib.h>
iThe legal values are different in the insert and delete functions:
插入:1≤i≤L.length +1
删除函数:1≤i≤L.lengthHow to achieve return delete value
Save the return value before using it in the main function为什么不能直接使用C语言
The code exists as & 的c++运算符( 例:int Load_Sq(SqList &L) )
&后跟一个变量.Each variable corresponds to a block of storage space.Each storage space has a number,即地址,&The variable name indicates that the code is taken out,The variable name means to retrieve the value in the storage space corresponding to the number.
边栏推荐
猜你喜欢
How does Apache, the world's largest open source foundation, work?
关于市场后市的发展预测? 2021-05-23
yolov5改进(一) 添加注意力集中机制
第十一单元 序列化器
Some impressions of the 519 plummet 2021-05-21
The world's largest Apache open source foundation is how it works?
Raft协议图解,缺陷以及优化
[ROS](01)创建ROS工作空间
logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
理解TCP长连接(Keepalive)
随机推荐
Unit 7 ORM table relationships and operations
【学习笔记】数位dp
paddle window10环境下使用conda安装
FFmpeg 的AVCodecContext结构体详解
rpm包的卸载与安装[通俗易懂]
Unit 4 Routing Layer
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
期货具体是如何开户的?
replay视频播放器_怎么让手机音乐跟视频一起放
Sentinel源码(二)入口方法分析
Basic operations of 8583 sequential stack
xshell连接虚拟机步骤_建立主机与vm虚拟机的网络连接
网络安全第五次作业
Haystack的介绍和使用
MySQL数据库语法格式
8581 线性链表逆置
drf序列化器-Serializer
Flask-RESTful请求响应与SQLAlchemy基础
vim复制粘贴_vim如何复制粘贴
Supervision strikes again, what about the market outlook?2021-05-22