链表实现报数游戏 您所在的位置:网站首页 报7游戏的数字表 链表实现报数游戏

链表实现报数游戏

2024-07-10 19:37| 来源: 网络整理| 查看: 265

编写一个能用链表实现报数游戏的小程序。 幼儿园M个小朋友围成一圈进行报数游戏,从1到N依次报数数,报到N的小朋友退出,然后重新从1报数,用程序模拟该过程,依次输出退出小朋友的编号。M和N的取值由用户输入。 效果如下: 输入:20 4 输出:4 8 12 16 20 5 10 15 1 7 14 2 11 19 13 6 3 9 18 17

#include using namespace std; struct node { int num; node * next; }; int main() { int i,length; int M,N; cin>>M; node * tmp=NULL; node * head=NULL; node * tail=NULL; tmp=new node; tmp->next=NULL; head=tmp; tail=tmp; head->num=1; for (i=2;i if (i==N) { length--; i=1; tail->next=tmp->next; coutnext; } return 0; }

#include #include typedef struct node { int number; struct node * next; }person; person * initLink(int n) { person * head=(person*)malloc(sizeof(person)); head->number=1; head->next=NULL; person * cyclic=head; for (int i=2; i person * tail=head; //找到链表第一个结点的上一个结点,为删除操作做准备 while (tail->next!=head) { tail=tail->next; } person * p=head; //找到编号为k的人 while (p->number!=k) { tail=p; p=p->next; } //从编号为k的人开始,只有符合p->next==p时,说明链表中除了p结点,所有编号都出列了, while (p->next!=p) { //找到从p报数1开始,报m的人,并且还要知道数m-1de人的位置tail,方便做删除操作。 for (int i=1; i int n; scanf("%d",&n); person * head=initLink(n); int k=1; int m; scanf("%d",&m); findAndKillK(head, k, m); return 0; }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有