C++实现数据结构四 单循环链表
2013-04-13 15:27
597 查看
ListNode.h
CircularList.h
template<typename Type> class CircularList; //不能写成:CircularList<Type> //重载操作数声明,注意和在类中友元证明方式的区别 //template<typename Type> class ListNode; //template<typename Type> //ostream& operator<< (ostream &os, ListNode<Type> &node); template<typename Type> class ListNode { private: friend class CircularList<Type>; friend ostream& operator<< <Type>(ostream&, ListNode<Type>&); ListNode(): next(NULL) {} ListNode(const Type item, ListNode<Type> *p=NULL ): data(item), next(p) {} ~ListNode() { next = NULL; } private: Type data; ListNode *next; }; template<typename Type> ostream& operator<< (ostream &os, ListNode<Type> &node) { os<<node.data; return os; }
CircularList.h
#include "ListNode.h" template<typename Type> class CircularList { public: CircularList() { head = new ListNode<Type>(); head->next = head; } ~CircularList() { MakeEmpty(); delete head; } public: void MakeEmpty(); int Length(); ListNode<Type> *Find(int n); bool Insert(Type item, int n=0); Type Remove(int n=0); bool RemoveAll(Type item); Type Get(int n); void Print(); private: ListNode<Type> *head; }; template<typename Type> void CircularList<Type>::MakeEmpty() { ListNode<Type> *pmove = head->next ; ListNode<Type> *pdel; while(pmove != head) { pdel=pmove; pmove = pmove->next; delete pdel; pdel = NULL; } } template<typename Type> int CircularList<Type>::Length() { ListNode<Type> *pmove = head; int count = 0; while(pmove->next != head) { ++count; pmove = pmove->next; } return count; } template<typename Type> ListNode<Type>* CircularList<Type>::Find(int n) { if(n<1) { cout<<"the n is illegal"<<endl; return NULL; } ListNode<Type> *pmove = head; for(int i=0; i<n; ++i) { pmove = pmove->next; if(pmove == head) { cout<<"the n is out of the boundary"<<endl; return NULL; } } return pmove; } template<typename Type> bool CircularList<Type>::Insert(Type item, int n=0) { if(n<0) { cout<<"The n is illegal"<<endl; } ListNode<Type> *pmove = head; for(int i=0; i<n; ++i) { pmove = pmove->next; if(pmove == head) { cout<<"the n is out of the boundary, so cannot insert the data"<<endl; return false; } } ListNode<Type> *pnode = new ListNode<Type>(); pnode->data = item; pnode->next = pmove->next; pmove->next = pnode; return true; } template<typename Type> Type CircularList<Type>::Remove(int n=0) { if(n < 1) { cout<<"the n is illegal"<<endl; return 0; } ListNode<Type> *pmove = head, *pdel; for(int i = 0; i< (n-1) && pmove->next != head ; ++i) { pmove= pmove->next; } if(pmove->next == head) { cout<<"the n is out of boundary"<<endl; return 0; } pdel = pmove->next; pmove->next = pdel->next; Type item = pdel->data; delete pdel; return item; } template<typename Type> bool CircularList<Type>::RemoveAll(Type item) { ListNode<Type> *pmove= head->next, *pdel, *p= head; int count =0; while(pmove != head) { //p->next = pmove; //p = p->next; if(pmove->data == item) { ++count; pdel =pmove; pmove = pmove->next; p->next =pmove; delete pdel; pdel = NULL; } else { pmove = pmove->next; p= p->next ; } } if(count == 0) return false; else return true; } template<typename Type> Type CircularList<Type>::Get(int n) { ListNode<Type> *p = Find(n); if(p == NULL) { cout<<"cannot get the data at "<<n<<endl; return 0; } return p->data; } template<typename Type> void CircularList<Type>::Print() { ListNode<Type> *pmove = head->next; cout<<"head-->"; while(pmove != head) { cout<<pmove->data<<"-->"; pmove = pmove->next; } cout<<"voer"<<endl; }
相关文章推荐
- c++实现数据结构4.双循环链表
- 用c++实现数据结构中的单链表
- 数据结构学习系列二-链表的C++实现
- 用C++实现数据结构二 带头结点的单链表
- C++实现数据结构三 双向循环链表
- 数据结构C++模板实现之----------------单向链表
- 数据结构 链表的lua实现 仿照C++中list 实现
- 【数据结构】用C++实现双循环链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
- c/c++ 数据结构-线性表(单链表基本操作的实现)
- 【C++数据结构学习笔记---线性表】用单链表实现线性表
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 数据结构:C++链表类及一元多项式的实现时遇到的困难
- 数据结构 带头结点的单链表 操作大全 最全的链表操作(c++实现)
- [数据结构]链表的实现-C++
- 简单数据结构之循环链表(C++实现)
- 数据结构-C++实现(一):数组链表
- C/C++基本数据结构:链表的基本使用和实现
- 数据结构-C++实现(二):单向链表
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
- 数据结构(3)单循环链表 c++ 模板实现