有那位学数据结构的朋友帮我解决一下这个问题?

来源:百度知道 编辑:UC知道 时间:2024/06/01 18:43:58
设计一个程序求出约瑟夫环的出列顺序.约瑟夫环问题的一种描述是:编号为1,2,3.......n的N个人按顺时针方向围做一圈,每人持有一个密码(正整数).一开始任选一个正整数作为报数上限值M,从第一个人开始按顺时针方向从1开始顺序报数,报到M时停止报数,报M的人出列,将他的密码作为新的M值,从他在顺时针方向上的下一个人开始重新从1报,如此下去,直到所有的人出列为止.例如N=7,7个人的密码依次为3,1,7,2,4,8,4,M的初始值取6,则正确的出列顺序为6,1,4,7,2,3,5.要求使用单向循环链表模拟此出列过程.

#include <iostream.h>
#include <iomanip.h>

#include "josephus.h"

template <class Type> CircListNode<Type>::CircListNode( Type d , CircListNode<Type> *next )
{
data = d;
link = next;
}

template <class Type> CircList<Type>::CircList( Type value )
{
first = current =last = new CircListNode<Type> (value);
first->link = first;
}

template <class Type> CircList<Type>::~CircList(void)
{
cout << "The CircList has been successfully destructed" << endl;
}

template <class Type> int CircList<Type>::Length(void) const
{
int count = 1;
CircListNode<Type> *find = first->link;
while ( find != first )
{
find = find->link;
count++;
}
return count;
}

template <class Type> Boolean CircList<