任务描述
本关任务:设计一个高效的用于删除整数顺序表 L 中所有值在[x,y]范围内的元素的算法。
阅读以下的算法代码,试着分析它的效率
void fun1(SqList *&L,ElemType x,ElemType y)
{ int i,j;
for (i=0;i<L->length;i++)
if (L->data[i]>=x && L->data[i]<=y)
{
for (j=i;j<L->length-1;j++)
L->data[j]=L->data[j+1];
L->length--;
}
}
请先来对这段代码进行分析
- 第一是否能实现删除整数顺序表 L 中所有值在[x,y]范围内的元素的功能。
- 第二,这样写的代码效率如何?
- 如何提高效率呢?
相关知识
在顺序表中进行元素的删除,考虑两个思路:
- 一靠移动来覆盖原数据达到删除的目的,这样的操作往往意味着大量元素的移动,时间效率不高
- 二靠交换,将要删除的元素交换到顺序表的尾部,然后通过length--,达到删除的目的。
实验要求
补充完整fun2()函数
- void fun2(SqList *&L,ElemType x,ElemType y)
测试fun1()函数
- 测试fun1()函数,看是否能完成任务,为什么?
测试说明
平台会对你编写的代码进行测试:
程序代码
sqlist.cpp
#include "sqlist.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);
}
int ListLength(SqList *L)
{
return(L->length);
}
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;
}
bool ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return false;
i--; //将顺序表位序转化为elem下标
for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++; //顺序表长度增1
return true;
}
bool ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length)
return false;
i--; //将顺序表位序转化为elem下标
e=L->data[i];
for (j=i;j<L->length-1;j++) //将data[i]之后的元素前移一个位置
L->data[j]=L->data[j+1];
L->length--; //顺序表长度减1
return true;
}
void fun1(SqList *&L,ElemType x,ElemType y)
{ int i,j;
for (i=0;i<L->length;i++)
if (L->data[i]>=x && L->data[i]<=y)
{ for (j=i;j<L->length-1;j++)
L->data[j]=L->data[j+1];
L->length--;
}
}
void fun2(SqList *&L,ElemType x,ElemType y)
{
int k=0;
for(int i=0;i<L->length;i++)
if(L->data[i]>=x && L->data[i]<=y)
k++;
else
L->data[i-k]=L->data[i];
L->length-=k;
}
sqlist.h
//顺序表基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{ ElemType data[MaxSize]; //存放顺序表元素
int length; //存放顺序表的长度
} SqList; //顺序表的类型
void CreateList(SqList *&L,ElemType a[],int n);
void DispList(SqList *L);
void fun1(SqList *&L,ElemType x,ElemType y);
void fun2(SqList *&L,ElemType x,ElemType y);
void DestroyList(SqList *&L);
main.cpp
#include "sqlist.h"
int main()
{
ElemType a[]={5,8,6,1,0,9,4,2,3,1};
ElemType x,y;
SqList *L;
CreateList(L,a,10);
printf("L:");DispList(L);
printf("请输入要删除的区间范围[x,y]x和y的值\n");
//void CreateList(SqList *&L,ElemType a[],int n);
//void DispList(SqList *L);
//void fun1(SqList *&L,ElemType x,ElemType y);
//void fun2(SqList *&L,ElemType x,ElemType y);
//void DestroyList(SqList *&L);
scanf("%d %d",&x,&y);
//fun1(L,x,y);
fun2(L,x,y);
printf("L:");DispList(L);
}