}
输入要求
一组数据
第一行输入一个 n ,表示将把 n 个数放进顺序表里
第二行输入 n 个数,建立顺序表
第三行输入一个 m ,把顺序表里第 m 个数赋值给 e
第四行输入一个 e ,在顺序表中从前往后查找第一个值为 e 的元素为第几个
第五行输入一个 m 个一个 e,在顺序表的第 m 个位置插入 e 元素
第五行输入一个m,删除顺序表里的第m个元素
输出要求
遍历顺序表结果占一行,每个数后面跟一个空格
测试数据
输入示例
10
1 2 3 4 5 6 7 8 9 10
5
8
7 11
3
输出示例
The value of e is 5
The index of 8 is 8
1 2 3 4 5 6 11 7 8 9 10
1 2 4 5 6 11 7 8 9 10
#include<stdio.h>
#include<stdlib.h>
// 函数结果状态码
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 1024 // 顺序表可能达到的最大长度
typedef int ElemType; // 元素数据类型
typedef struct {
ElemType *elem; //指向数据元素的基地址
int length; // 当前长度
} SqList; // 顺序表
// 顺序表基本操作,序号i从1开始
Status createList(SqList &L, int n); // 根据用户输入,创建n个元素的表
void traverseList(SqList L); // 遍历,打印表中元素
Status getElem(SqList L, int i, ElemType &e); // 取值,将序号i的元素存入e
int locateElem(SqList L, ElemType e); // 查找,返回元素e的序号
Status listInsert(SqList &L, int i, ElemType e); // 插入
Status listDelete(SqList &L, int i); // 删除
void destroyList(SqList &L){
free(L.elem);
L.elem = NULL;
}
int main(){
SqList list;
int e, n, m;
scanf("%d", &n);
createList(list, n);
scanf("%d", &m);
if(getElem(list,m,e) == OK){ //把 e 赋值为链表里的第 m 个数
printf("The value of e is %d\n",e);
}else{
printf("There is no m number in the list.\n");
}
scanf("%d", &e);
if(locateElem(list,e)){
printf("The index of %d is %d\n",e,locateElem(list,e));
}else{
printf("There is no number %d in the list.\n",e);
}
scanf("%d %d",&m,&e); // 插入元素 e 到第 m 个位置
if(listInsert(list,m,e) == OK){
traverseList(list);
}else{
printf("Insert Error\n");
}
scanf("%d",&m); // 删除第 m 个元素
if(listDelete(list,m) == OK){
traverseList(list);
}else{
printf("Delete Error\n");
}
destroyList(list);
}
**********************************************************************************************************************
#include<stdio.h>
#include<stdlib.h>
// 函数结果状态码
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 1024 // 顺序表可能达到的最大长度
typedef int ElemType; // 元素数据类型
typedef struct {
ElemType *elem; //指向数据元素的基地址
int length; // 当前长度
} SqList; // 顺序表
// 顺序表基本操作,序号i从1开始
Status createList(SqList &L, int n); // 根据用户输入,创建n个元素的表
void traverseList(SqList L); // 遍历,打印表中元素
Status getElem(SqList L, int i, ElemType &e); // 取值,将序号i的元素存入e
int locateElem(SqList L, ElemType e); // 查找,返回元素e的序号
Status listInsert(SqList &L, int i, ElemType e); // 插入
Status listDelete(SqList &L, int i); // 删除
void destroyList(SqList &L){
free(L.elem);
L.elem = NULL;
}
int main(){
SqList list;
int e, n, m;
scanf("%d", &n);
createList(list, n);
scanf("%d", &m);
if(getElem(list,m,e) == OK){ //把 e 赋值为链表里的第 m 个数
printf("The value of e is %d\n",e);
}else{
printf("There is no m number in the list.\n");
}
scanf("%d", &e);
if(locateElem(list,e)){
printf("The index of %d is %d\n",e,locateElem(list,e));
}else{
printf("There is no number %d in the list.\n",e);
}
scanf("%d %d",&m,&e); // 插入元素 e 到第 m 个位置
if(listInsert(list,m,e) == OK){
traverseList(list);
}else{
printf("Insert Error\n");
}
scanf("%d",&m); // 删除第 m 个元素
if(listDelete(list,m) == OK){
traverseList(list);
}else{
printf("Delete Error\n");
}
destroyList(list);
}
Status createList(SqList &L, int n){
L.elem = new ElemType [n+5];
for(int i=0;i<n;i++){
scanf("%d",L.elem+i);
}
L.length=n;
return OK;
}
void traverseList(SqList L){
for(int i=0;i<L.length;i++)
printf("%d ",L.elem[i]);
printf("\n");
}
Status getElem(SqList L, int i, ElemType &e){
if(i<1||i>L.length)
return ERROR;
e = L.elem[i-1];
return OK;
}
int locateElem(SqList L, ElemType e){
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){
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){
if((i<1)||(i>L.length))
return ERROR;
for(int j=i;j<=L.length;j++)
L.elem[j-1] = L.elem[j];
L.length--;
return OK;
}