|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 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[maxsize]; 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[nindex]; } 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[nindex]=value; } else { for(int n=nlength+1;n>nindex;n--) { data[n]=data[n-1]; } data[nindex]=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[ncount]==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[n]=data[n+1]; } 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_ */
|
|