约瑟夫环问题-循环链表VS数组
2013-08-18 21:27:50
循环链表、数组解决约瑟夫环问题的比较
注意几点:
- 循环链表的建立不难,在删除循环链表中元素时,用pCur->next != pCur判断结束;
- 每一轮计数开始,将计数器归1,counter = 1;
- 并将指针指向下一个元素,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数组的更多相关文章
- 约瑟夫环的C语言数组实现
约瑟夫环问题的具体描述是:设有编号为1,2,……,n的n个(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,才从他的下一个人起重新报数,报到m时停止报数,报m的出圈, ...
- javascript中使用循环链表实现约瑟夫环问题
1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...
- C++循环链表解决约瑟夫环问题
约瑟夫环问题可以简单的使用数组的方式实现,但是现在我使用循环链表的方法来实现,因为上午看到一道面试题规定使用循环链表解决约瑟夫环问题. 什么是约瑟夫环? “约瑟夫环是一个数学的应用问题:已知n个人(以 ...
- C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析
尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...
- j使用数组实现约瑟夫环 java
我们首先来看一下约瑟夫环问题: 给定m个人,m个人围成一圈,在给定一个数n,从m个人中的第一个人每第n个人便将其除去,求被最后一个出去的人的编号. 思路: 建立一个长度为m+1的数组,将其的内容初始化 ...
- 数据结构7: 循环链表(约瑟夫环)的建立及C语言实现
链表的使用,还可以把链表的两头连接,形成了一个环状链表,称为循环链表. 和它名字的表意一样,只需要将表中最后一个结点的指针指向头结点,就形成了一个环. 图1 循环链表 循环链表和动态链表相比,唯一的不 ...
- 约瑟夫环问题-Java数组解决
约瑟夫环问题说的是,n个人围成一圈,从第k个人开始沿着一个方向报数,报到第m个人时,第m个人出列,从紧挨着的下一个人(未出列)开始,求整个环中人的出列顺序.下面是我用java实现的解决方法. clas ...
- Java学习之约瑟夫环的两中处理方法
package day_2; import java.util.Scanner; /** * @author Administrator * 约瑟夫环问题: 设编号为 1,2,3,....n的N个人围 ...
- 通过例子进阶学习C++(六)你真的能写出约瑟夫环么
本文是通过例子学习C++的第六篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然后从出局的下一个人重新 ...
随机推荐
- 仿春雨医生 安卓app(android)
仿春雨医生 安卓app(android) 目前APP处与开发完善中,可过程序自下载更新,如有BUG报错,请联系QQ 131 065 1206 支持安卓(android) .IOS(IPHONE),PA ...
- 【原】web页面登陆验证
using Itcast.Mall.Model; using System; using System.Collections.Generic; using System.Linq; using Sy ...
- 【原】在一般处理程序中设置session
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...
- 解决IE6中ajax ‘aborted’错误请求中断
给a标签绑定了一个click事件用来触发ajax请求,在IE6中,请求时常会被中断,在其他浏览器中都一切正常. 在IE6中使用Fiddler2和httpWatch监视请求,经常会出现”aborted” ...
- 转 Linux命令及Linux终端的20个趣事
https://linux.cn/article-2831-1.html 1. 命令:sl (蒸汽机车) 你可能了解 ‘ls’ 命令,并经常使用它来查看文件夹的内容.但是,有些时候你可能会拼写成 ‘s ...
- centos 安装ecshop出现date错误
centos 安装ecshop 错误提示 Warning: date(): It is not safe to rely on the system's timezone settings. You ...
- RTC搭建android下三层应用程序访问服务器MsSql-服务器端
前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库里的中文有乱码一直未得到解决!rtc6.23版本,已经支持xe5,也支持fmx的android下开发, ...
- (转)Qt Model/View 学习笔记 (四)——创建新的Models
创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...
- Windows.Andy.Code4App.dll Win8.1/WP8.1通用类库@ver1.0.1
在上篇 Windows.Andy.Code4App.dll Win8.1/WP8.1通用类库@ver1.0.0 已经对Win8.1和WP8.1部分扩展通用类库做了说明,这篇继续对通用类库做扩展.写的 ...
- validate[.unobtrusive]和Bootstrap实现tooltip错误提示
validate[.unobtrusive]和Bootstrap实现tooltip错误提示 类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效 ...