![]() |
#2
kai2005-01-31 21:51
#include <iostream>
#include <cstdlib> 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 // here your code is something wrong. virtual int leftLength() const = 0; // the declaretion should be same, namely, const can not be forgotten //Return length of right partition // here your code is something wrong virtual int rightLength() const = 0; // the reason is the same //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. // here your code is something wrong. virtual bool getValue(Elem &) const = 0; // the reason is the same //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 true; } else return false; } bool getValue(Elem & it) const { if(rightLength()==0) return false; // else (it=listArray[fence]) return true; 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() { // when you use reference, it must be initialized. // and I don't think, it is necessary, here to use reference. Alist<int>list; //ΪʲôÕâÀï»á³ö´íÄØ£¬Ð¡µÜ°Ù˼²»µÃÆä½ØÍû¸ßÊÖÖ¸½Ì int i, value; cin>>i; while(i!=-999) { list.append(i); list.getValue(value); cout<<value<<" "; cin>>i; } system("pause"); return EXIT_SUCCESS; } 你的程序有问题,比如 insert() , 我帮你修改以后,只是在语法上合理了。 另外, list 的内部机制为双链表,即 *left, *right. 采用这种机制,使得insert an element 时,不需要对其他元素作移位处理,而通常的Vector container 需要做移位处理。 |
#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