************************* circleLinkList.h *************************
#include <iostream>
template <typename T>
class CircleLinkNode
{
public:
CircleLinkNode( const T& val)
{
value = val;
next = NULL;
}
T value;
CircleLinkNode *next;
}
template <typename T>
class CircleLinkList
{
public:
//构造函数
CircleLinkList()
{
head = NULL;
}
//判断是否为空
bool isEmpty()
{
return head == NULL;
}
//析构链表
~CircleLinkList()
{
while(!isEmpty())
{
deleteHead();
}
}
//插入节点(到尾部)
void insertNode( const T& val );
//删除节点(从头部)
void deleteHead();
//删除节点(按数值)
void deleteNode( const T& val );
}
************************* circleLinkList.cpp *************************
#include <iostream>
#include "circleLinkList.h"
//插入节点(到尾部)
template <typename T>
void CircleLinkList<T>::insertNode( const T& val )
{
CircleLinkNode *pNew = new CircleLinkNode(val);
if(isEmpty())
{
head = pNew;
head->next = head;
}
else
{
CircleLinkNode *pCur = head; //寻找尾节点
while( pCur->next != head )
pCur = pCur->next;
pCur->next = pNew;
pNew->next = head;
}
}
//删除节点(从头部)
template <typename T>
void CircleLinkList<T>::deleteHead()
{
if(isEmpty())
{
throw("Empty");
}
else
{
CircleLinkNode *tmp = head;
if( head->next == head ) //如果只有一个节点,自连接需特别处理
{
head = NULL;
}
else
{
CircleLinkNode *pEnd = head; //找到尾节点
while( pEnd->next != head )
pEnd = pEnd->next;
pEnd->next = head->next; //尾节点的指针跳过原头节点
head = head->next; //头节点后移
}
delete tmp;
}
}
//删除节点(按数值)
template <typename T>
void CircleLinkList<T>::( const T& val )
{
CircleLinkNode *tmp = NULL; //指向要删除的节点
if(isEmpty())
{
throw("Empty");
}
else if( head == head->next ) //当只有一个头节点时单独处理
{
if( head->value == val )
{
tmp = head;
head = NULL;
}
}
else
{
CircleLinkNode *pCur, *pPre;
pPre = NULL;
pCur = head;
while( pCur->next != head && pCur->value != val )
{
pPre = pCur;
pCur = pCur->next;
}
if( pCur->value == val ) //退出循环时,pCur要么指向尾节点,要么值为val
{
if( pCur == head )
{
tmp = head; //deleteHead
CircleLinkNode *pEnd = head;
while( pEnd->next != head )
pEnd = pEnd->next;
pEnd->next = head->next;
head = head->next;
}
else
{
pPre->next = pCur->next;
tmp = pCur;
}
}
}
if(tmp) delete tmp; //如果不为空则删除
}
约瑟夫环
************************* JosephusCircle.cpp *************************
#include <iostream>
typedef struct CircleNode
{
int no;
CircleNode *next;
}node;
class JosephusCircle
{
public:
JosephusCircle(int n);
void deleteNode(int val);
int function(int k, int m);
private:
node *head;
node *current;
int count;
};
JosephusCircle::JosephusCircle(int n)
{
head = NULL;
node *pNew, *pCur;
for(int i=1; i<=n; i++)
{
pNew = new node();
pNew->no = i;
if( head == NULL )
{
head = pNew;
pNew->next = head;
}
else
{
pCur = head;
while( pCur->next != head )
pCur = pCur->next;
pCur->next = pNew;
pNew->next = head;
}
}
count = n;
current = head;
}
void JosephusCircle::deleteNode(int val)
{
if(head == NULL)
{
throw("Empty");
}
else
{
node *tmp = NULL;
if(head->next == head && head->no == val)
{
tmp = head;
head = NULL;
}
else
{
node *pCur = head;
node *pPre = NULL;
while( pCur->next != head && pCur->no != val )
{
pPre = pCur;
pCur = pCur->next;
}
if(pCur->no == val)
{
if(pCur==head)
{
tmp = pCur;
while(pCur->next != head)
pCur = pCur->next;
pCur->next = head->next;
head = head->next;
}
else
{
tmp = pCur;
pPre->next = pCur->next;
}
}
}
if(tmp)
{
delete tmp;
count--;
}
}
}
void JosephusCircle::function(int n, int k, int m)
{
if( count == n )
{
for(int i=1; i<k; i++)
{
current = current->next;
}
}
for(int j=1; j<m; j++)
{
current = current->next;
}
node *tmp = current;
current = current->next;
printf("NO. %d\t Kicked!\n", tmp->no);
deleteNode(tmp->no);
if(count>0)
function(n,k,m);
else
printf("All Kicked!\n");
}
int main()
{
JosephusCircle jc(10);
jc.function(10,3,6);
return 0;
}