| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1237 人关注过本帖
标题:顺序表的问题
取消只看楼主 加入收藏
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
 问题点数:0 回复次数:4 
顺序表的问题

#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

搜索更多相关主题的帖子: 顺序 
2005-01-30 16:54
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
得分:0 
谢谢kai,我知道那里错了,我会进一步改进程序的
2005-02-01 13:35
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
得分:0 

但是我的链表实现也出现很多问题,就是根据上面的程序改的,请kai看下: #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() const=0; //Return length of right partition virtual int rightLength() const=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&) const=0; //Print the contents of the list virtual void print() const=0; }; template<class Elem> class Link{ public: Elem element; Link *next; Link(const Elem& elemval,Link* nextval=NULL) {element=elemval;next=nextval;} Link(Link* nextval=NULL){next=nextval;} }; template<class Elem> class LList:public List<Elem>{ private: Link<Elem> *head; Link<Elem> *tail; Link<Elem> *fence; int leftLength; int rightLength; void init(){ fence=head=tail=new Link<Elem>; leftLength=rightLength=0; } void removeall(){ //Return link nodes to free store while(head!=NULL){ fence=head; head=head->next; delete fence; } } public: LList(int size=10){init();} ~LList{removeall();} void clear(){removeall();init();} bool insert(const Elem& ); bool append(const Elem& ); bool remove(Elem&); void setStart() { fence=head;rightLength+=leftLength;leftLength=0;} void setEnd() { fence=tail;leftLength+=rightLength;rightLength=0;} void prev(); void next(){ if(fence!=tail) { fence=fence->next;rightLength--;leftLength++;} } int leftLength() const {return leftLength;} int rightLength() const { return rightLength;} bool setPos(int pos); bool getValue(Elem& it) const{ if(tightLength()==0) return false; it=fence->next->element; return true; } void print() const; };

template<class Elem> bool LList<Elem>::insert(const Elem& item) { fence->next=new Link<Elem>(item,fence->next); if(tail==fence) tail=fence->next; rightLength++; return true; } template<class Elem> bool LList<Elem>::append(const Elem& item) { tail=tail->next=new Link<Elem>(item,NULL); rightLength++; return true; }

template<class Elem> bool LList<Elem>::remove(Elem& it){ if(fence->next==NULL) return false; it=fence->next->element; link<Elem>* ltemp=fence->next; fence->next=ltemp->next; if(ltemp==tail) tail=fence; delete ltemp; rightLength--; return true; } template<class Elem> void LList<Elem>::prev() { Link<Elem>* temp=head; if(fence==head) return; while(temp->next!=fence) temp=temp->next; fence=temp; leftLength--;rightLength++; }

template<class Elem> bool LList<Elem>::setPos(int pos) { if(pos<0||pos>rightLength+leftLength) return false; fence=head; for(int i=0;i<pos;pos++) fence=fence->next; return true; }

template<class Elem> void LList<Elem>::print() const { Link<Elem>* temp=head; cout<<"<"; while(temp!=fence){ cout<<temp->next->element<<" "; temp=temp->next; } cout<<"|"; while(temp->next!=NULL) { cout<<temp->next->element<<" "; temp=temp->next; } cout<<">\n"; }

int main() { int A[10]={1,2,3,4,5,6,7,8,9,10}; LList<Elem> list; for(int i=0;i<10;i++) list.append(A[i]); list.setPos(5); list.print(); system("pause"); return EXIT_SUCCESS; } //链表.obj - 10 error(s), 0 warning(s)

2005-02-01 13:54
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
得分:0 
我是把一组数据分成左右两个部分,如&lt;1,2,3,4,5|6,7,8,9,10&gt;。fence就表示中间这条"|",leftLength和rightLength分别表示左右这两部分的数据个数。程序我会继续改进的,谢谢kai。
2005-02-02 12:05
骇客
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-11-9
收藏
得分:0 
我发现错误所在呢,我真是粗心啊。
template&lt;class Elem&gt;
bool LList&lt;Elem&gt;::setPos(int pos)
{
   if(pos&lt;0 || pos &gt; right_length + left_length)
      return false;
   fence = head;
   for(int i = 0; i &lt; pos; pos++)            //应该是for(int i=0;i&lt;pos;i++)

      fence = fence-&gt;next;
   return true;
}

这回没问题了,我觉得逻辑上还是没有错误的:
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

template&lt;class Elem&gt;
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 &amp;)=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 &amp;)=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 &amp;)=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 fence one step right;no change if already at end
virtual void next()=0;
//Return length of left partition
virtual int leftLength() const=0;
//Return length of right partition
virtual int rightLength() const=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&amp;) const=0;
//Print the contents of the list
virtual void print() const=0;
};

