用链表实现约瑟夫环

约瑟夫环问题,这是一个很经典算法,处理的关键是:伪链表

问题描述:N个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续从1开始报数,报到m的人出圈;如此往复,直到所有人出圈。(模拟此过程,输出出圈的人的序号)

在数据结构与算法书上,这个是用链表解决的。 https://blog.csdn.net/weixin_38214171/article/details/80352921

 

 

 

 

#include <stdio.h>
#include <stdlib.h>
//#define N 10
//#define M 3
typedef struct node{
	int n;
	struct node *next;
}S_T;

S_T *mk_node(int n, S_T *next)
{
	S_T *p = malloc(sizeof(S_T));
	if(p != NULL){
		p->n = n;
		p->next = next;
	}
	return p;
}

int main(void)
{
	S_T *head, *del;
	int i;
	int N, M;
	scanf("%d %d", &N, &M);

	head = mk_node(1, NULL);
	head->next = head;
	
	for(i = 2; i <= N; i++)
    {
		//head = head->next = creat_node(i,head->next);
		head->next = creat_node(i,head->next);
		head = head->next;
	}

	while(head != head->next)
    {
		for(i = 1; i < M; i++)
        {
			head = head->next;
		}
		del = head->next;
		head->next = head->next->next;
		printf("%d out\n", del->n);
		free(del);
	}
	printf("%d survie\n", head->n);
	return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/lb$ ./myysfh 
10 3
3 out
6 out
9 out
2 out
7 out
1 out
8 out
5 out
10 out
4 survie*/

 

 

 

/*
N个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续从1开始报数,报到m的人出圈;如此往复,直到所有人出圈。
*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


//#define N 20
//#define M 5

typedef struct link_node
{
	int n;
//	char name[64]={0};
	struct link_node *next; 
}LinkNode;

//LinkNode *creat_node(int num, char *strname, LinkNode *next)
LinkNode *creat_node(int num, LinkNode *next)
{
	LinkNode *p = malloc(sizeof(LinkNode));

	if(p != NULL)
	{
		p->n = num;
		//strcpy(p->name,strname);
		p->next = next;		
	}

	return p;
}




int main(void)
{
	int i=0;
	int N=0;
	int M=0;
	LinkNode *head, *del;


	
	scanf("%d %d", &N, &M);

	head = creat_node(1, NULL);
	head->next = head;

	for(i=2; i <= N; i++)
	{
		//head = head->next = creat_node(i,head->next);
		head->next = creat_node(i,head->next);
		head = head->next;
	}
	
	while(head != head->next)
	{
		for(i = 1; i < M; i++)
		{
			head = head->next;
		}
		del = head->next;
		head->next = head->next->next;
		printf("%d out\n", del->n);
		free(del);
	}
	printf("%d survie\n", head->n);
	return 0;

}



/*
akaedu@akaedu-G41MT-D3:~/lin/lb$ ./myysfh 
10 3
3 out
6 out
9 out
2 out
7 out
1 out
8 out
5 out
10 out
4 survie*/

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用C++链表实现约瑟夫环的示例代码: ```c++ #include <iostream> using namespace std; // 定义节点结构体 struct Node { int num; // 节点编号 Node *next; // 指向下一个节点的指针 }; // 创建链表 Node* createList(int n) { Node *head = new Node(); // 创建头节点 head->num = 1; head->next = NULL; Node *cur = head; for (int i = 2; i <= n; i++) { Node *newNode = new Node(); newNode->num = i; newNode->next = NULL; cur->next = newNode; cur = newNode; } cur->next = head; // 链接成环形链表 return head; } // 删除第m个节点 void deleteNode(Node *&head, int m) { Node *cur = head; Node *prev = NULL; // 找到第m个节点和它前一个节点 while (cur->num != m) { prev = cur; cur = cur->next; } // 删除第m个节点 if (prev == NULL) { // 删除头节点 head = cur->next; } else { prev->next = cur->next; } delete cur; } // 约瑟夫环 void josephus(int n, int m) { Node *head = createList(n); Node *cur = head; while (cur->next != cur) { // 当只剩下一个节点时退出循环 // 找到第m个节点 for (int i = 1; i < m; i++) { cur = cur->next; } // 删除第m个节点 int num = cur->num; deleteNode(head, num); cur = cur->next; // 继续从下一个节点开始找 } // 输出最后剩下的节点编号 cout << "Last node: " << cur->num << endl; // 释放链表内存 delete cur; } int main() { int n, m; cout << "Enter n and m: "; cin >> n >> m; josephus(n, m); return 0; } ``` 该实现方式中,首先创建一个包含n个节点的环形链表,然后从头节点开始,每次找到第m个节点并删除它,直到链表只剩下一个节点为止。最后输出剩下的节点编号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值