鱼C论坛

 找回密码
 立即注册
查看: 3066|回复: 0

[学习笔记] 约瑟夫问题

[复制链接]
发表于 2021-9-24 21:30:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define MAXSIZE 41

  5. typedef struct Like
  6. {
  7.         int T;
  8.         struct Like *next;
  9. }Like, *Likes;

  10. void explain();                        //本程序各个功能说明
  11. void InitList(Likes *N);//清空数据并初始化程序
  12. void GetList(Likes *N);        //随机创建41人员个编号
  13. void PutList(Like *N);        //打印所有链表
  14. void DeThList(Likes *N);        //依次删除第三人并打印编号
  15. void DeThTwList(Likes *N);        //依次删除第三人直至剩余最后两人并打印
  16. void InsertList(Likes *N);        //指定位置插入
  17. void DeApList(Likes *N);        //指定位置删除
  18. void DeApList(Likes *N)
  19. {
  20.         int K;
  21.         Like *temp = (*N)->next, *temps;

  22.         printf("删除数据:");
  23.         scanf("%d", &K);

  24.         while (temp->T != K)
  25.         {
  26.                 temps = temp;
  27.                 temp = temp->next;
  28.                 if (temp == (*N)->next)
  29.                 {
  30.                         printf("未找到匹配的数据请确认输入是否准确。\n");
  31.                         return;
  32.                 }
  33.         }
  34.         if ((*N)->next == temp)
  35.         {
  36.                 do
  37.                 {
  38.                         temps = temp;
  39.                         temp = temp->next;
  40.                 } while (temp != (*N)->next);
  41.                 (*N)->next = temp->next;
  42.         }
  43.         temps->next = temp->next;
  44.         free(temp);



  45.        

  46. }
  47. void InsertList(Likes *N)
  48. {
  49.         int K;
  50.         Like *temp = (*N)->next, *temps;


  51.         scanf("%d", &K);
  52.         getchar();
  53.         printf("在%d之后插入\n", K);

  54.         while (temp->T != K)
  55.         {
  56.                 temp = temp->next;
  57.                 if (temp == (*N)->next)
  58.                 {
  59.                         printf("未找到匹配的数据请确认输入是否准确。\n");
  60.                         return;
  61.                 }
  62.         }

  63.         temps = (Likes )malloc(sizeof(Like ));
  64.         printf("输入插入的值:");
  65.         scanf("%d", &temps->T);
  66.         getchar();
  67.         temps->next = temp->next;
  68.         temp->next = temps;
  69. }
  70. void DeThTwList(Likes *N)
  71. {
  72.         int i, j;
  73.         Like *temp = (*N)->next, *temps, *tempss;

  74.         for (i = 0; i < MAXSIZE - 2; i++)
  75.         {
  76.                 for (j = 0; j < 2; j++)
  77.                 {
  78.                         temps = temp;
  79.                         temp = temp->next;
  80.                 }
  81.                 if ((*N)->next == temp)
  82.                 {
  83.                         (*N)->next = temp->next;        //如果头文件数据域被删除,用temp->next更换头文件数据域中的数据
  84.                 }
  85.                 tempss = temp;
  86.                 temp = temp->next;       
  87.                 temps->next = temp;        //与前一个链表相连
  88.                 free(tempss);
  89.         }
  90.         PutList(*N);

  91. }
  92. void DeThList(Likes *N)
  93. {
  94.         int i, j;
  95.         Like *temp = (*N)->next, *temps, *tempss;

  96.         for (i = 0; i < MAXSIZE; i++)
  97.         {
  98.                 for (j = 0; j < 2; j++)
  99.                 {
  100.                         temps = temp;
  101.                         temp = temp->next;
  102.                 }

  103.                 if (temp == (*N)->next)
  104.                 {
  105.                         (*N)->next = temp->next;
  106.                 }
  107.                 printf("<%d>", temp->T);
  108.                 tempss = temp;
  109.                 temp = temp->next;
  110.                 temps->next = temp;
  111.                 free(tempss);
  112.         }
  113.         putchar('\n');

  114. }

  115. void PutList(Like *N)
  116. {
  117.         Like *temp = N->next;
  118.         while (1)
  119.         {
  120.                 printf("<%d>", temp->T);
  121.                 temp = temp->next;

  122.                 if (N->next == temp)
  123.                 {
  124.                         break;
  125.                 }
  126.         }
  127.         putchar('\n');
  128. }
  129. void GetList(Likes *N)
  130. {
  131.         int i;
  132.         Like *temp, *tail;
  133.         srand((unsigned)time(NULL));        //随机数按照时间归零

  134.         for (i = 0; i < MAXSIZE; i++)
  135.         {
  136.                 temp = (Like *)malloc(sizeof(Like ));
  137.                 temp->T = (rand() % 100);
  138.                
  139.                
  140.                 if ((*N)->next == *N)
  141.                 {
  142.                         (*N)->next = temp;
  143.                 }
  144.                 else
  145.                 {
  146.                         tail->next = temp;
  147.                 }
  148.                 tail = temp;
  149.                 tail->next = (*N)->next;
  150.         }
  151.        
  152. }

  153. void InitList(Likes *N)
  154. {
  155.         int i = 0;
  156.         Like *temp;

  157.         if (*N != NULL)
  158.         {
  159.                 while (i <= MAXSIZE)
  160.                 {
  161.                         temp = *N;
  162.                         *N = (*N)->next;
  163.                         free(temp);
  164.                         i++;
  165.                 }
  166.         }
  167.         *N = (Likes )malloc(sizeof(Like ));
  168.         (*N)->next = *N;       
  169. }
  170. void explain()
  171. {
  172.         printf("1、清空数据并初始化程序\n");
  173.         printf("2、随机创建41人员个编号\n");
  174.         printf("3、打印所有人员编号\n");
  175.         printf("4、依次删除第三人并打印编号\n");
  176.         printf("5、依次删除第三人直至剩余最后两人\n");
  177.         printf("6、指定位置插入\n");
  178.         printf("7、删除指定人员\n");
  179.         printf("0、结束本程序\n");
  180. }

  181. int main()
  182. {
  183.         int i;
  184.         Like *N = NULL;

  185.         explain();
  186.         while (1)
  187.         {
  188.                 printf("输入选项:");
  189.                 scanf("%d", &i);
  190.                 getchar();

  191.                 switch (i)
  192.                 {
  193.                 case 1:{
  194.                         InitList(&N);
  195.                            }
  196.                         break;
  197.                 case 2:{
  198.                         GetList(&N);
  199.                            }
  200.                         break;
  201.                 case 3:{
  202.                         PutList(N);
  203.                            }
  204.                         break;
  205.                 case 4:{
  206.                         DeThList(&N);
  207.                            }
  208.                         break;
  209.                 case 5:{
  210.                         DeThTwList(&N);
  211.                            }
  212.                         break;
  213.                 case 6:{
  214.                         InsertList(&N);
  215.                            }
  216.                         break;
  217.                 case 7:{
  218.                         DeApList(&N);
  219.                            }
  220.                         break;
  221.                 case 0:{
  222.                         return 0;
  223.                            }
  224.                         break;
  225.                 }
  226.         }
  227.        

  228.         return 0;
  229. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 00:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表