2013-08-18 21:27:50

循环链表、数组解决约瑟夫环问题的比较

注意几点:

  1. 循环链表的建立不难,在删除循环链表中元素时,用pCur->next != pCur判断结束;
  2. 每一轮计数开始,将计数器归1,counter = 1; 
  3. 并将指针指向下一个元素,pCur = pCur->next;  //从下一个元素开始计数

代码(测试暂未发现错误,欢迎交流指正):

 #include <iostream>
#include <cassert>
using namespace std; typedef int DataType; typedef struct node
{
DataType data;
struct node *next;
}LNode,*PLNode; //头插法建立循环链表
PLNode CreatCircularLink(PLNode &pHead,size_t n)
{
pHead = NULL;
PLNode pTail = NULL;
PLNode pNew = NULL;
size_t counter = n; while (counter >= )
{
pNew = new LNode;
pNew->data = counter;
pNew->next = pHead;
pHead = pNew; if (NULL == pTail)
{
pTail = pNew;
}
--counter;
} pTail->next = pHead;
return pHead;
} //利用循环链表,显示出队序列
void DisplaySequenceOfDequeue(PLNode &pHead,size_t n,size_t m)
{
CreatCircularLink(pHead,n); assert( n > && m > && n > m ); PLNode pCur = pHead;
PLNode pNodeToDelete = NULL;
size_t counter = ; while (pCur->next != pCur)
{
counter = ; //新一轮计数,计数值归1
while (counter < m - )
{
pCur = pCur->next;
++counter;
}
pNodeToDelete = pCur->next;
pCur->next = pCur->next->next;
cout<<pNodeToDelete->data<<"\t";
delete pNodeToDelete; pCur = pCur->next; //从下一个元素开始计数
} cout<<pCur->data<<"\t";
delete pCur;
cout<<endl;
} //显示循环链表
void DisplayCircularLink(PLNode &pHead)
{
PLNode pCur = pHead;
PLNode pTail = pHead; while (pCur->next != pTail)
{
cout<<pCur->data<<"\t";
pCur = pCur->next;
}
cout<<pCur->data<<"\t";
cout<<endl;
} typedef struct JRnode
{
size_t id;
bool flag;
}JRNode,*PJRNode;
//
//void JosephRing(size_t n,size_t m,size_t *(&outQueue))
//{
// JRNode *inQueue = new JRNode[n];
// size_t counter = 0;
// size_t step = 0;
// int index = 0;
//
// for (index = 0;index < n;++index)
// {
// inQueue[index].id = index + 1;
// inQueue[index].flag = true;
// }
//
// index = 0;
// while (counter < n)
// {
// step = 0;
// while (step < m)
// {
// while (!inQueue[index].flag)
// {
// index = (index + 1) % n;
// }
// index = (index + 1) % n;
// ++step;
// }
//
// inQueue[index].flag = false;
// outQueue[counter++] = index;
//
// /*if (index == n)
// {
// index = n - 1;
// }*/
//
// }
//
// delete [] inQueue;
//} //用数组解决
void Circle_out(size_t n, size_t s, size_t m, size_t *p)
{
int i;
int *circle = new int[n];
int pos = s-;
int cnt = ;
int p_cnt = ;
int N = n;
for(i=;i<n;++i)
circle[i] = ;
while(n--)
{
cnt = m;
while(cnt)
{
if(circle[pos]==) //如果该位置未出圈,将该位置计入
cnt--;
pos++;
if(pos==N) //注意:不是if(pos==n),因为n在执行的过程中是变化的,是临时变量
pos = ;
}
//cout<<"pos = "<<pos<<endl;
if(pos==) //此处必须对pos为0的情况进行特殊处理
{
circle[N-] = ;
p[p_cnt++] = N;
}
else
{
circle[pos-] = ; //circle[pos-1],not circle[pos]
p[p_cnt++] = pos; //pos,not (pos-1)
}
}
delete [] circle;
} void DisplayArray(size_t *array,size_t len)
{
assert(NULL != array && len > );
size_t index = ; while (index < len)
{
cout<<array[index]<<"\t";
index++;
}
cout<<endl; } //测试约瑟夫环
void TestCircularLink()
{
PLNode pHead = NULL;
size_t n = ;
size_t m = ; cout<<"Please enter n and m : "<<endl;
while (cin>>n>>m)
{
//CreatCircularLink(pHead,n);
//DisplayCircularLink(pHead);
cout<<"(DisplaySequenceOfDequeue)The Sequence Of Dequeue is :"<<endl;
DisplaySequenceOfDequeue(pHead,n,m); size_t *outQueue = new size_t[n];
Circle_out(n, , m, outQueue); cout<<"(Circle_out)The Sequence Of Dequeue is :"<<endl;
DisplayArray(outQueue,n); delete [] outQueue; cout<<"Please enter n and m : "<<endl;
}
} int main()
{
TestCircularLink();
return ;
}

