[C++]資料結構:基於鏈表結構的Chain類和遍曆器ChainIterator類

來源:互聯網
上載者:User
//基於鏈表的類Chain #include <iostream>#include<new.h>using namespace std;  //節點類,定義了每個節點的儲存類型和指標名稱template<class T>  class ChainNode{      public:          T data;          ChainNode<T> *link;  };  //鏈表類,封裝了鏈表操作的相應方法template<class T>  class Chain{      public:          Chain(){first=0;}//建構函式,頭結點指向空值        ~Chain();//解構函式bool IsEmpty()const{return first==0;}//判斷是否為空白int Length()const;//返回該鏈表的長度bool Find(int k,T&x)const;//返回第k個元素到x中int Search(const T&x)const;//返回x所在的位置          Chain<T>& Delete(int k,T& x);//刪除第k個元素並把它返回到x中Chain<T>& Insert(int k,const T&x);//在第k個元素之後插入x        void Output(ostream& out) const;//重載操作符的輸出函數ChainNode<T> *first;//指向第一個節點的指標ChainNode<T> *last;//指向最後一個節點的指標void Erase();void Zero(){first=0;};Chain<T>& Append(const T&x);}; //鏈表遍曆器類實現對鏈表的遍曆template<class T>class ChainIterator{public:T* Initialize(const Chain<T>&c){location = c.first;if(location){return &location->data;//為何有地址符?}return 0;}T* Next(){if(!location)return 0;location = location->link;if(location)return &location->data;return 0;}private:ChainNode<T>*location;};//鏈表的解構函式,用於刪除所有的鏈表中的節點template<class T>Chain<T>::~Chain(){Erase();}//清除掉鏈表中的所有元素並且釋放記憶體template<class T>void Chain<T>::Erase(){ChainNode<T>*next;//指向下一個節點while(first){next=first->link;delete first;first = next;}}//輸出鏈表template<class T>void Chain<T>::Output(ostream& out)const{ChainNode<T>*current;for(current=first;current;current=current->link){out<<current->data;if(!current->link){out<<""<<endl;}else{out<<",";}}}//重載操作符template<class T>ostream& operator<<(ostream& out,const Chain<T>&x){x.Output(out);return out;}class OutOfBounds{  public:  OutOfBounds(){  cout<<"Out Of Bounds!"<<endl;  }  };  //記憶體不足的異常類  class NoMem{      public:          NoMem(){              cout<<"No Memory!"<<endl;          }  };  //使new引發NoMem異常而不是xalloc異常  //如果要恢複原始行為可以做以下調用  //_set_new_handler(Old_Handler);  int my_new_handler(size_t size){      throw NoMem();  } //確認鏈表的長度template<class T>int Chain<T>::Length()const{ChainNode<T>*current = first;int length = 0;while(current){length++;current = current->link;}return length;}//在鏈表中尋找第k個元素//存在就儲存到x中//不存在則返回false,否則返回truetemplate<class T>bool Chain<T>::Find(int k,T&x)const{if(k<1)return false;ChainNode<T>*current = first;int index = 1;while(index<k&¤t){current = current->link;index++;}if(current){x = current->data;return true;}return false;}//在鏈表中搜尋//尋找x,如果發現則返回x的下標//如果x不存在則返回0template<class T>int Chain<T>::Search(const T&x)const{ChainNode<T>*current = first;int index = 1;while(current&¤t->data!=x){current = current->link;index++;}if(current){return index;}return 0;}//從鏈表中刪除一個元素//將第k個元素取至x//然後從鏈表中刪除第k個元素//如果不存在則引發異常OutOfBoundstemplate<class T>Chain<T>& Chain<T>::Delete(int k,T& x){if(k<1||!first){throw OutOfBounds();}ChainNode<T>*p = first;if(k==1){first = first->link;}else{ChainNode<T>*q = first;for(int index = 1;index<k-1&&q;index++){q = q->link;//此時q指向要刪除的前一個節點}if(!q||!q->link){throw OutOfBounds();}p = q->link;if(p==last)last=q;q->link=p->link;//從鏈表中刪除該節點x = p->data;delete p;return *this;}}//在第k個位置之後插入元素//不存在則報OutOfBounds異常//沒有足夠記憶體則報NoMem異常template<class T>Chain<T>& Chain<T>::Insert(int k,const T&x){if(k<0){throw OutOfBounds();}ChainNode<T>*p = first;for(int index = 1;index<k && p;index++){p = p->link;}if(k>0 && !p){throw OutOfBounds();}ChainNode<T>*y = new ChainNode<T>;y->data = x;if(k){y->link=p->link;p->link=y;}else{//作為第一個元素插入y->link = first;first = y;}if(!y->link)last=y;return *this;}//在鏈表右端添加一個資料template<class T>Chain<T>& Chain<T>::Append(const T&x){ChainNode<T>*y;y = new ChainNode<T>;y->data = x;y->link = 0;if(first){last->link = y;last = y;}else{first = last = y;}return *this;} int main(){  //設定當使用new建立對象存在空間不足的問題時調用自己的解決方案    _set_new_handler(my_new_handler);    try{          Chain<int>myList;            cout<<"myList : "<<myList<<endl;          cout<<"Length  = "<<myList.Length()<<endl;          cout<<"IsEmpty = "<<myList.IsEmpty()<<endl;            myList.Append(5);          myList.Append(0);          myList.Insert(1,2);          cout<<"myList : "<<myList<<endl;          cout<<"Length  = "<<myList.Length()<<endl;          cout<<"IsEmpty = "<<myList.IsEmpty()<<endl;            int f ;           myList.Find(1,f);//把第一個位置的元素賦值給z了          cout<<"First is "<<f<<endl;          cout<<"Length  = "<<myList.Length()<<endl;            myList.Delete(1,f);          cout<<"Delete is "<<f<<endl;  cout<<"myList : "<<myList<<endl;         cout<<"Length  = "<<myList.Length()<<endl;          cout<<"IsEmpty = "<<myList.IsEmpty()<<endl;  cout<<"Iterator : "<<endl; //使用遍曆器輸出int *x;ChainIterator<int>c;x=c.Initialize(myList);while(x){cout<<*x;x = c.Next();if(x){cout<<",";}else{cout<<""<<endl;}}cout<<endl;      }catch(exception e){          cout<<"Hey!An exception has occurred!";      }    return 0;  }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.