正文

单循环链表--实现2009-04-01 22:21:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/mojiaojiaozhu/41908.html

分享到:

#include<iostream>using namespace std; template<class Type> class CircularList; template<class Type> class CircListNode{private: friend class CircularList<Type>; CircListNode():link(NULL){} CircListNode(const Type item,CircListNode<Type> *next=NULL):m_data(item),link(next){} ~CircListNode(){  link=NULL; } private: Type m_data; CircListNode *link;};template<class Type> class CircularList{public: CircularList():first(new CircListNode<Type>()){  first->link=first;  } ~CircularList(){  MakeEmpty();  delete first; }public: void MakeEmpty(); //clear the list int Length();  //get the length CircListNode<Type> *Find(Type value,int n); //find the nth data which is equal to value CircListNode<Type> *Find(int n);   //find the nth data bool Insert(Type item,int n=0);   //insert the data into the nth data of the list Type Remove(int n=0);     //delete the nth data bool RemoveAll(Type item);    //delete all the datas which are equal to value Type Get(int n); //get the nth data void Print();  //print the list private: CircListNode<Type> *first; }; template<class Type> void CircularList<Type>::MakeEmpty(){ CircListNode<Type> *pdel,*pmove=first; while(pmove->link!=first){  pdel=pmove->link;  pmove->link=pdel->link;  delete pdel; }} template<class Type> int CircularList<Type>::Length(){ CircListNode<Type> *pmove=first; int count=0; while(pmove->link!=first){  pmove=pmove->link;  count++; } return count;} template<class Type> CircListNode<Type>* CircularList<Type>::Find(int n){ if(n<0){  cout<<"The n is out of boundary"<<endl;  return NULL; } CircListNode<Type> *pmove=first->link; for(int i=0;i<n&&pmove!=first;i++){  pmove=pmove->link; } if(pmove==first){  cout<<"The n is out of boundary"<<endl;  return NULL; } return pmove;} template<class Type> CircListNode<Type>* CircularList<Type>::Find(Type value,int n){ if(n<1){  cout<<"The n is illegal"<<endl;  return NULL; } CircListNode<Type> *pmove=first; int count=0; while(count!=n){  pmove=pmove->link;  if(pmove->m_data==value){   count++;  }  if(pmove==first){   cout<<"can't find the element"<<endl;   return NULL;  } } return pmove;} template<class Type> bool CircularList<Type>::Insert(Type item, int n){ if(n<0){  cout<<"The n is out of boundary"<<endl;  return 0; } CircListNode<Type> *pmove=first; CircListNode<Type> *pnode=new CircListNode<Type>(item); if(pnode==NULL){  cout<<"Application error!"<<endl;  exit(1); } for(int i=0;i<n;i++){  pmove=pmove->link;  if(pmove==first){   cout<<"The n is out of boundary"<<endl;   return 0;  } }  pnode->link=pmove->link; pmove->link=pnode; return 1;} template<class Type> bool CircularList<Type>::RemoveAll(Type item){ CircListNode<Type> *pmove=first; CircListNode<Type> *pdel=first->link; while(pdel!=first){  if(pdel->m_data==item){   pmove->link=pdel->link;   delete pdel;   pdel=pmove->link;   continue;  }  pmove=pmove->link;  pdel=pdel->link; } return 1;} template<class Type> Type CircularList<Type>::Remove(int n){ if(n<0){  cout<<"can't find the element"<<endl;  exit(1); } CircListNode<Type> *pmove=first,*pdel; for(int i=0;i<n&&pmove->link!=first;i++){  pmove=pmove->link; } if(pmove->link==first){  cout<<"can't find the element"<<endl;  exit(1); } pdel=pmove->link; pmove->link=pdel->link; Type temp=pdel->m_data; delete pdel; return temp;} template<class Type> Type CircularList<Type>::Get(int n){ if(n<0){  cout<<"The n is out of boundary"<<endl;  exit(1); } CircListNode<Type> *pmove=first->link; for(int i=0;i<n;i++){  pmove=pmove->link;  if(pmove==first){   cout<<"The n is out of boundary"<<endl;   exit(1);  } } return pmove->m_data;} template<class Type> void CircularList<Type>::Print(){ CircListNode<Type> *pmove=first->link; cout<<"first"; while(pmove!=first){  cout<<"--->"<<pmove->m_data;  pmove=pmove->link; } cout<<"--->over"<<endl<<endl<<endl;} using namespace std; int main(){ CircularList<int> list; for(int i=0;i<20;i++){  list.Insert(i*3,i); } cout<<"the Length of the list is "<<list.Length()<<endl; list.Print(); for(i=0;i<5;i++){  list.Insert(3,i*3); } cout<<"the Length of the list is "<<list.Length()<<endl; list.Print();  list.Remove(5); cout<<"the Length of the list is "<<list.Length()<<endl; list.Print();  list.RemoveAll(3); cout<<"the Length of the list is "<<list.Length()<<endl; list.Print();  cout<<"The third element is "<<list.Get(3)<<endl;  list.MakeEmpty(); cout<<"the Length of the list is "<<list.Length()<<endl; list.Print();  return 0;}  

阅读(2828) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册