ysfh.c

来自「本文件是数据结构试验中的约瑟夫环的源文件」· C语言 代码 · 共 88 行

C
88
字号
#include <stdio.h>

typedef struct LNode  /*定义链表的存储结构*/
                                  
{
 int num,pwd;
 struct LNode *next;
};
struct LNode *head,*p,*pt;
/*head是头结点指针,p是当前指针的前一指针, pt是当前指针*/


int creatLinkList(int n)
{
   int i;
   head=(struct LNode*)malloc(sizeof(struct LNode)); /*带头结点的链表*/
   if(!head)  {return 0;}
   p=head;
   for(i=1;i<n;i++)
  {
     pt=(struct LNode*)malloc(sizeof(struct LNode)); /*生成新结点*/
     if(!pt)  {return 0;}
     p->next=pt;
     p=pt;
  }
   p->next=head;   /*重新指向头结点,构成循环链表*/
   pt=head;
}


int enterPwd(int n)
{
   int i,j;
   printf("\nplease enter the password:(example:12 13 14)\n");

    for( i=1;i<=n;i++)
    {
       scanf("%d",&j);
       pt->num=i;
       pt->pwd=j;
       pt=pt->next;
    }
     pt=p;
}


int outList(int m,int n)
{
   int i,a;
   for(i=1;i<=n;i++)
   {
      for(a=1;a<m;a++)    /*遍历整个链表*/
      {
        pt=pt->next;
      }
      p=pt->next;
      m=p->pwd;
      printf("%d ",p->num);
      pt->next=p->next;  /*删除链表*/

      free(p);    /*释放动态分配的结点指针*/
   }
}


void proFace()
{
    printf("****************************************");
    printf("****************************************");

}
void main()
{
    int m,n;
    proFace();
    printf("\nplease enter m and n:(example:12 13 14)\n");
    scanf("%d %d",&m,&n);   /*n是人数,m报数上限值*/
    creatLinkList( n);

    enterPwd( n);

    printf("\nthe output quene is:\n");
    outList( m,n);

}


⌨️ 快捷键说明

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