template&lt;class Elem&gt;
class Link
{
public:
  Elem element;
  Link * next;
  Link(const Elem &amp; elemval, Link * nextval = NULL)
  {
     element = elemval;
     next = nextval;
  }
  Link(Link * nextval = NULL)
  {
     next = nextval;
  }
};

template&lt;class Elem&gt;
class LList : public List&lt;Elem&gt;
{
private:
   Link&lt;Elem&gt; * head;
   Link&lt;Elem&gt; * tail;
   Link&lt;Elem&gt; * fence;
   int left_length;
   int right_length;
   void init()
   {
      fence = head = tail = new Link&lt;Elem&gt;;
      left_length = right_length = 0;
   }
   void removeall()
   {   //Return link nodes to free store
      while(head != NULL)
      {
         fence = head;
         head = head-&gt;next;
         delete fence;
      }
   }
public:
   LList(int size = 10)
   {
      init();
   }
   ~LList()
   {
      removeall();
   }
   void clear()
   {
      removeall();
      init();
   }
   bool insert(const Elem &amp; );
   bool append(const Elem &amp; );
   bool remove(Elem &amp;);
   void setStart()
   {  
      fence = head;
      right_length += left_length;
      left_length=0;
   }
   void setEnd()
   {  
      fence = tail;
      left_length += right_length;
      right_length = 0;
   }
   void prev();
   void  next()
   {
      if(fence != tail)
      {
         fence = fence-&gt;next;
         right_length--;
         left_length++;
      }
   }
   int leftLength() const
   {
      return left_length;
   }
   int rightLength() const
   {
      return right_length;
   }
   bool setPos(int pos);
   bool getValue(Elem &amp; it) const
   {
      if(rightLength() == 0)
         return false;
      it = fence-&gt;next-&gt;element;
      return true;
   }
   void print() const;
};

template&lt;class Elem&gt;
bool LList&lt;Elem&gt;::insert(const Elem &amp; item)
{
   fence-&gt;next = new Link&lt;Elem&gt;(item,fence-&gt;next);
   if(tail == fence)  
      tail = fence-&gt;next;
   right_length++;
   return true;
}
template&lt;class Elem&gt;
bool LList&lt;Elem&gt;::append(const Elem&amp; item)
{
   tail = tail-&gt;next = new Link&lt;Elem&gt;(item, NULL);
   right_length++;
   return true;
}

template&lt;class Elem&gt;
bool LList&lt;Elem&gt;::remove(Elem &amp; it)
{
   if(fence-&gt;next == NULL)
      return false;
   it = fence-&gt;next-&gt;element;
   Link&lt;Elem&gt; * ltemp = fence-&gt;next;
   fence-&gt;next = ltemp-&gt;next;
   if(ltemp == tail)
      tail = fence;
   delete ltemp;
   right_length--;
   return true;
}
template&lt;class Elem&gt;
void LList&lt;Elem&gt;::prev()
{
   Link&lt;Elem&gt; * temp = head;
   if(fence == head)
      return;
   while(temp-&gt;next != fence)
      temp = temp-&gt;next;
   fence = temp;
   left_length--;
   right_length++;
}

template&lt;class Elem&gt;
bool LList&lt;Elem&gt;::setPos(int pos)
{
   if(pos&lt;0 || pos &gt; right_length + left_length)
      return false;
   fence = head;
   for(int i = 0; i &lt; pos; i++)
      fence = fence-&gt;next;
   return true;
}

template&lt;class Elem&gt;
void LList&lt;Elem&gt;::print() const
{
   Link&lt;Elem&gt; * temp = head;
   cout&lt;&lt;"&lt;";
   while(temp != fence)
   {
      cout&lt;&lt;temp-&gt;next-&gt;element&lt;&lt;" ";
      temp = temp-&gt;next;
   }
   cout&lt;&lt;"|";
   while(temp-&gt;next != NULL)
   {
      cout&lt;&lt;temp-&gt;next-&gt;element&lt;&lt;" ";
      temp = temp-&gt;next;
   }
   cout&lt;&lt;"&gt;\n";
}

int main()
{
   int A[10] = {1,2,3,4,5,6,7,8,9,10};
   LList&lt;int&gt; list;
   for(int i=0; i&lt;10; i++)
      list.append(A[i]);
   list.setPos(5);         //现在没问题了
                                 
   list.print();
   system("pause");
   return EXIT_SUCCESS;
}
2005-02-02 12:47
快速回复:顺序表的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026080 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved