顺序表模板C++

来源:互联网 发布:js请求url获取返回值 编辑:程序博客网 时间:2024/06/04 18:11

C++ 顺序表模板

#include <iostream>template<class T>    class SeqList    {    private:        T *elem;        int length;          int MaxSize;    public:        SeqList(int InitSize);        ~SeqList();        void Clear();        bool IsEmpty() const        {            return (length == 0);        }        bool IsFull() const        {            return(length == MaxSize);        }        int Length() const;        T Get(int i) const;        int Next(int i) const;        int Prior(int i)const;        int Find(T e)const;        int Insert(int i, const T &e);        int Delete(T &e, int i);    };template <class T>SeqList<T>::SeqList(int InitSize){    if (InitSize > 0)    {        elem = new T[InitSize];        Exception(!elem, "there is no space in memory");        length = 0;        MaxSize = InitSize - 1;    }}template<class T>SeqList<T>::~SeqList(){    delete[] elem;}template<class T>void SeqList<T>::Clear(){    length = 0;}template<class T>int SeqList<T>::Length() const{    return length;}template<class T>T SeqList<T>::Get(int i) const{    Exception((i < 1) || (i > length), "out of bounds access");    return elem[i];}template<class T>int SeqList<T>::Next(int i)const{    Exception((i<1) || (i>length - 1), "Out of bounds access");    return i + 1;}template<class T>int SeqList<T>::Prior(int i)const{    Exception((i <= 1) || (i > length), "Out of bounds access");    return i - 1;}template<class T>int SeqList<T>::Find(T e) const{    elem[0] = e;    int i = length;    while (elem[i] != e)        i--;    return i;}template<class T>int SeqList<T>::Insert(int i, const T &e){    Exception((i<1) || (i>length + 1), "i is not correct");    Exception(MaxSize == length, "no space for new item");    for ( int j = length; j >= i; j--)        elem[j + 1] = elem[j];    elem[i] = e;    length++;    return 1;}template <class T>int SeqList<T>::Delete(T &e, int i){    Exception((i<1) || (i>length), "i is illgeal");    e = elem[i];    for (int j = i; j < length; j++)        elem[j] = elem[j + 1];    length--;    return i;}
0 0