64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream.h>
#include<stdio.h>
template <class T>
class LinearList
{
public:
LinearList(){};
~LinearList(){};
virtual bool IsEmpty() const=0;
virtual int Length() const=0;
virtual bool Find(int k,T&x) const=0;
virtual int Search(const T&x) const=0;
virtual bool Insert(int k,const T&x)=0;
virtual bool Delete(int k)=0;
virtual bool Update(int k,const T&x)=0;
virtual void Output(ostream& out)const=0;
};
template <class T>
class SeqList:public LinearList<T>
{
public:
SeqList(int MaxListSize);
~SeqList(){delete[] elements;};
bool IsEmpty() const;
int Length() const;
bool Find(int k,T&x)const;
int Search(const T&x)const;
bool Insert(int k,const T&x);
bool Delete(int k);
bool Update(int k,const T&x);
void Output(ostream& out)const;
private:
int length;
int MaxLength;
T *elements;
};
template<class T>
int SeqList<T>::Search(const T&x)const
{
for(int i=0;i<length;i++)
if(elements[i]==x)return ++i;
return 0;
}
template <class T>
SeqList<T>::SeqList(int MaxListSize)
{
MaxLength=MaxListSize;
elements=new T[MaxLength];
length=0;
}
template <class T>
bool SeqList<T>::IsEmpty()const
{
return length==0;
}
template <class T>
int SeqList<T>::Length()const
{
return length;
}
template<class T>
bool SeqList<T>::Find(int k,T&x)const
{
if(k<1||k>length){
cout<<"Out of Bounds"<<endl;
return false;
}
x=elements[k-1];
return true;
}
template<class T>
bool SeqList<T>::Insert(int k,const T&x)
{
if(k<0||k>length){
cout<<"Out Of Bounds"<<endl;
return false;
}
if(length==MaxLength){
cout<<"OverFlow"<<endl;
return false;
}
for(int i=length-1;i>=k;i--)elements[i+1]=elements[i];
elements[k]=x;
length++;
return true;
}
template <class T>
bool SeqList<T>::Delete(int k)
{
if(!length){
cout<<"UnderFlow"<<endl;
return false;
}
if(k<1||k>length){
cout<<"Out Of Bounds"<<endl;
return false;
}
for(int i=k;i<=length;i++)elements[i-1]=elements[i];
length--;
return true;
}
template<class T>
bool SeqList<T>::Update(int k,const T&x)
{
if(k<1||k>length){
cout<<"Out Of Bounds"<<endl;
return false;
}
elements[k-1]=x;
return true;
}
template<class T>
void SeqList<T>::Output(ostream& out)const
{
for(int i=0;i<length;i++)
out<<elements[i]<<' ';
}
void main()
{
SeqList<char> sl(100);
sl.Search('a');
}