C++循环链表(殷人昆版)
2015-12-30 15:56
417 查看
<span style="font-size:18px;"><strong><strong><span style="font-size:18px;">#include <iostream> #include <string.h> #include <stdlib.h> using namespace std; template<class T> struct CircListNode { T data; CircListNode<T> *next; CircListNode(CircListNode<T> *n = NULL) :next(n) {} CircListNode(T d, CircListNode<T> *n = NULL) :data(d) ,next(n) {} }; template<class T> class CircList { public: CircList() { first = new CircListNode<T>; first->next = first; } CircList(const T& x) { first = new CircListNode<T>(x); first->next = first; } CircList(CircList<T> & L) { T value; CircListNode<T> *srcptr = L.getHead(); CircListNode<T> *destptr = first = new CircListNode<T>; while(srcptr->next != first) { value = scrptr->next->data; destptr->next = new CircListNode<T>(value); destptr = destptr->next; srcptr = srcptr->next; } destptr->next = first; } ~CircList() { //makeEmpty(); delete first; } void makeEmpty() //将链表置空 { if(IsEmpty()) { return; } CircListNode<T> *p = NULL; while(first->next != first) { p = first->next; first->next = p->next; delete p; } } int Length() const//计算循环链表的长度 { int count = 0; CircListNode<T> * p = first; while(p->next != first) { count++; p = p->next; } return count; } bool IsEmpty()//判空 { if(first->next == first) { return true; } return false; } CircListNode<T> *getHead() const//返回附加头节点的地址 { return first; } void setHead(CircListNode<T> *p)//设置附加头结点地址 { first = p; } CircListNode<T> *Serach(T x)//搜索含数据x的元素 { if(IsEmpty()) { return NULL; } CircListNode<T> * p = first->next; while(p != first) { if(p->data == x) { return p; } p = p->next; } return p; } CircListNode<T> *Locate(int i)//搜索第i个元素的地址 { if(IsEmpty()||i < 0) { return NULL; } CircListNode<T> * current = first->next; int k = 0; while(current != first && k < i) { current = current->next; k++; } return current; } T *getData(int i) //取出第i个元素的值 { CircListNode<T> * current = Locate(i); if(current == NULL) { return NULL; } return &(current->data); } void setData(int i, T& x)//用x修饰第i个元素的值 { CircListNode<T> * current = Locate(i); if(current == NULL) { return; } current->data = x; } bool Insert(int i, T& x)//在第i个元素后插入x { CircListNode<T> * current = Locate(i); if(current == NULL) { return false; } CircListNode<T> * s = new CircListNode<T>(x); s->next = current->next; current->next = s; return true; } bool Remove(int i, T& x)//删除第i个元素,x 返回该元素的值 { if(IsEmpty()) { return false; } CircListNode<T> * current = Locate(i-1); if(current == NULL && current->next == first) { return false; } CircListNode<T> * del = current->next; current->next = del->next; x = del->data; delete del; return true; } void PushBack(const T& x) { CircListNode<T> * p = first; while(p->next!= first) { p = p->next; } CircListNode<T> *s = new CircListNode<T>(x); p->next = s; s->next = first; } void input() //输入 { T x; cout << "请输入表中元素:"; while((cin >> x) && x) { PushBack(x); } } void output() //输出 { CircListNode<T> * p = first; while(p->next != first) { cout << p->next->data << "-->"; p = p->next; } cout << "First" << endl; } CircList<T> &operator=(CircList<T> &L)//重载函数,赋值 { T value; CircListNode<T> *srcptr = L.getHead(); CircListNode<T> *destptr = first = new CircListNode<T>; while(srcptr->next != first) { value = scrptr->next->data; destptr->next = new CircListNode<T>(value); destptr = destptr->next; srcptr = srcptr->next; } destptr->next = first; return *this; } private: CircListNode<T>* first; };</span></strong></strong></span>
还有其约瑟夫环的实现:
<span style="font-size:18px;"><strong>template<class T> void Josephus(CircList<T> &Js, int n, int m) { CircListNode<T> * p = Js.Locate(0); CircListNode<T> * pre = NULL; int i,j; for(i = 0; i < n-1; ++i) { for(j = 1; j < m; ++j) { pre = p; p = p->next; if(p->data == -1)//略过头结点 { p = p->next; } } cout << "出列的人是:" << p->data << endl; pre->next = p->next; delete p; p = pre->next; } cout <<"剩下的人是:" << p->data << endl; } int main() { CircList<int> clist(-1); int i,n,m; cout << "输入游戏者人数和报数间隔:"; cin >> n >> m; for(i = 1; i <= n; ++i) { clist.PushBack(i); } Josephus(clist, n, m); return 0; }</strong></span>
相关文章推荐
- C++单链表(殷人昆版)
- C++顺序表(殷人昆版)
- c语言_FILE结构体解释及相关操作
- C++开发人脸性别识别教程(3)——OpenCv配置和ImageWatch插件介绍
- C/C++中extern关键字详解
- C++ 学习(使用 using 改写继承类继承成员的性质)
- C++小题(六)
- C++ 学习(派生类到基类准还的可访问性)
- C++开发人脸性别识别教程(2)——VisualStudio初探
- 测试一下
- c++ libjson相关资料
- 如何成为一个C++高级程序员
- 一份C++线程池的代码,非常实用
- OC语言使用“消息结构”(messaging structure)而非“函数调用”(function calling)
- c语言ispunct函数
- 站在巨人的肩膀上,C++开源库大全
- C&C++编译(1)
- Java与C/C++有什么区别
- Java与C/C++有什么区别
- C语言中联合体union的使用