今天我们来看下一个新概念:循环链表。那么什么是 循环链表呢?从概念上来说,任意数据元素都有一个前驱和一个后继,所有的数据元素的关系构成一个逻辑上的环;在其实现上,循环链表是一种特殊的单链表,它的尾结点的指针域保存了首结点的地址。下来我们以图解的方式来看看循环链表的逻辑构成方式,如下
下来我们来看看循环链表的继承层次结构,如下
它和我们之前实现的 StaticLinkList 是同一个层次的,都继承于 LinkList 。
接下来我们来看看循环链表的实现思路:
1、通过模板定义 CircleLinkList 类,继承自 LinList 类;
2、定义内部函数 last_to_first(),用于将单链表首尾相连;
3、特殊处理:首元素的插入操作和删除操作;
4、重新实现:清空操作和遍历操作。
那么循环链表实现的要点如下:
A、插入位置为 0 时:
-- 头结点和尾结点均指向新结点;
-- 新结点成为首结点插入链表;
B、删除位置为 0 时:
-- 头结点和尾结点指向位置为 1 的结点;
-- 安全销毁首结点;
下来我们就来看看循环链表的具体实现,如下
CircleLinkList.h 源码
#ifndef CIRCLELINKLIST_H #define CIRCLELINKLIST_H #include "LinkList.h" namespace DTLib { template < typename T > class CircleLinkList : public LinkList<T> { protected: typedef typename LinkList<T>::Node Node; int mod(int i) const { return (this->m_length == 0) ? 0 : (i % this->m_length); } Node* last() const { return this->position(this->m_length-1)->next; } void last_to_first() const { last()->next = this->m_header.next; } public: bool insert(const T& e) { return insert(this->m_length, e); } bool insert(int i, const T& e) { bool ret = true; i = i % (this->m_length + 1); ret = LinkList<T>::insert(i, e); if( ret && (i == 0) ) { last_to_first(); } return ret; } bool remove(int i) { bool ret = true; i = mod(i); if( i == 0 ) { Node* toDel = this->m_header.next; if( toDel != NULL ) { this->m_header.next = toDel->next; this->m_length--; if( this->m_length > 0 ) { last_to_first(); if( this->m_current == toDel ) { this->m_current = toDel->next; } } else { this->m_header.next = NULL; this->m_current = NULL; } this->destroy(toDel); } else { ret = false; } } else { ret = LinkList<T>::remove(i); } return ret; } bool set(int i, const T& e) { return LinkList<T>::set(mod(i), e); } T get(int i) const { return LinkList<T>::get(mod(i)); } T get(int i, const T& e) const { return LinkList<T>::get(mod(i), e); } int find(const T& e) const { int ret = -1; Node* slider = this->m_header.next; for(int i=0; i<this->m_length; i++) { if( slider->value == e ) { ret = i; break; } slider = slider->next; } return ret; } void clear() { while( this->m_length > 1 ) { remove(1); // 效率最大化 } if( this->m_length == 1 ) { Node* toDel = this->m_header.next; this->m_header.next = NULL; this->m_length = 0; this->m_current = NULL; this->destroy(toDel); } } bool move(int i, int step) { return LinkList<T>::move(mod(i), step); } bool end() { return (this->m_length == 0) || (this->m_current == NULL); } ~CircleLinkList() { clear(); } }; } #endif // CIRCLELINKLIST_H
我们下来用一个问题来验证下我们上面写的循环链表是否正确。此问题是:约瑟夫环问题。
那么什么是约瑟夫环问题呢?已知 n 个人(以编号0、1、2、3、...、n-1分别表示)围坐在一张圆桌周围。从编号为 k 的人开始报数,数到 m 的那个人出列;他的下一个人又开始从 1 报数,数到 m 的那个人又出列;依次规律重复下去,直到圆桌周围的人全部出列。那么为什么会有这个问题呢?这其中有个小故事:据说著名犹太历史学家 Josephus 有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与 Josephus 及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第 1 个人开始报数,每报数到第 3 人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而 Josephus 和他的朋友并不想遵从。那么,一开始要站在什么地方才能避免被处决?这时我们便可以利用循环列表来解决这个问题了,测试代码如下
#include <iostream> #include "CircleLinkList.h" using namespace std; using namespace DTLib; void josephus(int n, int s, int m) { CircleLinkList<int> c1; for(int i=1; i<=n; i++) { c1.insert(i); } c1.move(s-1, m-1); while (c1.length() > 0 ) { c1.next(); cout << c1.current() << endl; c1.remove(c1.find(c1.current())); } } int main() { josephus(41, 1, 3); return 0; }
我们来编译运行看看结果
我们看到最后两个数字是 16 和 31,也就是说。要是 Josephus 及他的朋友一开始便站在 16 和 31 的位置上,那么他们将幸免于难。通过今天对循环链表的学习,总结如下:1、循环链表为一种特殊的单链表;2、尾结点的指针域保存了首结点的地址;3、页数处理首元素的插入操作和删除操作;4、重新实现清空操作和遍历操作。