C++环形链表实现约瑟夫(Josephu)问题

news/2024/9/29 1:18:22/

C++环形链表实现约瑟夫(Josephu)问题

约瑟夫问题如下:设有n个人围坐圆桌周围。从某个位置上的人开始从1报数,数到m的人便出列,下一个人(第m+1个)又从1报数开始,数到m的人便是第2个出列的人,依次类推,直到最后一个人出列为止,这样就可以得到一个人员排列的新次序。例如,n=8,m=4,从第1个人数起,得到的新次序为48521376。

存储结构:带头结点的双向环形链表

//Josephu.h
#pragma once
typedef int nodetype;
typedef struct circlenode
{nodetype data;struct circlenode* front;struct circlenode* next;
}CLListNode;CLListNode*  CirclenodeInit();void CilcleListPush(CLListNode*& node,nodetype new_data);CLListNode* FindTail(CLListNode* L);void CLListPrint(CLListNode* L);bool CLListIsNull(CLListNode* L);CLListNode* JosephuCLList(CLListNode*& L,int m, int n);

简单的环形链表创建过程。front和next指针,便于链表的插入和删除。
JosephuCLList函数实现约瑟夫环问题。

//Josephu.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include"Josephu.h"//结点初始化
CLListNode* CirclenodeInit()
{CLListNode* node = (CLListNode*)malloc(sizeof(CLListNode));node->front = NULL;node->next = NULL;return node;}void CilcleListPush(CLListNode*& node,nodetype new_data)
{//开始只有一个结点if (node->next==NULL){CLListNode* new_node= CirclenodeInit();new_node->data = new_data;node->next = new_node;new_node->front = node;node->front = new_node;new_node->next = node;}//大于等于2个结点else{CLListNode* tail = FindTail(node);CLListNode* new_node= CirclenodeInit();new_node->data = new_data;//尾结点尾指针指向新节点,新结点指针指向尾节点tail->next = new_node;new_node->front = tail;//新结点尾指针指向头节点,头结点指针指向新节点node->front = new_node;new_node->next = node;}}CLListNode* FindTail(CLListNode* L)
{CLListNode* tail = L->next;int i = 0;//找尾结点,个数大于1的情况while (tail->next != L&&tail->next != NULL){tail = tail->next;i++;}return tail;}bool CLListIsNull(CLListNode* L)
{//只有头结点和有头结点和第一个元素if ((L->front == L->next)){if (L->next != NULL)return false;elsereturn true;}//其余情况elsereturn false;
}void CLListPrint(CLListNode* L)
{if (CLListIsNull(L)){cout << "the list is empty" << endl;}else{CLListNode* p = L->next;int i = 0;while (p != L){cout << p->data<<"\t";p = p->next;}cout << endl;}
}
CLListNode* JosephuCLList(CLListNode*& L,int m,int n)
{CLListNode* Josephu = L->next;//out指针出列CLListNode* out = L;while (!CLListIsNull(L)){//报m次数,第m个出列//因为每次报完后指针都后移一位,所以只用循环m-1次for (int i = 0; i < m-1; i++){//如果下一个是头结点就跳过头结点Josephu = Josephu->next;if (Josephu == L)Josephu = L->next;}out = Josephu;Josephu = Josephu->next;//跳过头结点if (Josephu == L)Josephu = L->next;cout << out->data << "\t";//1到m-1次去除结点if (out->front != out->next){out->front->next = out->next;out->next->front = out->front;}//只剩最后一个元素清除if(out->front == out->next){break;	}free(out);}Josephu = L->front;return Josephu;
}
//main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include"Josephu.h"
void text()
{CLListNode* L = CirclenodeInit();nodetype i = 1;int n = 8, m = 4;for (i = 1; i <= n; i++){CilcleListPush(L,i);}CLListPrint(L);CLListNode* Josephu=JosephuCLList(L, m, n);cout << endl;cout << "Josephu:"<<Josephu->data << endl;free(Josephu);free(L);}int main()
{text();return 0;
}

http://www.ppmy.cn/news/1531243.html

相关文章

vue-i18n在使用$t时提示类型错误

1. 问题描述 Vue3项目中&#xff0c;使用vue-i18n&#xff0c;在模版中使用$t时&#xff0c;页面可以正常渲染&#xff0c;但是类型报错。 相关依赖版本如下&#xff1a; "dependencies": {"vue": "^3.4.29","vue-i18n": "^9.1…

6.C_数据结构_查询_哈希表

概述 哈希表的查询是通过计算的方式获取数据的地址&#xff0c;而不是依次比较。在哈希表中&#xff0c;有一个键值key&#xff0c;通过一些函数转换为哈希表的索引值。 其中&#xff1a;这个函数被称为哈希函数、散列函数、杂凑函数&#xff0c;记为&#xff1a;H(key) 哈希…

重建大师区块划分的原则是什么?

根据计算机内存来进行划分&#xff0c;一般预估内存不超过计算机内存的2/3。 重建大师&#xff0c;这是一款专为超大规模实景三维数据生产设计的集群并行处理软件&#xff0c;支持卫星影像、航空影像、倾斜影像和激光点云多源数据输入建模&#xff0c;可完成超大规模数据的空三…

Ubuntu 安装配置nginx

参考文章&#xff1a; Ubuntu20.04安装配置Nginx_ubuntu20.04安装nginx-CSDN博客

【操作系统强化】王道强化一轮笔记

第一章 计算机系统概述 考点1 操作系统的概念、特征和功能 1. 2. 考点2 内核态与用户态 1. 2.用户态和内核态之间的切换本质上就是应用程序和操作系统对CPU控制器的切换 考点3 中断和异常 1. 2. 考点4 系统调用 1. 2. 3.C 考点5 操作系统引导 1. 2. ①磁盘的物理格式化&…

小说阅读器小程序+ssm论文源码调试讲解

2 系统开发环境 为了能够使本系统较好、较为完善的被设计实现出来&#xff0c;在功能上&#xff0c;我对新系统进行了细致的分析。通过详细的分析&#xff0c;我选择了SSM框架来进行开发设计&#xff0c;在数据存储上&#xff0c;采用 Mysql数据库来进行设计。本系统选择的开发…

OpenCV视频I/O(5)视频采集类VideoCapture之从视频流中获取下一帧的函数grab()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 从视频文件或捕获设备中抓取下一帧。 grab() 函数是 OpenCV 中 VideoCapture 类的一个成员函数&#xff0c;用于从视频流中获取下一帧而不立即检…

DoppelGanger++:面向数据库重放的快速依赖关系图生成

doi&#xff1a;DoppelGanger: Towards Fast Dependency Graph Generation for Database Replay&#xff0c;点击前往 文章目录 1 简介2 架构概述3 依赖关系图3.1 符号和问题定义3.2 无 IT(k) 图3.3 无 OT 图表3.4 无 OTIT 图表3.5 无 IT[OT] 图表3.6 输出确定性保证 4 重复向后…