选首领

/*选首领(此程序在WIN-TC下通过)*/
#include<stdio.h>
#include<stdlib.h>

typedef struct node
{ int code;
  struct node *next;
}node,*linklist;

linklist create_list1(int n) /*尾插法创建单循环链表*/
{ int i;
  linklist head,p,q;
  head=(node *)malloc(sizeof(node));
  head->code=n;
  head->next=head;
  q=head;
  for(i=n-1;i>=1;i--)
  { p=(node *)malloc(sizeof(node));
    p->code=i;
    p->next=head;
    q->next=p;q=p;
   }
   return head;
 }

 linklist create_list2(int n) /*前插法创建单循环链表*/
{ int i;
  linklist head,p;
  head=(node *)malloc(sizeof(node));
  head->code=1;
  head->next=head;

  for(i=n;i>1;i--)
  { p=(node *)malloc(sizeof(node));
    p->code=i;
    p->next=head->next;
    head->next=p;
   }
   return head;
 }

 void output(linklist head)  /*输出单循环链表*/
{ linklist p;
  p=head;
  printf("The list of the member's flag:/n");
  do
  { printf("%d/n",p->code);
    p=p->next;
  }while(p!=head);
  printf("/n");
}

void play(linklist head ,int n)  /*选首领的具体实现*/
{ linklist p=head,q;
  int c=1,k=n;
  while(k>1)
  { if(c==2)
      { q=p->next;p->next=q->next;
        printf("/nThe %d member has been went out./n",q->code);
        free(q);c=0;k--;
       }
    else
      { p=p->next;c++;}
   }
   printf("/nCongratulations!/nThe %d member is the winner!",p->code);
}

main()
{ int n;
  linklist head;
  printf("Please input  people's number:");
  scanf("%d",&n);
  head=create_list1(n);
  output(head);
  play(head,n);
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值