Joseph cycle

The first experimental data structures dragged on for a long time to complete, finally able to understand where the pointer is used loud noise, but is estimated to be me alone to write or unbearable.

But at least we have learned how to build the structure in a circular linked list, be understood that the dispensing member may comprise a pointer pointing to this type of structure, and the dynamic storage space .

1. Experiment Title: Application of the linear form

A description of Joseph (Joeph) the problem is: numbered 1,2, ..., n n individuals sitting around a circle in the clockwise direction,  

Each person holding a password (positive integer). Optionally a positive integer started as the upper limit of the number of packets m, starting from the first individual

Clockwise from a start sequence number of packets, number of packets is stopped at the registration m. M newspaper people out of the line, his password as the new value of m, starting with his next person in a clockwise direction again from number 1 newspaper, and so on, until all the columns up to everyone. A test procedure designed to determine the order of the columns.

2. demand analysis

This program is written in C language to circular list completion, to complete this program to master to create a circular list, the definition of a pointer type of member variables in the structure, the insertion node in the list, delete nodes, and how Joseph cycle for adequate understanding, through a series of sequentially outputting the column work.

① scope and form input values ​​of input: insert element is the need to enter the number of people playing the game and everyone's password, but also to enter the initial password value. All input elements must be a positive integer.

② outputted form: followed by an output column order number.

③ program functions can be achieved: 1, the end of the insertion node interpolation. 2, by deleting the password control node. 3, calculate a sequence and the sequence of the deletion node of the column;

④ Test data: m is an initial value of 20; Password: 3,1,7,2,4,8,4 (6,1,4,7,2,3,5 should correct result).

-------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<malloc.h>
typedef int datatype;
struct Node typedef
{
 DataType code;
 DataType NUM;
 struct Node * Next;
} LinkList; // Create a node pointer and a data field structure
linklist * creatlist (int x) // create lists
{
 LinkList * P = NULL, * Q = NULL, * H = NULL;
 int I, code;
 I =. 1;
 the printf ( "Please enter% d person's password:", I);
 scanf_s ( "% d", & code);
 the printf ( "\ n-") ;
 P = (LinkList *) the malloc (the sizeof (LinkList));
 p-> code = code;
 p-> NUM = I;
 p-> Next = NULL;
 I ++;
 H = P;
 the while (I <= X)
 {
  printf ( "% d people enter the first password:", I);
  scanf_s ( "% d", & code);
  printf ( "\ n-");
  = Q (LinkList *) the malloc (the sizeof (LinkList));
  Q-> code = code;
  Q-> NUM = I;
  Q-> Next = NULL;
  p-> Next = Q;
  P = Q;
  I ++;
 }
 Q -> Next = H;
 return H;
}
void printList (LinkList H *, int X) // Print out position and a password
{
 LinkList P *;
 int. 1 = I;
 P = H;
 IF (P = NULL!)
  the while ( I <= X)
  {
   the printf ( "% D \ D T% \ n-", p-> NUM, p-> code);
   P = p-> Next;
   I ++;
  }
 the printf ( "\ n-\ n-");
}
after void joseph (linklist * h) // initial password input value cycle Joseph
{
 LinkList P *, Q *, V *;
 int m, K =. 1;
 = H- P> Next;
 Q = H;
 the printf ( "Please enter the password first initial value:");
 scanf_s ( "% D", & m);
 the printf ( "\ n-");
 the printf ( "Final results : ");
 ! the while (P = Q)
 {
  K ++;
  IF (K == m)
  {
   the printf ("% D \ T ", p-> NUM);
   m = p-> code;
   K =. 1;
   IF ( H-> Next == P)
    H-> = p-Next> Next;
   Q-> = p-Next> Next;
   V = P;
   P = p-> Next;
   Free (V);
  }
  Q = P;
  P p-=> Next;
 }
 the printf ( "% D \ T", p-> NUM);
 the printf ( "\ n-");
}
 
void main ()
{
 LinkList * H;
 int the X-;
 printf ( "Please enter the number of participants game:");
 scanf_s ( "% d", & the X-);
 printf ( "\ the n-");
 H = creatlist (the X-);
 printf ( "everyone's number and password is: \ n-");
 printList (H, X);
 Joseph (H);
}

Guess you like

Origin www.cnblogs.com/ArnoldSchwarzenegger/p/11878144.html