#include<iostream> using namespace std; template<class Elem> class List{ //Renitialize the list. The client is responsible for //reclaiming the storage used by the list elements. virtual void clear()=0; //Insert an element at the front of the right partition //Return true if successfull,false if the list if full. virtual bool insert(const Elem&)=0; //Append an element at the end of the right partition.Return //true if successful,false if the list if full. virtual bool append(const Elem&)=0; //Remove the first element of the right partition.Return true //if seccessful,false if the right partition is empty. //The element removed is returned in the parameter. virtual bool remove(Elem&)=0; //Place fence at list start,making left partition empty. virtual void setStart()=0; //Place fence at list end,making right partition empty. virtual void setEnd()=0; //Move fence one step left;no change if already at start virtual void prev()=0; //Move fende one step right;no change if already at end virtual void next()=0; //Return length of left partition virtual int leftLength()=0; //Return length of right partition virtual int rightLength()=0; //If pos and more elements are in the list,set the size of left partition //to pos and return true.Otherwise,do nothing and return false. virtual bool setPos(int pos)=0; //Return in first parameter the first element of the right partition. //Return true if successful,flase if the right partition is empty. virtual bool getValue(Elem&)=0; //Print the contents of the list virtual void print() const=0; };
template<class Elem> class Alist : public List<Elem>{ private: int maxSize; //Maximum size of elements in list int listSize; //Actual number of elements in list int fence; //position of fence Elem *listArray; //array holding list elements public: Alist(int size=10){ maxSize=size; listSize=fence=0; listArray=new Elem[maxSize]; } ~Alist() { delete [] listArray;} //Destructor void clear() { delete [] listArray; listSize=fence=0; listArray= new Elem[maxSize]; } bool insert(const Elem&); bool append(const Elem&); bool remove(Elem&); void setStart(){fence=0;} void setEnd(){ fence=listSize;} void prev() { if(fence!=0) fence--;} void next() { if(fence<=listSize) fence++;} int leftLength() const { return fence;} int rightLength() const { return listSize-fence;} bool setPos(int pos){ if(pos>0)&&(pos<=listSize) fence=pos; return (pos>0)&&(pos<=listSize); } bool getValue(Elem& it) const{ if(rightLength()==0) return false; else (it=listArray[fence]) return true; } void print() const{ int temp=0; cout<<"<"; while(temp<fence) cout<<listArray[temp++]<<" "; cout<<"|"; while(temp<listSize) cout<<listArray[temp++]<<" "; cout<<">\n"; } };
template<class Elem> bool Alist<Elem>::insert(const Elem& item){ if(listSize==maxSize) return false; for(int i=listSize;i>fence;i--) listArray[i]=listArray[i-1]; listArray[fence]=item; listSize++; return true; } template<class Elem> bool Alist<Elem>::append(const Elem& item){ if(listSize==maxSize)return false; listArray[listSize++]=item; return true; }
template<class Elem> bool Alist<Elem>::remove(Elem& it){ if(rightLength()==0) return false; it=listArray[fence]; for(int i=fence;i<listSize-1;i++) listArray[i]=listArray[i+1]; return true; }
int main(){ Alist<int>& list; //为什么这里会出错呢,小弟百思不得其截望高手指教 int i; cin>>i; while(i!=-999){ list.append(i); cin>>i; } return 0; } cpp(115) : error C2530: 'list' : references must be initialized