Joseph Ring Problem 2

[Title description]
A description of the Joseph ring problem is: n persons numbered 1, 2, ..., n sit in a circle in a clockwise direction, and each person holds a password (positive integer). At the beginning, choose a positive integer as the upper limit of the number of reports m, start from the first person in a clockwise direction and start from 1 to report the number, stop reporting the number when you report to m, and the person who reports to m will list his password As a new value of m, start counting again from the next person in the clockwise direction, and so on, until everyone is out. Try to design a program to find out the order.
[Basic requirements]
Use a one-way circular list storage structure to simulate this process, and print out personal numbers in the order of listing.
[Test data]
The initial value of m is 20; the passwords of n=7,7 are: 3,1,7,2,4,8,4, first the value of m is 6, and the correct order of listing is 6. 1,4,7,2,3,5.
[Realization reminder]
Set n<=30;

#include<stdio.h>
#include<stdlib.h>
#define N 30		//n<=30;
int a[N];			//定义一个全局的数组用来存储每个人的密码
typedef struct Node{
	int data;		//data用来存储每个人的编号,即下标
	struct Node *next;
}Node,*LNode;
	
LNode InitLNode(int n){
	LNode p,tail;
	LNode head=(LNode)malloc(sizeof(Node));
	if(head==NULL)
		return 0;
	head->next=NULL;
	head->data=n;
	tail=head;
	for(int i=1;i<=n;i++){
		p=(LNode)malloc(sizeof(Node));
		p->data=i;			//给他们编号
		p->next=tail->next;
		tail->next=p;
		tail=p;
	}
	tail->next=head->next;
	return head->next;
}

void Dele_List(LNode List,int n,int m){
	LNode p=List;
	LNode tail=List;
	while(p->next!=p){		//	循环遍历
		for(int i=1;i<m;i++){		//遍历前m-1个节点,使得p正好指向第m个节点
			tail=p;
			p=p->next;
		}
		tail->next=p->next;			
		printf("出列的下标为:%d\n",p->data);
		m=a[p->data];			//把第m个节点的密码赋给m
		free(p);		//删除第m个节点
		p=tail->next;		
	}
	printf("出列的下标为:%d\n",p->data);		//因为上面那个循环并不能正好把列表中的节点释放完,最后还剩一个,需要单独释放
	free(p);
}

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	LNode List=InitLNode(n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	Dele_List(List,n,m);
	return 0;
}

要点:
1.出队函数中的循环条件
2.循环并不能删除所有节点

Guess you like

Origin blog.csdn.net/qq_45465526/article/details/101559775