Sequential List(顺序表)

来源:互联网 发布:淘宝运费险价格表 编辑:程序博客网 时间:2024/05/16 16:13

//本文件是顺序表(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;}//destructorbool 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不在表中,则返回0for(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



原创粉丝点击