第1关:顺序表的查找功能
任务描述
本关任务:实现顺序表中数据的查找功能。
相关知识
为了完成本关任务,你需要掌握:1. 线性表;2. 顺序表。
线性表
线性表是最基本、最简单、也是最常用的一种数据结构。线性表结构中,数据元素之间通过一对一首尾相接的方式连接起来。具体实现时,线性表可以采用不同的存储策略。
下面给出了一种基于顺序存储的线性表(即:顺序表)实现方案:
该方案将线性表存储在一片连续空间里,并通过data[]
和length
两个属性元素,组织成为一个结构体:
-
data[]
: 存储线性表数据元素的数组; -
length
: 当前线性表里的数据元素个数。
为了讨论简化,我们假设每个数据元素是一个整数:
typedef int ElemType; // 数据元素的类型
顺序表
顺序表的类型定义如下:
#define MAXSIZE 50 // 最大长度
typedef struct {
ElemType data[MaxSize]; // 存放顺序表元素
int length; // 线性表的当前长度
}SqList;
若定义:SqList * L
,则通过L
就可对顺序表进行操作。
对数据元素进行操作处理是一个数据结构的重要组成部分。顺序表涉及的主要操作如下:
-
顺序表的初始化:构造一个最多可存储
MAXSIZE
个数据元素的顺序表,并将其初始状态设置为length=0
,即为空表。该操作函数具体定义如下:void InitList(SqList *&L)//初始化线性表
-
销毁顺序表:释放
L
所指向的用于存储数据元素的存储空间。该操作函数具体定义如下:void DestroyList (SqList *&L)
-
判断顺序表是否为空:若为空表,则返回
true
,否则返回false
。该操作函数具体定义如下:bool ListEmpty(SqList *L)
-
创建顺序表:通过一个数组中的
n
个元素,创建一个顺序表。该操作函数具体定义如下:void CreateList(SqList *&L,ElemType a[],int n)
-
输出顺序表: 输出整个顺序表。该操作函数具体定义如下:
void DispList(SqList *L)
-
查找顺序表的第
i
个数据元素:获取顺序表的第i
个数据元素L->data[i-1]
赋给e
,i
的有效范围[1,L->length]
。该操作函数具体定义如下:bool GetElem(SqList *L,int i,ElemType &e)
-
查找顺序表中第一个值为
x
的数据元素的位置: 在顺序表中查找第一个值为x
的元素,找到则返回该元素在表中的位置,否则返回0
。该操作函数具体定义如下:int LocateElem(SqList *L, ElemType e)
编程要求
在右侧编辑器中补充代码,完成GetElem
和LocateElem
两个操作函数,以实现顺序表中数据的查找功能,具体要求如下:
-
GetElem
: 获取顺序表的第i
个数据元素L->data[i-1]
,i
的有效范围[1,L->length]
; -
LocateElem
: 在顺序表中查找第一个值为e
的元素,找到则返回该元素在表中的位置,否则返回0
。
注意:在实现两个操作函数的函数体内可调用其他操作。
测试说明
可在右侧文件夹中查看step1/Main.cpp
文件,以便与你的操作。
平台会对你编写的代码进行测试:
测试输入:
5 // 输入数据元素的个数
8 9 12 33 45 // 输入5个数据元素,创建顺序表
2 // 获取第2个数据元素
33 // 查找值为33的数据元素
预期输出:
9 // 输出第2个数据元素的值
4 // 输出值为33的数据元素在表中的位置
开始你的任务吧,祝你成功!
sqlist_findAt.cpp
#include<stdio.h>
#include<stdlib.h>
#include"sqlist_findAt.h"
void CreateList(SqList *&L,ElemType a[],int n)//整体建立顺序表
{
L=(SqList *)malloc(sizeof(SqList));
for(int i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList *&L) //初始化线性表
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void DestroyList(SqList *&L) //销毁线性表
{
free(L);
}
bool ListEmpty(SqList *L) //判线性表是否为空表
{
return(L->length==0);
}
void DispList(SqList *L) //输出线性表
{
for(int i=0;i<L->length;i++)
{
printf("%d",L->data[i]);
}
printf("\n");
}
bool GetElem(SqList *L,int i,ElemType &e)
{
if(i<1||i>L->length)
return false;
e=L->data[i-1];
return true;
}
int LocateElem(SqList *L, ElemType e)
{
int i=0;
while(i<L->length&&L->data[i] != e)
i++;
if(i>=L->length)
return 0;
else
return i+1;
}
sqlist_findAt.h
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}SqList;
void CreateList(SqList *&L,ElemType a[], int n);
void InitList(SqList *&L);
void DestroyList(SqList *&L);
bool ListEmpty(SqList *L);
void DispList(SqList *L);
bool GetElem(SqList *L,int i,ElemType &e);
int LocateElem(SqList *L,ElemType e);
main.cpp
#include"sqlist_findAt.h"
#include<stdio.h>
int main()
{
int a[MaxSize],n,k,e;
SqList *L;
InitList(L);
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
CreateList(L,a,n);
scanf("%d",&k);
if(GetElem(L,k,e))
printf("%d\n",e);
else
printf("查找失败!");
scanf("%d",&e);//输入要查找的元素值
k=LocateElem(L,e);
printf("%d\n",k);
return 0;
}
第2关:顺序表的插入和删除功能
sqlist_Del_Insert.cpp
#include<stdio.h>
#include<stdlib.h>
#include"sqlist_findAt.h"
void CreateList(SqList *&L,ElemType a[],int n)//整体建立顺序表
{
L=(SqList *)malloc(sizeof(SqList));
for(int i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList *&L) //初始化线性表
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void DestroyList(SqList *&L) //销毁线性表
{
free(L);
}
bool ListEmpty(SqList *L) //判线性表是否为空表
{
return(L->length==0);
}
void DispList(SqList *L) //输出线性表
{
for(int i=0;i<L->length;i++)
{
printf("%d",L->data[i]);
}
printf("\n");
}
bool ListInsert(SqList *&L, int i, ElemType e) //线性表的插入操作
//在第i个位置插入值为e的元素,i的取值范围为[1,L->length+1]
{
int j;
if(i<1||i>L->length+1||L->length == MaxSize)
return false;
i--;
for(j=L->length;j>i;j--)
L->data[j] = L->data[j-1];
L->data[i] = e;
L->length++;
return true;
}
bool ListDelete(SqList *&L, int i,ElemType &e)//线性表的删除操作
//删除第i个位置的元素,i的取值范围为[1,L->length]
{
int j;
if(i<1||i>L->length)
return false;
i--;
e=L->data[i];
for(j=i;j<L->length-1;j++)
L->data[j] = L->data[j+1];
L->length--;
return true;
}
sqlist_Del_Insert.h
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}SqList;
void CreateList(SqList *&L,ElemType a[], int n);
void InitList(SqList *&L);
void DestroyList(SqList *&L);
bool ListEmpty(SqList *L);
void DispList(SqList *L);
bool ListInsert(SqList *&L,int i,ElemType e);
bool ListDelete(SqList *&L,int i, ElemType &e);
main.cpp
#include"sqlist_Del_Insert.h"
#include<stdio.h>
int main()
{
int a[MaxSize],n,k,e;
SqList *L;
InitList(L);
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
CreateList(L,a,n);
scanf("%d",&k);
if(ListDelete(L,k,e))
DispList(L);
else
printf("\n");
scanf("%d %d",&k,&e);
if(ListInsert(L,k,e))
DispList(L);
else
printf("\n");
DestroyList(L);
return 0;
}