⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 josephus.c

📁 用循环链表解Josephus问题。设有n个人围坐在一个圆桌周围
💻 C
字号:
#include<stdio.h>   
  typedef struct node{
  int n;   
  struct node *m;
  } NODE;  
  NODE *head;/*定义一个指向该结构体的头指针*/
  
  NODE *create(int x)/*创建具有x个结点的单链表*/  
  {   
  NODE *p;   
  int i=1;   
  p=head=(NODE *)malloc(sizeof(NODE));   
  head->m=head; /*造循环链表时头指针的指针域设置*/  
  while(i<=x)   
  {   
  p->n=x+1-i;   
  p->m=head->m;   
  head->m=p;   
  i++;   
  p=(NODE *)malloc(sizeof(NODE));   
  }   
  return(head);   
  } 
  
  void output(int a,int b)   
  {   
  int i;   
  NODE *p,*q;   
  p=head; /*将指针移到起始结点,即第a个结点*/    
  i=0;   
  while(i<a)   
  {   
  p=p->m;   
  i++;   
  }    /*删除满足报数值的结点*/
  while(p->m!=p)   
  {   
  i=1;   
  while(i<b)/*找到符合报数值结点的前一个结点,即第b-1个结点*/  
  {   
  p=p->m;   
  i++;   
  }    /*先输出,后删除*/
  q=p->m;   
  printf("%d",q->n);   
  p->m=q->m;   
  free(q);   
  }   
  printf("%d",p->n);/*输出仅剩的结点*/ 
  }   
  
  main()   
  { 
  int location,callnum;    
  printf("\ninput location=");   
  scanf("%d",&location);   
  printf("\ninput callnum=");   
  scanf("%d",&callnum); 
  head=create(location);   
  output(location,callnum);       
  }   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -