zoukankan      html  css  js  c++  java
  • 数据结构趣题——约瑟夫环

       1: #include <stdio.h>
       2: #include <stdlib.h>
       3:  
       4: /*链表结点定义*/
       5: typedef struct node {
       6:     int number;  /*编号*/
       7:     int psw;    /*个人密码*/
       8:     struct node *next;
       9: } LNode, *LinkList;
      10:  
      11:  
      12: void insertList(LinkList *list, LinkList q, int e1, int e2) {
      13:     LinkList p;
      14:     p = ( LinkList)malloc(sizeof(LNode));
      15:     p->number = e1;
      16:     p->psw = e2;
      17:  
      18:     if(!*list) {
      19:         *list = p;
      20:         p->next = NULL;
      21:     }
      22:     else {
      23:         p->next = q->next;
      24:         q->next = p;
      25:     }
      26: }
      27:  
      28:  
      29:  
      30: void CreatJoseph(LinkList *jsp , int n)
      31: {
      32:     LinkList  q = NULL , list = NULL;
      33:     int i ,  e2;
      34:     printf("Please input the password for people in the Joseph circle\n");
      35:  
      36:     for(i = 0; i < n; i++) {
      37:         scanf("%d", &e2);
      38:         insertList(&list, q, i + 1, e2); /*向q指向的结点后面插入新的结点*/
      39:  
      40:         if(i == 0) q = list;   /*第一次之生成头结点,q也指向头结点*/
      41:         else q = q->next;        /*q指向下一结点*/
      42:     }
      43:  
      44:     q->next = list; /*形成循环链表*/
      45:  
      46:     *jsp = list;  //返回
      47: }
      48:  
      49:  
      50: void exJoseph(LinkList *jsp, int m)
      51: {
      52:     LinkList p , q;
      53:     int i;
      54:     q = p = *jsp ;
      55:  
      56:     while(q->next != p) q = q->next; /*q指向p的前一个结点*/
      57:  
      58:     printf("The order of a column is\n") ;
      59:  
      60:     while(p->next != p) {
      61:         for(i = 0; i < m - 1; i++)
      62:         {   /*p指向要删除的结点,q指向p的前一个结点*/
      63:             q = p;
      64:             p = p->next;
      65:         }
      66:  
      67:         q->next = p->next;
      68:         printf("%d ", p->number);
      69:         m = p->psw;
      70:         free(p);
      71:         p = q->next;
      72:     }
      73:  
      74:     printf("\nThe last person in the circle is %d\n", p->number);   /*打印出最后留在队中的人的编号*/
      75: }
      76:  
      77: int main()
      78: {
      79:     LinkList jsp;
      80:     int n , m;
      81:     printf("Please input number of the people in the Joseph circle\n");
      82:     scanf("%d", &n) ;
      83:     CreatJoseph(&jsp, n);
      84:     printf("Please input the first maximum number\n");
      85:     scanf("%d", &m) ;
      86:     exJoseph(&jsp, m) ;
      87: }
  • 相关阅读:
    144环形链表
    83. 删除排序链表中的重复元素
    21合并两个有序链表
    PyCharm2020激活破解教程
    Python正课目录
    2条pip命令解决Python项目依赖的导出和导出
    pip离线安装模块
    Python正课149 —— luffy项目 User表的配置
    Python正课148 —— luffy项目 数据库配置
    解决:django中LookupError No installed app with label 'admin'
  • 原文地址:https://www.cnblogs.com/steven_oyj/p/1745971.html
Copyright © 2011-2022 走看看