注册 登录
编程论坛 C++教室

C++问题难坏我这个菜鸟了

wenzenglin 发布于 2007-03-31 23:25, 583 次点击

#include<iostream.h>
const int defaultSize=10;
template<class Type> class SeqList{
public:
SeqList(int MaxSize = defaultSize);
~SeqList(){ delete [] data;}
int Length() const {return last + 1;}
int Find(Type & x) const;
int IsIn(Type & x);
int Insert(Type & x, int i );
int Remove(Type & x);
int Next(Type & x);
int Prior(Type & x);
int IsEmpty(){ return last == -1;}
int IsFull(){ return last == MaxSize-1;}
Type Get(int i){ return i<0 || i>last? NULl :data[i];}
private:
Type * data;
int MaxSize;
int last;
};
template< class Type>
SeqList<Type>::SeqList(int sz){
if(sz > 0){
MaxSize=sz;
last = -1;
data = new Type[MaxSize];
}
}
template< class Type>
int SeqList<Type>::Find (Type & x)const{ //定位
int i=0;
while(i<= last && data[i] != x) i++; //顺序查找
if( i>last ) return -1;
else return i;
}
template< class Type>
int SeqList<Type>::IsIn (Type & x){ //判断X是否在表中
int i=0, found=0;
while(i<=last && !found)
if(data[i] !=x )i++;
else found = 1;
return found;
}
template<class Type>
int SeqList<Type>::Insert (Type & x, int i){ //插入x在表中第i个位置处
if(i<0 || i>last+1 || last == MasSize-1)return 0;
else {
last++;
for(int j=last;j>i;j--)data[j] = data[j-1];// 依次后移
data[i] = x;
return 1;
}
}
template<class Type>
int SeqList<Type>::Remove (Type & x){ //删除x
int i = Find(x); //在表中查找x
if(i>=0){
last--;
for(int j=i;j<=last;j++)data[j] = data[j+1];
return 1;
}
return 0; //x在表中不存在,不能删除
}
template<class Type>
int SeqList<Type>::Next (Type & x){
int i=Find(x);
if(i>=0 && i<last)return i+1; //x的后继存在
else return -1; //x不在表中或是x的后继不存在
}
template<class Type>
int SeqList<Type>::Prior (Type & x){ //寻找x 的前驱
int i=Find(x);
if(i>0 && i<=last)return i-1; //x的前驱位置
else return -1;
}
void main()
{
SeqList<int> myS;
cout<<myS.Insert(11, 0); //??????????????????????????????????????????????????
cout<<myS.Length ()<<endl;

}

高手给看一下,Compiling不通过啊

4 回复
#2
RL7202007-03-31 23:59
汗一个先。。。
定义的是Insert (Type & x, int i)好不好。。。
{
int i = 11;
cout<<myS.Insert(i, 0);
}
#3
wenzenglin2007-04-01 11:21
Insert (Type & x, int i);
能给解释一下这种定义吗?不太懂~~~~~(Type & x;)
#4
RL7202007-04-01 12:10
以下是引用wenzenglin在2007-4-1 11:21:52的发言:
Insert (Type & x, int i);
能给解释一下这种定义吗?不太懂~~~~~(Type & x;)

这不是典型的引用调用么?
还是你是说Type?那个是模板。。

#5
wfpb2007-04-02 15:22

他是说把&去掉,因为没有这种格式:int &x=11;这是错误的.
insert不会修改这个参数,也不需要用引用.

还有,MasSize写错了,改MaxSize;

1