【无标题】

【无标题】,第1张

【无标题】
typedef struct LNode
{
	elemtype data;
	struct LNode* next;
}linkList;
int joseph(linkList* head, elemtype n, elemtype c, elemtype m)//妈的faker,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
{
	linkList* p = head;
	
	if (c > n || !m)//c 从第几人开始,如果比总人数大 或者m为零退出
	{
		return 0;
	}
	int count = 0;
	for (count = 1; count < c; ++count)
	{
		p = p->next;
	}
	linkList* q = p->next;
	// q p 一前一后, q 在前, p 在后
	int i = 0;
	int j = 0;
	for(i = 1;i < n ;++i)//控制删除
	{
		for(j = 1; j < m; ++j)
		{

			if ((p->next != NULL) && (q->next != NULL))//p q 都不为尾巴
			{
				p = p->next;
				q = q->next;
			}
			 else if (q->next == NULL)//q 是表尾
			{
				p = p->next;
				q = head->next;
				
			}
			 else if(p->next == NULL)//p 是表尾
			{
				p = head->next;
				q = q->next;
			}
		}
		linkList* r;
		if (q->next == NULL) //删除节点 如果q 为表尾
		{
			r = q;
			q = head->next;
			p->next = NULL;
			printf("%d  ", p->data);
			free(r);
			r = NULL;
		}
		else//q 不为表尾
		{
			r = q;//临时指针
			q = q->next;
			if (p->next != NULL)//如果p 为表尾
				p->next = q;
			else//p 不为表尾
				head->next = q;
				printf("%d  ", r->data);
			free(r);
			r = NULL;
		}
	}
	if(head->next != NULL)
	printf("           %d", (head->next)->data);
}
int main()
{
	int count = 0;//开辟空间
	int n = 0;//人数
	int m = 0;//到第几人出局
	int c = 0;//从第几人开始
	linkList* head = (linkList*)malloc(sizeof(struct LNode));
	if (head != NULL)
	{
		head->next = NULL;
	}
	linkList* s;
	int x = 39;
	printf("请输入人数,从第几人开始,循环到第几任出局:");
	scanf_s("%d%d%d", &n, &c, &m);
	linkList* p1 = head;
	for (count = 1; count <= n; ++count)
	{
		s = (linkList*)malloc(sizeof(struct LNode));
		if (s != NULL)
		{
			s->data =  count;
			p1->next = s;
			p1 = s;
		}
	}
	p1->next = NULL;
	
	joseph(head, n, c, m);

	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5659737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存