yuesefu.cpp

来自「关于数据结构的各章节的c原代码实现」· C++ 代码 · 共 84 行

CPP
84
字号
// yuesefu.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
struct lnode{
	int num;
	int cliper;
	struct lnode *next;
};
typedef struct lnode node;
typedef node* llnode;
llnode createlist(int n)
{
	llnode head,r,s;
	int clip;
	head=(llnode)malloc(sizeof(node));
	printf("输入第1个人的密码\n");
	scanf("%d",&clip);
	head->num=1;
    head->cliper=clip;
	head->next=NULL;
	r=head;
	for (int i=2;i<=n;i++)
	{
		printf("输入第%d个人的密码\n",i);
	    scanf("%d",&clip);
		s=(llnode)malloc(sizeof(node));
		s->num=i;
		s->cliper=clip;
		s->next=NULL;
		r->next=s;
		r=s;

	}
	r->next=head;
	return head;

}
llnode ysf(llnode h,int m)
{
llnode head,p,q;
int k=1;
head=h;
q=head;
p=q->next;
while (q!=p)
{
	k++;
	if (k==m)
	{   
		printf("出列的是%d:\n",p->num);
		m=p->cliper;	
		q->next=p->next;
		p=q->next;
		k=0;
	}
	else
	{
	q=p;
	p=p->next;
	}
}

return q;
}
int main(int argc, char* argv[])
{    
	llnode h,temp;
	int m,n;
	printf("输入m值:");
	scanf("%d",&m);
    printf("输入n值:");
	scanf("%d",&n);
	h=createlist(n);
    temp=ysf(h,m);
	printf("最后出列的是%d:\n",temp->num);
	
		
	return 0;
}

⌨️ 快捷键说明

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