R7-2 数据结构实验一 顺序表的插入
分数 15
作者 王群芳
单位 合肥师范学院
以顺序表作存储结构,实现线性表的创建、插入。
顺序表的类型描述
#define MAXSIZE 10 // MAXSIZE为最大数据元素数目
typedef int ElemType;
typedef struct
{ ElemType *elem;
int length;
} SqList;
输入格式:
输入分三行
第一行 元素个数
第二行 元素的值。元素间以空格分隔。
第三行 待插入的位置和元素值
具体格式参看输入样例
输出格式:
输出分两行
第一行 插入前的线性表
第二行 插入后的线性表。 如因插入位置错误失败,输出Insert position error! 如因为表满插入失败,输出OVERFLOW!
具体格式参看输出样例
输入样例:
5
1 3 5 7 9
3 4
输出样例:
Before:(1,3,5,7,9)
After:(1,3,4,5,7,9)
输入样例:
5
1 3 5 7 9
7 10
输出样例:
Before:(1,3,5,7,9)
Insert position error!
输入样例:
10
1 2 3 4 5 6 7 8 9 10
6 7
输出样例:
Before:(1,2,3,4,5,6,7,8,9,10)
OVERFLOW!
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
C (gcc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include
#include
#define MAXSIZE 10
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
}SqList;
//初始化顺序表
void InitList(SqList *L){
L->elem = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
L->length = 0;//初始长度0
}
//打印顺序表
void PrintList(SqList L){
printf("(");
for (int i = 0; i < L.length;i++){
if (i > 0){
printf(",");
}
printf("%d",L.elem[i]);
}
printf(")\n");
}
int ListInsert(SqList *L, int pos,ElemType e){
if (pos < 1 || pos > L->length + 1){
return 0;
}
if (L->length >= MAXSIZE){
return -1;
}
for (int i = L->length;i >= pos; i--){
L->elem[i] = L->elem[i - 1];
点赞 (1)
回复