7-1 jmu-ds-顺序表区间元素删除
题目:
若一个线性表L采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)。
输入格式:
三行数据,第一行是顺序表的元素个数,第二行是顺序表的元素,第三行是x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后的顺序表。
输入样例:
10
5 1 9 10 67 12 8 33 6 2
3 10
输出样例:
1 67 12 33 2
答案:
#include<bits/stdc++.h>
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct//定义顺序表
{
ElemType data[101];
int length;
}List;
typedef List *SqList;//定义为指针,故下面用->
void creatlist(SqList &l,int n)//输入元素,建立表
{
l=new List;//?
l->length=n;
for(int i=0;i<l->length;++i)
{
cin>>l->data[i];
}
}
Status deletelist(SqList &l,int x,int y)//删除元素,更新表
{
int j=0;
for(int i=0;i<l->length;++i)
{
if((l->data[i]<x)||(l->data[i]>y))
{
l->data[j]=l->data[i];
j++;
}
}
l->length=j;
return 1;
}
void printlist(SqList l)//输出元素,输出新表
{
for(int i=0;i<l->length;++i)
{
cout<<l->data[i];
if(i<l->length-1)
cout<<" ";
}
}
int main()
{
SqList s;
int n,x,y;
cin>>n;
creatlist(s,n);
cin>>x>>y;
deletelist(s,x,y);
printlist(s);
}
注意:
base operand of '->' has non-pointer type 'const Comple
->操作符前面的操作数类型不是指针类型
错误原因
函数(&对象)
{
对象名.成员 //正确方式
对象名->成员 //编译报错 base operand of '->' has non-pointer type .......
}
引用传递的参数应该理解为对象,而不是指针