测试结果:

Please enter n and m :

(DisplaySequenceOfDequeue)The Sequence Of Dequeue is :

(Circle_out)The Sequence Of Dequeue is :

Please enter n and m :

(DisplaySequenceOfDequeue)The Sequence Of Dequeue is :

(Circle_out)The Sequence Of Dequeue is :

Please enter n and m :
^Z
请按任意键继续. . .

约瑟夫环问题-循环链表VS数组的更多相关文章

  1. 约瑟夫环的C语言数组实现

    约瑟夫环问题的具体描述是:设有编号为1,2,……,n的n个(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,才从他的下一个人起重新报数,报到m时停止报数,报m的出圈, ...

  2. javascript中使用循环链表实现约瑟夫环问题

    1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...

  3. C++循环链表解决约瑟夫环问题

    约瑟夫环问题可以简单的使用数组的方式实现,但是现在我使用循环链表的方法来实现,因为上午看到一道面试题规定使用循环链表解决约瑟夫环问题. 什么是约瑟夫环? “约瑟夫环是一个数学的应用问题:已知n个人(以 ...

  4. C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析

    尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...

  5. j使用数组实现约瑟夫环 java

    我们首先来看一下约瑟夫环问题: 给定m个人,m个人围成一圈,在给定一个数n,从m个人中的第一个人每第n个人便将其除去,求被最后一个出去的人的编号. 思路: 建立一个长度为m+1的数组,将其的内容初始化 ...

  6. 数据结构7: 循环链表(约瑟夫环)的建立及C语言实现

    链表的使用,还可以把链表的两头连接,形成了一个环状链表,称为循环链表. 和它名字的表意一样,只需要将表中最后一个结点的指针指向头结点,就形成了一个环. 图1 循环链表 循环链表和动态链表相比,唯一的不 ...

  7. 约瑟夫环问题-Java数组解决

    约瑟夫环问题说的是,n个人围成一圈,从第k个人开始沿着一个方向报数,报到第m个人时,第m个人出列,从紧挨着的下一个人(未出列)开始,求整个环中人的出列顺序.下面是我用java实现的解决方法. clas ...

  8. Java学习之约瑟夫环的两中处理方法

    package day_2; import java.util.Scanner; /** * @author Administrator * 约瑟夫环问题: 设编号为 1,2,3,....n的N个人围 ...

  9. 通过例子进阶学习C++(六)你真的能写出约瑟夫环么

    本文是通过例子学习C++的第六篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然后从出局的下一个人重新 ...

随机推荐

  1. jquery实现视觉滚动--fullpage

    用fullpage.js实现视觉滚动效果 1.Html页面 <!DOCTYPE html> <html> <head> <meta charset=" ...

  2. Geographic Coordinate Systems

    Coordinate Systems Geographic Coordinate Systems This is an archive of a previous version of the Arc ...

  3. 解决EnableVisualStyles Bug

    一位朋友碰到了一个WinForm的问题,在网上搜了一通,没找到能解决问题的方案, 正好我以前以碰到过,在这里把解决方案呈上,以便有遇到此问题的朋友能有帮助. 问题是这样的,当启用了虚拟样式后,设置好的 ...

  4. Java 多线程 锁 存款 取款

    http://jameswxx.iteye.com/blog/806968 最近想将java基础的一些东西都整理整理,写下来,这是对知识的总结,也是一种乐趣.已经拟好了提纲,大概分为这几个主题: ja ...

  5. php环境扩展包so

    http://files.cnblogs.com/files/adtuu/no-debug-non-zts-20121212.zip /Applications/XAMPP/xamppfiles/li ...

  6. [译]线程生命周期-理解Java中的线程状态

    线程生命周期-理解Java中的线程状态 在多线程编程环境下,理解线程生命周期和线程状态非常重要. 在上一篇教程中,我们已经学习了如何创建java线程:实现Runnable接口或者成为Thread的子类 ...

  7. linux 输入子系统(4)---- input子系统的初始化

    Input子系统的初始化函数为input_init(),如下: static int __init input_init(void) { int err; input_init_abs_bypass( ...

  8. Linux vi 中搜索关键字

    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 在vi里可没有菜单-〉查找 不过没关系,可以在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然 ...

  9. Homebrew下安装Cocoapods

    在Mavericks下安装Cocoapods遇到ruby安装问题,参照以下网址圆满解决问题. http://www.moncefbelyamani.com/how-to-install-xcode-h ...

  10. 十三、mysql 分区之 RANGE && LIST

    .RANGE 分区 创建实例: CREATE TABLE EMP ( id int not null primary key auto_increment, name ) not null defau ...