peterboboo 发表于 2013-10-25 21:42:44

c++数组实现顺序表(模板修正),求指教。再次感谢仰望天上的光!

本帖最后由 peterboboo 于 2013-10-25 21:47 编辑

/*
* *Created on: 2013-10-25 *      Author: Administrator */#ifndef SEQLIST_H_#define SEQLIST_H_#include <ostream>using namespace std;const int maxsize=255;template <class T>class SeqList {private:        T data;        int nlength;public:        int Length() const;        T Get(int nindex);        bool Insert(int nindex,T value);        int Locate(T value);        bool Delete(int nindex);        bool IsEmpty() const;        void Erase() const;public:        SeqList();        ~SeqList();};template <class T>SeqList<T>::SeqList() {        // TODO Auto-generated constructor stub        nlength=0;}template <class T>inline int SeqList<T>::Length() const{        return nlength;}template <class T>T SeqList<T>::Get(int nindex){        if(0>nindex||nlength<nindex)                return 0;        return data;}template <class T>bool SeqList<T>::Insert(int nindex,T value){        if(nlength==maxsize)                return false;        if(nindex<0||nindex>nlength)                return false;        if(nindex==nlength)        {                data=value;        }        else        {                for(int n=nlength+1;n>nindex;n--)                {                        data=data;                }                data=value;        }        nlength++;        return true;}template <class T>int SeqList<T>::Locate(T value){        if(nlength==0)                return 0;        int ncount=Length();        while(ncount--)        {                if(data==value)                        break;        }        return ncount;}template <class T>bool SeqList<T>::Delete(int nindex){        int ncount=Length();        if(nindex<0||nindex>ncount) return false;        for(int n=nindex;n<ncount;n++)        {                data=data;        }        nlength--;        return true;}template <class T>inline bool SeqList<T>::IsEmpty() const{        return(nlength==0);}template <class T>inline void SeqList<T>::Erase() const{        nlength=0;}template <class T>SeqList<T>::~SeqList() {        // TODO Auto-generated destructor stub}template <class T>ostream &operator<<(ostream &output,SeqList<T> &p){        int ncount=p.Length();        int npos=0;        while(npos<ncount)        {                output<<p.Get(npos++);                output<<endl;        }        return output;}#endif /* SEQLIST_H_ */
页: [1]
查看完整版本: c++数组实现顺序表(模板修正),求指教。再次感谢仰望天上的光!