Sequential List(顺序表)

//本文件是顺序表(Sequential List)的头文件,使用C++模板类进行封装(This file is the header file of Sequential list,and packed by C++ tempalte class)
#ifndef SEQLIST_H
#define SEQLIST_H
#include <iostream>
#include "Exception.h"
#include "AbstractList.h"
using namespace std;

template<class T>
class SeqList:AbstractList<T>
{
	public:
		SeqList(int MaxListSize=10);			//constructor			
		~SeqList(){delete [] element;}			//destructor			
		bool IsEmpty() const {return length==0;}	//判断表是否为空(Judge the linear list is empty or not)
		int Length() const {return length;}		//获取表的长度(Get the length of the linear list)
		bool Find(int k,T& x) const;			//用x返回第k个元素,若没有第k个元素,返回假(Output the k-th element to x,return 0 if the it isn't exist)
		int Search(const T& x) const;			//返回元素x的位置(Return the index of the element x)
		SeqList<T>& Delete(int k);			//删除第k个元素(Delete the k-th element)
		SeqList<T>& Insert(int k,const T& x);		//在第k个元素后面插入x(Insert x after the k-th element)
		//void Output(ostream& out) const;		//输出顺序表(Output the linear list )
		
	private:
		int length;
		int MaxSize;
		T*	element;
};

//实现构造函数(Instantiate constructor)
template<class T>
SeqList<T>::SeqList(int MaxListSize)
{
	MaxSize=MaxListSize;
	element=new T[MaxSize];
	length=0;
}

//实现Find(Instantiate Find)
template<class T>
bool SeqList<T>::Find(int k,T& x) const 
{
	//如果不存在第k个元素,则返回false,否则返回true(If the k-th element doesn't exist,it returns false,otherwise it returns true)
	if(k<1||k>length){return false;}	//k超出顺序表的范围,非法的输入(k beyond the range of linear list,it's a illegal input )
	x=element[k-1];
	return true;
}

//实现Search(Instantiate Search)
template<class T>
int SeqList<T>::Search(const T& x) const
{
	//查找x,如果找到,则返回x所在的位置;如果x不在表中,则返回0
	for(int i=0;i<length;i++)
	{
		if(x==element[i])
		{
			return ++i;
		}
	}
	return 0;
}

//实现Insert(Instantiate Insert)
template<class T>
SeqList<T>& SeqList<T>::Insert(int k,const T& x)
{
	//在第k个位置插入x,如果不存在第k个位置,则引发异常OutOfBounds(Insert x at the k-position,an exception 'OutOfBounds' will be thrown if the k-position doesn't exist)
	if(k<0||k>length+1) throw OutOfBounds();
	//如果表已满,则引发异常NoMem(An exception 'NoMem' will be thrown if the linear list is full)
	if(length==MaxSize) throw NoMem();
	//向后移动一个位置(Move backwards one position)
	for(int i=length-1;i>=k;i--)
	{
		element[i+1]=element[i];
	}
	element[k]=x;
	length++;
	
	return *this;
}

//实现Delete(Instantiate Delete)
template<class T>
SeqList<T>& SeqList<T>::Delete(int k)
{
	//如果不存在第k个元素,则引发异常OutOfBounds(An exception 'OutOfBounds'will be thrown if the k-th element doesn't exist)
	if(k<1||k>length) throw OutOfBounds();
  
	//把第k+1,k+2,...元素向前移动一个位置(Move the element:k+1-th,k+2-th... forwards one position)
	for(int i=k;i<length;i++)						
	{
		element[i-1]=element[i];
	}
	length--;
	return *this;
}


#endif

//This is "AbstractList.h" header file  
#ifndef AbstractList_
#define AbstractList_
#include <iostream>
using namespace std;

template<class T>
class AbstractList
{
	public:
	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 AbstractList<T>& Delete(int k) =0;
	virtual AbstractList<T>& Insert(int k,const T& x) =0;
	//virtual void Output(ostream& out) const =0;
};
#endif
//This is "Exception.h" header file
#ifndef EXCEPTION_H
#define EXCEPTION_H
#include <iostream>
#include <string>
using namespace std;
class OutOfBounds
{
	public:
		OutOfBounds();
		void PrintError(const string error)const;
	private:
		string error;
};

class NoMem
{
	public:
		NoMem();
		void PrintError(const string error)const;
	private:
		string error;
};

void OutOfBounds::PrintError(const string error) const
{
	cout<<error<<endl;
}

OutOfBounds::OutOfBounds()
{
	error="Out of the bounds of linear list!";
}

void NoMem::PrintError(const string error) const
{
	cout<<error<<endl;
}

NoMem::NoMem()
{
	error="No Member!";
}
#endif



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值