Joseph's game of life and death (c language version)

Title description:
        A description of Joseph's problem is:
        n persons numbered 1, 2, ..., n sit in a circle in a clockwise direction, and each holds a password (a positive integer).
        At the beginning, choose a positive integer as the upper limit m of the number of reports, starting from the first person in a clockwise direction and starting from 1 to report the number sequentially, and stop reporting the number when you report to m.
        The person who reported m goes out, uses his password as the new value of m, and starts counting from 1 again from the next person in the clockwise direction, and so on, until everyone goes out.
Problem analysis:
        Analyzing the problem, we can know that the Joseph Ring of Life and Death game can essentially be understood as the application of a circular singly linked list . Each person is equivalent to a node of the linked list. The realization of killing: delete the linked list node. Repeat the operation until the last person is out.
Function code:
void Initialise() //Initialize the linked list
void Traversal() //Traverse the linked list
void Delete() //Delete the node (to achieve killing)
Complete code:

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

//定义结点
typedef struct LinkList
{
    
    
	int data;
	struct LinkList *next;
}node;
//初始化链表
 void Initialise(node **pNode,int item)//**pNode 指向指针的指针   指向的对象pNode还是指针
 {
    
    
	 node *tmp;
	 node *target;
	 int i=1;         
	 while(i <= item)
	 {
    
    
		 if(* pNode == NULL)
		 {
    
    
			 *pNode = (node *)malloc(sizeof(LinkList));
			 if(!(* pNode))
			 {
    
    
				 exit(0);//退出
			 }
			 (*pNode)->data = i;
			 (*pNode)->next = *pNode ;
			 i++;
		 }
		 else
		 {
    
    
			 target = *pNode;
			 while(target->next != *pNode)
			 {
    
    
				 target = target->next ;
			 }
			 tmp = (node*)malloc(sizeof(LinkList ));
			 if(! tmp)
			 {
    
    
				 exit(0);
			 }
			 tmp->data = i;
			 i++;
			 tmp->next  = *pNode ;
			 target->next  = tmp;
		 }
	 }
 }
 //遍历链表
 void Traversal(node *pNode)
 {
    
    
	 node *tmp;
	 tmp = pNode;
	 printf("\n\t\t*****************************链表元素*********************************\n\n");
	 do
	 {
    
    
		 printf("\t%d",tmp->data );
		 tmp = tmp->next ;
	 }while(tmp->next != pNode);
	 printf("\t%d",tmp->data );
	 puts("");
 }
 //删除结点(杀人)
 void DeleteNode(node **pNode,int x,int y)
 {
    
    
	 int people = 1;
	 int Key[80];
	 printf("请依次输入每人的密码(使用空格隔开):\n\n");
	 for(int i=1;i<=y;++i)
	 {
    
    
		 scanf("%d",&Key[i]);

	 }
	 printf("出列顺序:\n\n");
	 node *target;
	 target = *pNode ;
	 while(people <= y)
	 {
    
    
		 node *tmp;
		 if(people != 1)
		 {
    
    
			 target = target->next ;
		 }
		 if(x != 1)
		 {
    
    
			 for(int i=1;i<x-1;++i)
			 {
    
    
				 target = target->next ;
			 }
			 tmp = target->next ;
			 printf("%d\t",tmp->data );//打印删除结点标号
			 x = Key[tmp->data ];
			 target->next  = tmp ->next ;
			 free(tmp);
		 }
		 else
		 {
    
    
			 printf("%d\t",target->data );
		 }
		 people++;
	 }
 }


 int main()
 {
    
    
	 printf("Designer by MaLong\n");
	 printf("\n\t\t**********************************************************************\n");
	 printf("\t\t*                                                                    *\n");
	 printf("\t\t*                                                                    *\n");
	 printf("\t\t********************欢迎来到约瑟夫杀人游戏****************************\n"); 
	 printf("\t\t*                                                                    *\n");
	 printf("\t\t*                                                                    *\n");
	 printf("\t\t**********************************************************************\n\n\n\n");
	 int x,y;
	 printf("请输入参与游戏的人数:\n");
	 scanf("%d",&y);
	 printf("请输入初始密码:\n");
	 scanf("%d",&x);
	 node *head = NULL;
	 Initialise (&head,y);
	 Traversal (head);
	 DeleteNode (&head,x,y);
	 return 0;
 }

Operation result:
Insert picture description here
I hope to help you!
Please advise us on the shortcomings!

Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/93849397