Joseph Ring Question
//Joseph Ring. CPP: Defines the entry point of the console application. //#include "stdafx.h"#include <stdio.h>#include <malloc.h>#include <Windows.h>/ * Constant definition * /#define OK 0 //Successful execution #define ERR_MEMORY-1 //memory allocation error #define ERR_INVALIDPARAM-2 //input parameter is invalid / * node Structure definition * /typedef intElemtype;//element type is shapingtypedef structnode {elemtype id;structNode* Next;} Listnode,*linklist;typedef intStatus;//function return status/* The following function function is to create a single-loop linked list of the leading nodes of length n, with each node initial ordinal (ID) being the order in which the node was created and added to the list of links */Status createlist (linklist Head,intN) {ListNode *p, *s;intIif(! Head)returnErr_invalidparam; Head->next = Head;//Initialize head nodep = Head;//p Point head node for(i =1; I <= N; i++) {s = (listnode*)malloc(sizeof(ListNode)); S->id = i; S->next = p->next; P->next = s; p = p->next; }returnOK;}/ * The following function realizes the Joseph ring problem algorithm, the basic idea is for the non-empty circular chain list from the first node (ordinal 1) to start the count, the number of nodes for M is deleted, its next node as a new starting point, starting from 1 to count, until the loop list only one node, the node is finally listed, Represent the survivor * *intJosephus (linklist Head,intm) {intICount =1, Iorder =0; ListNode *pcur, *pprev, *pdel;//Save the current count and the current dequeue order respectively if(! Head)returnErr_invalidparam;//Linked list not validPprev = Head;//pprev point to head nodePcur = head->next;//pcur point to the first node while(Pprev! = pcur->next)//When the list has only one node left{if(pcur! = Head&&icount = = m) {Pdel = Pcur;//Save the node currently being deleted to PdelPprev->next = pcur->next;//delete the current node.Pcur = pcur->next;//pcur points to the next node as a new starting pointiorder++;//dequeue order plus 1 printf("%d out-of-sequence numbers are:%d\n", Iorder, Pdel->id);//output dequeue sequence number Free(Pdel); ICount =1;//Reset count off, starting from 1}Else{if(Pcur! = Head) icount++;//Head node not countPprev = Pcur;//pprev point to current nodePcur = pcur->next;//pcur point to next node} }printf("%d out-of-sequence numbers are:%d\n", ++iorder, Head->next->id);//output final out sequence number Free(Pcur); Free(Pprev);//Release The last two nodes of the list.}/ * Main program * /voidMain () {intN, M; Linklist Joseph;//Declaration circular link List printf("Please enter number of participants N:"); scanf_s ("%d", &n);printf("Please enter the upper limit m:"); scanf_s ("%d", &m); Joseph = (listnode*)malloc(sizeof(ListNode));if(CreateList (Joseph, n)! = OK) {printf("Linked list creation Error!" \ n ");return; }if(Joseph->next = = Joseph) {printf("The number of participants entered incorrectly!" \ n ");return; }printf("\ n"); Josephus (Joseph, M); System"Pause");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Joseph Ring Question