Solving Joseph ring problem with linear table (C language)

Source: Internet
Author: User

#include <stdio.h>#include <stdlib.h>#define OK 1;#define ERROR 0;typedefintStatus;typedefintelemtype;typedef struct cnode{elemtype data; struct Cnode*next;} CNode; CNode*joseph; Status Create_clist (CNode*clist,intN) {CNode*p,*q;intI Clist=null;//Null=0? for(i = n; I >=1; i--) {p= (CNode*)malloc (sizeof (CNode));if(P==null) {return-1;//Storage space allocation failed} p->data=i; P->Next=clist; clist=p;if(i==n) {Q=p; }    }Q-Next=clist; Joseph=clist;returnOK;} Status Joseph (CNode*clist,int m,intNintK) {intI CNode*p,*q;if(m>n) {returnERROR;}if(! Create_clist (Clist,n)) {returnERROR;} P=joseph; for(i =1; I <m; i++) {p=p->Next;} while(p) { for(i =1; I <k-1; i++) {p=p->Next; }Q=p->Next;printf("%d\ n",Q->data);if(p->Next==p) {p=null; }Else{p->Next=Q-Next; P=p->Next; FreeQ); }}clist=null;} void Main () {int m, N,k,i; CNode*clist; CList =null;printf("\ n Please enter the number of people sitting around the round Table N:"); scanf"%d", &n);printf("\ n Please enter the first time to start the person's position m:"); scanf"%d",&m);printf("\ n Please enter the number of people you wish to count down to? : "); scanf"%d", &k); Create_clist (Clist,n);printf("\ nthe order of the dequeue is as follows: \ n"); Joseph (CList,m, n,k);system("Pause");}

Solving Joseph ring problem with linear table (C language)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.