2020.03.13
顺序表实验
一、判断题
二、单选题
三、填空题
三、函数题
(一)顺序表的查找操作
本题要求实现一个函数,要求从顺序表中查找指定元素,并返回第一个查找成功的元素在表中的位置序号,若查找失败,则返回0;
函数接口定义:
int LocateElem(SqList L,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 LocateElem(SqList L,ElemType e);
int main()
{
SqList L;
InitList(L);
ElemType e;
int p;
scanf("%d",&e);
p=LocateElem(L,e);
printf("The position of %d in SequenceList L is %d.",e,p);
return 0;
}
/* 请在这里填写答案 */
输入格式:
输入数据有1行,首先给出以-1结束的顺序表元素值(不超过100个,-1不属于顺序表元素),然后是待查找的元素值。所有数据之间用空格分隔。
输入样例:
2 6 4 9 13 -1 2
输出样例:
The position of 2 in SequenceList L is 1.
答案:
int LocateElem(SqList L,ElemType e){
for(int i=0;i<L.length;i++)
if(L.elem[i]==e) return (i+1);
return 0;
}
(二)顺序表的插入操作
本题要求实现一个函数,在顺序表的第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<1||i>L.length+1) return 0;
if(L.length==MAXSIZE) return 0;
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;
}
(三)顺序表的删除操作
本题要求实现一个函数,要求将顺序表的第i个元素删掉,成功删除返回1,否则返回0;
函数接口定义:
int ListDelete(SqList &L,int i);
其中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 ListDelete(SqList &L,int i);
int main()
{
SqList L;
InitList(L);
int i;
scanf("%d",&i);
int result=ListDelete(L,i);
if(result==0){
printf("Delete Error.The value of i is illegal!");
}else if(result==1){
printf("Delete 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 1
输出样例:
Delete Success.The elements of the SequenceList L are: 6 4
答案:
int ListDelete(SqList &L,int i){
if(i<1||i>L.length) return 0;
for(int j=i;j<=L.length-1;j++){
L.elem[j-1]=L.elem[j];
}
L.length--;
return 1;
}
(四)求顺序表最大值
本题要求实现一个函数,要求返回顺序表的最大值,空表返回0。题目保证顺序表中所有元素都为正整数。
函数接口定义:
int GetMax(SqList L);
其中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 GetMax(SqList L);
int main()
{
SqList L;
InitList(L);
ElemType e;
int p;
p=GetMax(L);
if(p) printf("The max of SequenceList L is %d.\n",p);
else printf("The SequenceList is null.\n");
return 0;
}
/* 请在这里填写答案 */
输入格式:
输入数据有1行,首先给出以-1结束的顺序表元素(不超过100个,-1不属于顺序表元素),所有数据之间用空格分隔。题目保证输入的顺序元素都是正整数。
输入样例:
2 6 4 13 9 -1
输出样例:
The max of SequenceList L is 13.
答案:
int GetMax(SqList L){
if(L.length==0) return 0;
int max=L.elem[0];
for(int i=0;i<L.length;i++){
if(L.elem[i]>max) max=L.elem[i];
}
return max;
}
五、编程题
顺序表的建立及遍历
读入n值及n个整数,建立顺序表并遍历输出。
输入格式:
读入n及n个整数
输出格式: 输出n个整数,以空格分隔(最后一个数的后面没有空格)。
输入样例: 在这里给出一组输入。例如:
4
-3 10 20 78
输出样例:
在这里给出相应的输出。例如:
-3 10 20 78
答案:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
} SqList;
void InitList(SqList &L)
{
L.elem=(ElemType*)malloc(sizeof(MAXSIZE));
L.length=0;
}
int main()
{
int n;
ElemType q;
SqList L;
InitList(L);
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&q);
L.elem[L.length++]=q;
}
for(int i=0; i<L.length; i++)
printf("%d%c",L.elem[i],i==L.length-1?'\n':' ');
return 0;
}