📄 seqlist.h
字号:
//--------------------------//
// //
// 顺序表类 //
// //
//--------------------------//
#if !defined(_INC_SEQLIST_OO)
#define _INC_SEQLIST_OO
#include<stdlib.h>
template<class T>
class SeqList
{
private:
T *data;
int max, size;
public:
SeqList(int n=50);
~SeqList();
int Size()const;
int Empty()const;
int Full()const;
T GetData(int pos)const;
int Locate(T item)const;
void SetData(T item,int pos);
void Insert(T item,int pos);
void Insert(T item);
void Delete(int pos);
void Clear();
};
template<class T>
SeqList<T>::SeqList(int n)
{
data=new T[n];
if(data==NULL)
{
cerr<<"overflow"<<endl;
exit(1);
}
max=n;
size=0;
}
template<class T>
SeqList<T>::~SeqList()
{
delete []data;
}
template<class T>
int SeqList<T>::Size()const
{
return(size);
}
template<class T>
int SeqList<T>::Empty()const
{
if(size==0)
return(1);
return(0);
}
template<class T>
int SeqList<T>::Full()const
{
if(size==max)
return(1);
return(0);
}
template<class T>
T SeqList<T>::GetData(int pos)const
{
if(pos<0||pos>size-1)
{ cout<<"Pos in GetData is out of range!"<<endl;
exit(1);
}
return(data[pos]);
}
template<class T>
int SeqList<T>::Locate(T item)const
{
int i=0;
while(i<=size-1&&item!=data[i])
i++;
if(i<=size-1)
return(i);
return(-1);
}
template<class T>
void SeqList<T>::SetData(T item,int pos)
{
if(pos<0||pos>size-1)
{ cout<<"Pos in SetData is out of range!"<<endl;
exit(1);
}
data[pos]=item;
}
template<class T>
void SeqList<T>::Insert(T item,int pos)
{
int j;
if(size==max)
{ cout<<"SeqList is full!"<<endl;
exit(1);
}
if(pos<0||pos>size)
{ cout<<"Pos in Insert is out of range!"<<endl;
exit(1);
}
for(j=size-1;j>=pos;j--)
data[j+1]=data[j];
data[pos]=item;
size++;
}
template<class T>
void SeqList<T>::Insert(T item)
{
if(size==max)
{ cout<<"SeqList overflow!"<<endl;
exit(1);
}
data[size]=item;
size++;
}
template<class T>
void SeqList<T>::Delete(int pos)
{
int j;
if(size==0)
{ cout<<"Delete an empty list!"<<endl;
exit(1);
}
if(pos<0||pos>size-1)
{ cout<<"Pos in Delete is out of range"<<endl;
exit(1);
}
for(j=pos+1;j<=size-1;j++)
data[j-1]=data[j];
size--;
}
template<class T>
void SeqList<T>::Clear()
{
size=0;
}
#endif //_INC_SEQLIST_OO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -