Joseph Ring (n personal sit around, C language, data structure)

Source: Internet
Author: User

Joseph Ring Problem (c language, data structure edition)

First, the problem description

N Personal Siege of a table (the first connected), agreed to count from 1, the number of people reporting k out, and then the next one from 1 to report, and so on. The last one left wins. (There are many similar problems, such as the choice of the Monkey King, etc., the solution is the same)

Second, the Thinking analysis

(1) The order of people can be simply numbered, from 1 to N;

(2) construct a circular chain list, which can solve the problem of the first connection, and if the person's number is changed to the name or other convenient

(3) Inserting a person's number into the data domain of the structure;

(4) The number of the person to be traversed, and the number of the person who participated;

(5) began to count, from the beginning of the count, the person to report K out (delete the sub-node), (output out of the people more humane) to avoid waste, can release the secondary node. Until the number is only one person, exit the loop. Output the winning person.

Third, the implementation of the algorithm

(1) Structural structure

typedef struct node{

int num;//data Field

struct Node *next;

}josenode, *pnode, *hnode;//pnode as pointer variable, Hnode as head node.

(2) Get the head node.

Hnode h = ((Hnode) malloc (sizeof (Josenode)));

(3) Initializing the circular single link list

int Joseinit (Hnode *h)

{

if (!h)

{

printf ("Initialize linked list Error!") \ n ");

return 0;

}

(*h)->next = (*h);//cyclic single-linked list

return 1;

}

(4) Insertion algorithm

int Joseinsert (josenode *h, int pos, int x)

{

Pnode p=h,q;

int i=1;

if (pos = = 1)/*/tail interpolation */

{

P->num = x;

P->next = p;

return 1;

}

while (I<POS-1)

{

p=p->next;

i++;

}

Q= (pnode) malloc (sizeof (Josenode));

q->num=x;

q->next=p->next;

p->next=q;

return 1;

}

for (i = 1; I <=N; i++)

{

Joseinsert (h, I, I);

}

(4) Traversal algorithm

void Traverselist (Hnode h, int M)

{

int i = 0;

Pnode p = h;

printf ("The number of persons involved is: \ n");

while (I<M)

{

printf ("%d\t", p->num);

p = p->next;

i++;

}

printf ("\ n");

}

(5) Find out the person and delete (delete algorithm)

int Josedelete (hnode h, int M, int k)

{int i;

Pnode p=h,q;

while (m>1)//loop termination condition, only one person left

{

for (i=1;i<k-1;i++)//Here is i<k-1, because the one to find is the last node of the outgoing person ①

{

p=p->next;

}

q=p->next;

p->next=q->next;

printf ("Outgoing man:%d \ n", q->num);

Free (q);//Release secondary node

p=p->next; //relative to the ①, ① found only the first node of the person who was out.

m--;

}

printf ("*************** winner:%d ***************", p->num);

return 1;

}

Iv. Complete code

#include <stdio.h>

#include <malloc.h>

/* Build struct */

typedef struct node{

int Num;

struct Node *next;

}josenode, *pnode, *hnode;

/********** initializing a cyclic single-link list *********/

int Joseinit (Hnode *h)

{

if (!h)

{

printf ("Initialize linked list Error!") \ n ");

return 0;

}

(*h)->next = (*h);//cyclic single-linked list

return 1;

}

/************* single-linked list insert Operation **********/

int Joseinsert (josenode *h, int pos, int x)

{

Pnode p=h,q;

int i=1;

if (pos = = 1)/*/tail interpolation */

{

P->num = x;

P->next = p;

return 1;

}

while (I<POS-1)

{

p=p->next;

i++;

}

Q= (pnode) malloc (sizeof (Josenode));

q->num=x;

q->next=p->next;

p->next=q;

return 1;

}

/* Traverse */

void Traverselist (Hnode h, int M)

{

int i = 0;

Pnode p = h;

printf ("The number of persons involved is: \ n");

while (I<M)

{

printf ("%d\t", p->num);

p = p->next;

i++;

}

printf ("\ n");

}

/************** Exit Function ****************/

int Josedelete (hnode h, int M, int k)

{int i;

Pnode p=h,q;

while (m>1)

{

for (i=1;i<k-1;i++)

{

p=p->next;

}

q=p->next;

p->next=q->next;

printf ("Outgoing man:%d \ n", q->num);

Free (q);

p=p->next;

m--;

}

printf ("*************** winner:%d ***************", p->num);

return 1;

}

/***************************************/

int main ()

{

int i;//counter

int n;//Number of participants

int k;//count off password

printf ("Please enter the number of participants:");

scanf ("%d", &n);

printf ("Please enter the exit password:");

scanf ("%d", &k);

/************** get the head node ****************/.

Hnode h = ((Hnode) malloc (sizeof (Josenode)));

/*************** initializing single-linked list ************/

Joseinit (&H);

/****** inserts the number into the circular list ******/

for (i = 1; I <=N; i++)

{

Joseinsert (h, I, I);

}

/************** traversal single-linked list ***************/

Traverselist (H,n);

/**************************************/

Josedelete (H, N, K);

printf ("\ n");

printf ("\ n");

return 0;

}

Five, experimental verification

  

Proven, experimental success! If there is a shortage of places, please point out to discuss together! If you can't write clearly, you can contact me.

Joseph Ring (n personal sit around, C language, data structure)

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.