作者:十里春风不如你 | 来源:互联网 | 2023-10-12 10:51
/*
线性表类
*/
class seqlist
{
private:
DataType* data;
int max;
int size;
public:
seqlist(int n=50);
virtual ~seqlist();
int ListSize(void)const;
int ListEmpty(void)const;
int ListFull(void)const;
DataType GetData(int pos)const;
int Locate(DataType& item)const;
void Insert(const DataType& item,int pos);
void Insert(const DataType& item);
void Delete(int pos);
void SetData(const DataType& item,int pos);
void ClearList(void);
};
seqlist::seqlist(int n)
{
cout<<"constructor"< data=new DataType[n];
max=n;
size=0;
}
seqlist::~seqlist()
{
cout<<"destructor"< delete[]data;
}
int seqlist::ListSize(void)const
{
return(size);
}
int seqlist::ListEmpty(void)const
{
if(size==0)
{
return 1;
}
return 0 ;
}
int seqlist::ListFull(void)const
{
if(size==0)
{
return 1;
}
return 0;
}
DataType seqlist::GetData(int pos)const
{
if(pos<0||pos>size-1)
{
cerr<<"Pos in GetData is out of range!"< exit(1);
}
return(data[pos]);
}
int seqlist::Locate(DataType& item)const
{
int i=0;
while(i<=size-1&&item!=data[i])
{
i++;
}
if(i<=size-1)
{
return(i);
}
return(-1);
}
void seqlist::SetData(const DataType& item,int pos)
{
if(pos<0||pos>size-1)
{
cerr<<"Pos in SetData is out of range"< exit(1);
}
data[pos]=item;
}
void seqlist::Insert(const DataType& item,int pos)
{
int i;
if(size==max)
{
cerr<<"SeqList is full"< exit(1);
}
if(pos<0||pos>size)
{
cerr<<"Posin Insert is out of range"< exit(1);
}
if(pos==size)
{
data[pos]=item;
}else
{
for(i=size-1;i>=pos;i--)
{
data[i+1]=data[i];
}
}
data[pos]=item;
size++;
}
void seqlist::Insert(const DataType& item)
{
if(this->size==this->max)
{
cerr<<"SeqList is full"< exit(1);
}
this->data[this->size]=item;
this->size++;
}
void seqlist::Delete(int pos)
{
int i;
if(size==0)
{
cerr<<"Delete an empty list"< exit(1);
}
if(pos<0||pos>size-1)
{
cerr<<"Pos in Delete is out of range"< exit(1);
}
for(i=pos+1;i<=size-1;i++)
{
data[i-1]=data[i];
}
size--;
}
void seqlist::ClearList(void)
{
size=0;
}