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

📄 yue.c

📁 一个数据结构的课程设计文件,C编写.内容为:约瑟夫环
💻 C
字号:
  /*       预处理       */
  #define   LEN   sizeof(struct   node)
  #include   "stdio.h"   
    
  /*    结点定义    */   
  struct   node{   
      int           data;   
      int           No;   
      struct     node   *   next;   
      };   
    
  /*     建立循环链表函数     */   
  struct   node   *CreatCycLkList()  /*函数返回指向循环链表的第一个结点的指针*/
  {   
      struct   node   *head,*p1,*p2;   
      int   n=0;   
    
      p1=p2=(struct   node   *)malloc(LEN);     
      printf("\ninput   the   %d   pwd:",n+1);   
      scanf("%d",&p1->data);   
      p1->No=n+1;   
      head=NULL;   
    
      while(p1->data!=0){   
          n++;   
          if(n==1)   
              head=p1;   
          else   
              p2->next=p1;   
          p2=p1;   
          p1=(struct   node   *)malloc(LEN);   
          printf("input   the   %d   pwd:",n+1);   
          scanf("%d",&p1->data);   
          p1->No=n+1;   
      }   
      p2->next=head;     
      return(head);   
  }   
    
  /*    主函数     */
  main()   
  {   
      int   pwd;   
      struct   node   *head,*prior,*p;
    
      printf("please   input   the   first   password:");   
      scanf("%d",&pwd);   
      printf("\n");   
      head=CreatCycLkList();   
      p=head;   
    
      while(p!=p->next){   
          while(pwd!=1){   
              prior=p;   
              p=p->next;   
              pwd--;   
  }   /*外while判断是否为最后一个结点,内while语句为了查找下一个要出队列的结点*/
    
          printf("%d   ",p->No);   
          pwd=p->data;
  prior->next=p->next;      /*从表中删除刚出列的结点*/
  p=prior->next;
      }   
      printf("%d",p->No);   /*输出最后一结点的原位置编号*/
      getch();
  } 

⌨️ 快捷键说明

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