#ifndef LINEARLIST_H #define LINEARLIST_H //enum bool{true,false}; template<class T> class LinearList{ public: LinearList(); //constructor; ~LinearList(); //destructor; virtual int Size()const=0; //get the size of a list; virtual int Length()const=0; //get the length of a list; virtual int Search(T &x)const=0; //search the value x from a list; virtual int Locate(int i)const=0; //get the locate number of index i of a list; virtual T*getData(int i)const=0; //get the value of index i of a list; virtual void setData(int i,T& x)=0; //replace the place of index i x; virtual bool Insert(int i,T& x)=0; //insert a value x at index i place into a list; virtual bool Remove(int i, T& x)=0; //remove the value of index i place and return the value by x; virtual bool IsFull()const=0; //judge a list is full; virtual bool IsEmpty()const=0; //judge a list is empty; virtual void Sort()=0; //sort a list; virtual void input()=0; //input; virtual void output()=0; //output; virtual LinearList<T> operator=(LinearList<T>& L)=0; //copy a list to a new list; }; #endif #ifndef SEQLIST_H #define SEQLIST_H #include "LinearList.h" #include <iostream> #include <exception> #include <new> using namespace std; const int defaultSize=100; const int increment=10; template <class T> class SeqList:public LinearList<T>{ protected: T *data; int maxSize; int last; void reSize(); public: SeqList(int sz=defaultSize); //constructor; SeqList(SeqList<T>& L); //copy constructor; ~SeqList(){delete[]data} //destructor; int Size()const{return maxSize;} //return the max size of the list; int Length()const{return last+1;} //return the length of the list; int Search(T& x)const; //search a element from a list; int Locate(int i)const; //location; T*getData(int i)const{return(i>0&&i<=last+1)?&data[i-1]:NULL} //get a element if it is in list; void setData(int i,T& x){if(i>0&&i<last+1)data[i-1]=x;} //set a element x if its place exists; bool Insert(int i,T& x); //insert x into a list; bool Remove(int i, T& x); //remove a element from a list; bool IsFull()const{return(last+1==maxSize)?true:false;} //judge that list is full; bool IsEmpty()const{return(last==-1)?true:false;} //judge that list is empty; void input(); //input; void output(); //output; SeqList<T>operator=(SeqList<T>& L); //copy the whole list; }; template<class T> SeqList<T>::SeqList(int sz){ if(sz>0){ maxSize=sz; last=-1; try{ data=new T[maxSize]; } catch(std::bad_alloc c){ cout<<c.what()<<endl; exit(1); } } } template<class T> SeqList<T>::SeqList(SeqList<T>& L){ maxSize=L.Size(); last=L.Length()-1; try{ data=new T[maxSize]; } catch(std::bad_alloc c){ cout<<c.what()<<endl; exit(1); } for(int i=1;i<last+1;++i){ data[i-1]=L.getData(i); } } template<class T> void SeqList<T>::reSize(){ int newSize=maxSize+increment; try{ T *newarray=new T[newSize]; } catch(std::bad_alloc c){ cout<<c.what()<<endl; exit(1); } int n=last+1; T*srcptr=data; T*destptr=newarray; while(n--)*destptr++=*srcptr++; delete[]data; data=newarray; maxSize=newSize; } template<class T> int SeqList<T>::Search(T& x)const{ for(int i=0;i<=last;++i){ if(data[i]==x)return i+1; } return 0; } template<class T> int SeqList<T>::Locate(int i)const{ if(i>=1&&i<=last+1)return i; else return 0; } template<class T> bool SeqList<T>::Insert(int i,T& x){ if(IsFull()){ try{ reSize(); } catch(std::exception e){ cout<<e.what()<<endl; return false; } } for(int j=last;j>=i-1;--j){ data[j+1]=data[j]; } data[i-1]=x; ++last; return true; } template<class T> bool SeqList<T>::Remove(int i, T& x){ if(IsEmpty())return false; if(i<1;i>last+1)return false; x=data[i-1]; for(int j=i;j<=last;++j){ data[j-1]=data[j]; } --last; return true; } template<class T> void SeqList<T>::input(){ T tmp; last=0; while(cin>>tmp){ if(tmp=="/n")break; try(IsFull()){ reSize(); } catch(std::exception e){ cout<<e.what()<<endl; exit(1); } data[last]=tmp; ++last; } } template<class T> void SeqList<T>::output(){ if(IsEmpty())cerr<<"no element."<<endl; for(int i=0;i<=last;++i)cout<<data[i]<<endl; } template<class T> SeqList<T> SeqList<T>::operator=(SeqList<T>& L){ maxSize=L.Size(); last=L.Length()-1; try{ data=new T[maxSize]; } catch(std::bad_alloc c){ cout<<c.what()<<endl; exit(1); } for(int i=1;i<last+1;++i){ data[i-1]=L.getData(i); } } #endif