📄 sqlist.h
字号:
//顺序表类
#ifndef _SQLIST_H
#define _SQLIST_H
#include<iostream>
using namespace std;
const int NOT_PRESENT=0;
const int ENTRY_FOUND=1;
const int RANGE_ERROR=0;
const int SUCCESS=1;
const int OVER_FLOW=0;
const int MAX_LIST_SIZE=1000;
//class SqList
template<class ElemType>
class SqList
{
protected:
//顺序表实现的数据成员
int count;//数据元素个数
ElemType elem[MAX_LIST_SIZE];//元素存储空间
//辅助函数
bool Full()const;
void Init();
public:
SqList();
~SqList(){};
int Length()const;
bool Empty()const;
void Clear();
//遍历线性表
void Traverse(void(*Visit)(ElemType &e));
int GetElem(int position,ElemType &e)const;
int SetElem(int position,const ElemType &e);
int Delete(int position,ElemType &e);
int Insert(int position,const ElemType &e );
SqList(const SqList<ElemType> ©);//复制构造函数
SqList<ElemType> &operator=(const SqList<ElemType>©);//赋值语句重载
};
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////以下是类成员函数的实现////////////////////////////
template<class ElemType>
bool SqList<ElemType>::Full()const
{
return count==MAX_LIST_SIZE;
}
template<class ElemType>
void SqList<ElemType>::Init()
{
count=0;
}
template<class ElemType>
SqList<ElemType>::SqList()
{
Init();
}
template<class ElemType>
int SqList<ElemType>::Length()const
{
return count;
}
template<class ElemType>
bool SqList<ElemType>::Empty()const
{
return count==0;
}
template<class ElemType>
void SqList<ElemType>::Clear()
{
count=0;
}
template<class ElemType>
void SqList<ElemType>::Traverse(void(*visit)(ElemType &))
{//遍历函数
for(int curPosition<=Length();curPosition++)
{
(*visit)(elem[curPosition-1]);
}
}
template< class ElemType>
int SqList<ElemType>::GetElem(int position,ElemType &e)const
{//取指定位置的数据元素
if(position<1||position>Length())
{
return NOT_PRESENT;
}
else
{
e=elem[position-1];
return ENTRY_FOUND;
}
}
template<class ElemType>
int SqList<ElemType>::SetElem(int position,const ElemType &e)
{//设定指定位置的元素值
if(position<1||position>Length())
{
return RANGE_ERROR;
}
else
{
elem[position-1]=e;
return SUCCESS;
}
}
template<class ElemType>
int SqList<ElemType>::Insert(int position,const ElemType &e )
{//指定位置插入元素
int len=Length();
ElemType tmp;
if(Full())
{
return OVER_FLOW;
}
else if(position<1||position>len+1)
{
return RANGE_ERROR;
}
else
{
count++;
for(int curPosition=len;curPosition>=position;curPosition--)
{
GetElem(curPosition,tmp);
SetElem(curPosition+1,tmp);
}
SetElem(position,e);
return SUCCESS;
}
}
template<class ElemType>
int SqList<ElemType>::Delete(int position,ElemType &e)
{//删除指定位置的元素
int len=Length();
Elemtype tmp;
if(position<1||position>=len)
{
return RANGE_ERROR;
}
else
{
GetElem(position,e);
for(int curPosition=position+1,curPosition<=len;CurPosition+1)
{
GetElem(curPosition,tmp);
Setelem(curPosition-1,tmp);
}
count--;
return SUCCESS;
}
}
template<class ElemType>
SqList<ElemType>::SqList(const SqList<ElemType>©)
{//复制线性表
int copyLength=copy.Length();
Elemtype e;
Init();
for(int curPosition=1;curPosition<=copyLength;curPosition++)
{
copy.GetElem(curPosition,e);
Insert(Lenght()+1,e);
}
}
template<class ElemType>
SqList<ElemType>&SqList<ElemType>::operator=(const SqList<ElemType> ©)
{//赋值运算符重载
if(©!=this)
{
int copyLength=copy.Length;
ElemType e;
Clear();
for(int curPosition=1;curPosition<=copyLength;curPosition++)
{
copy.GetElem(curPosition,e);
Insert(Length()+1,e);
}
}
return *this;
}
#endif//类的实现文件结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -