转载: 约瑟夫环 循环链表 必备「建议收藏」

转载: 约瑟夫环 循环链表 必备

大家好,又见面了,我是全栈君。

http://blog.csdn.net/gggg_ggg/article/details/42965853

  1. /********************************************************************  
  2. created:2015年1月20日 23:06:46     
  3. author: Jackery      
  4. purpose: Joseph problem  
  5. *********************************************************************/    
  6. #include”stdafx.h”  
  7. #include<iostream>  
  8. using namespace std;  
  9.   
  10. typedef struct _Node  
  11. {  
  12.     int data;  
  13.     struct _Node*next;  
  14. } node_t;  
  15.   
  16. typedef struct _Linklist  
  17. {  
  18.     node_t*phead;  
  19.     node_t*ptail;  
  20.     int len;  
  21. }Linklist;  
  22. static node_t*GetNode(int i )//新建并初始化节点  
  23. {  
  24.     node_t*pNode;  
  25.     pNode=new node_t;  
  26.     if(!pNode)  
  27.     {  
  28.         cout <<“内存分配失败” <<endl;  
  29.         exit(-1);  
  30.     }  
  31.     pNode->data=i;  
  32.     pNode->next=NULL;  
  33.     return pNode;  
  34.     delete pNode;  
  35. }  
  36. void init_list(Linklist*plist)//用第一个节点初始化循环单链表  
  37. {  
  38.     node_t*p;  
  39.     p=GetNode(1);  
  40.     //printf(“TheNewNodeis:%d\n”,p->data);//****TEST****  
  41.     plist->phead=p;  
  42.     plist->ptail=p;  
  43.     p->next=plist->phead;  
  44.     plist->len=1;  
  45. }  
  46.   
  47. //把其余数据添加到循环单链表中  
  48. static void Create_List(Linklist*plist,int n)  
  49. {  
  50.     int i=0;  
  51.     node_t*pNew;  
  52.     for(i=2;i<=n;i++)  
  53.     {  
  54.         pNew=GetNode(i);  
  55.         /********TEST********  
  56.         cout <<“The New Node is:” <<pNew->data << endl;  
  57.         ********TEST********/  
  58.         plist->ptail->next=pNew;  
  59.         plist->ptail=pNew;  
  60.         pNew->next=plist->phead;  
  61.         plist->len++;  
  62.     }  
  63. }  
  64. //输出链表内容  
  65. // void Print_List(Linklist*plist)  
  66. // {  
  67. //  node_t*pCur=plist->phead;  
  68. //  do  
  69. //  {  
  70. //      cout << “The “<<  pCur->data <<“person.”  <<endl;  
  71. //      pCur=pCur->next;  
  72. //  }while(pCur!=plist->phead);  
  73. //  cout << “The length of the List “<< plist->len<< endl;;  
  74. // }  
  75.   
  76. //Joseph function implement  
  77. void joseph(Linklist* plist,int m,int k)  
  78. {    
  79.     node_t *pPre=plist->ptail;  
  80.     node_t *pCur=plist->phead;  
  81.     int i,j;  
  82. cout << “出队列的顺序依次为: “<< endl;  
  83.     while(plist->len != 1)  
  84.     {  
  85.         i=0;  
  86.         j=0;  
  87.         while(j<k-1)  
  88.         {  
  89.             pPre=pPre->next;  
  90.             j++;  
  91.         }  
  92.         while(i< m -1)  
  93.         {  
  94.             pPre=pPre->next;  
  95.             i++;  
  96.         }  
  97.         pCur=pPre->next;  
  98.         int temp=pCur->data;  
  99.         cout <<“第 ” << temp << ”  个人 “<< endl ;  
  100.         pPre->next=pCur->next;  
  101.         free(pCur);  
  102.         plist->len–;  
  103.     }  
  104.     cout <<“第 ” << pPre->data << ” 个人” << endl; ;  
  105.     cout << “The last one is:” << pPre->data<< endl;  
  106. }  
  107. int main(int argc, char * argv[])  
  108. {  
  109.     int n=0;  
  110.     cout <<“约瑟夫环长度为 : “<<endl;;  
  111.     cin >> n;  
  112.     int m=0;  
  113.     cout << “每此数到m个时,此人出列”<<endl;  
  114.     int k;  
  115.     cin >> k;  
  116.     cout << “从第k 个开始数” << endl;  
  117.     cin >>m;  
  118.     Linklist pList;  
  119.     init_list(&pList);  
  120.     Create_List(&pList,n);  
  121. //  Print_List(&pList);  
  122.     joseph(&pList,m,k);  
  123.     return 0;  
  124. }  

转载于:https://www.cnblogs.com/eat-too-much/p/6550458.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108621.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 浏览器扩展程序安装指南在哪_360浏览器扩展在哪里

    浏览器扩展程序安装指南在哪_360浏览器扩展在哪里小助手浏览器扩展程序安装指南

    2022年10月7日
    3
  • python里[::-1]_python中的数组类型

    python里[::-1]_python中的数组类型目录1.python数组下标2.b=a[i:j]3.b=a[i:j:k]1.python数组下标python下标有两套,一套是正的,一套是负的,a=’python’的下表如下python正下标012345负下标-6-5-4-3-2-1使用正下标时,下标i取值范围为0<=i<len(a)超出范围为越界使用负下标时,下标i取值范围为-1>=i>-len(a)-1超出范围为越界2.b=a[i:

    2022年8月13日
    4
  • SqlCommand.ExecuteReader 方法

    SqlCommand.ExecuteReader 方法SqlCommand.ExecuteReader方法将CommandText发送到Connection并生成一个SqlDataReader。重载列表名称说明SqlCommand.ExecuteReader()…

    2022年6月20日
    28
  • 激活成功教程芝诺悖论之阿基里斯追乌龟

    激活成功教程芝诺悖论之阿基里斯追乌龟版权所有。所有权利保留。欢迎转载,转载时请注明出处:阿基里斯是古希腊神话中善跑的英雄。在他和乌龟的竞赛中,他速度v1,位置坐标0;乌龟速度v2,位置坐标s。已知v1>v2。他在后面追,但他不可能追上乌龟。因为在竞赛中,追者首先必须到达被追者的出发点,当阿基里斯追到s时,乌龟已经又向前爬了s/v1*v2,位置s+s/v1*v2。于是,一个新的起点产生了;阿基里斯必须继续追,而当他追到乌龟爬的

    2022年6月23日
    33
  • acwing1106. 山峰和山谷(宽搜bfs)「建议收藏」

    acwing1106. 山峰和山谷(宽搜bfs)「建议收藏」FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。为了能够对旅程有一个安排,他想知道山峰和山谷的数量。给定一个地图,为FGD想要旅行的区域,地图被分为 n×n 的网格,每个格子 (i,j) 的高度 w(i,j) 是给定的。若两个格子有公共顶点,那么它们就是相邻的格子,如与 (i,j) 相邻的格子有(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)。我们定义一个格子的集合 S 为山峰(山谷)当且仅当:

    2022年8月9日
    6
  • JAVA的一般输入输出 和 快速输入输出 (BufferedReader&BufferedWrite)

    JAVA的一般输入输出 和 快速输入输出 (BufferedReader&BufferedWrite)JAVA基础知识和常用算法合集:https://blog.csdn.net/GD_ONE/article/details/104061907目录1.主类的命名必须是Main2.输入输出:2.1输入:(1)使用Scanner类进行输入(2)hasNext()方法2.2输出3快速输入输出3.1使用StreamTokenizer和PrintW…

    2022年5月26日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号