Implementation of a Circular Single-chain table with Joseph's Problem

Source: Internet
Author: User

In our sophomore year, we started to learn the data structure. Fortunately, we had a good time at home during the summer vacation. Now we are not very difficult to learn. Haha.

The question of Joseph is clear to everyone. The question behind the data structure book is to talk about it. At the next glance, I thought about it for a while. Then, the program will be knocked out.

# Include <stdio. h>

# Include <alloc. h>

# Include <time. h>

I will first include three headers. I don't need to mention the first two. In the next time post-file, I want it to automatically generate a password, saving myself the need to input it. Oh, I am so lazy ~

Typedef struct cycle

{

Int key;

Int node;

Struct cycle * next;

} * Cycle;

Define the data type, key-Password, node-number of each person.

Next we will create 20 nodes and 21 leading nodes. Password range: 0 ~ 20.

Cycle create ()

{

Cycle head;

Cycle P, last;

Short int flag = 1, nodeadder = 0;

If (null = (Head = (cycle) malloc (sizeof (struct cycle ))))

{

Printf ("error, sufficient memory! /N ");

Exit (0 );

}

Head-> key =-1;

Head-> node =-1;

Head-> next = NULL;

Last = head;

 

While (FLAG)

{

If (null = (P = (cycle) malloc (sizeof (struct cycle ))))

{

Printf ("error, insufficient memory! /N ");

Exit (0 );

}

P-> node = ++ nodeadder;

P-> key = rand () % 21;

P-> next = NULL;

 

If (null! = Head-> next)

{

Last-> next = P;

}

Else

{

Head-> next = P;

}

Last = P;

Last-> next = head;

 

If (nodeadder> = 20)

{

Flag = 0;

}

}

Return head;

}

The output of the cyclic single-chain table is actually not very useful to the program, but only to detect the generation of the cyclic single-chain table.

Void output (cycle head)

{

Cycle P = head-> next;

While (-1! = P-> node)

{

Printf ("% d: % d/N", p-> node, p-> key );

P = p-> next;

}

}

Output the password of each node from the cyclic single-chain table. In this function, first determine the maximum number of reports. Here I enter 20.

Then output the result:

Please input the maxnum: 20

Node: Key

20: 1

1: 0

3: 12

6: 19

10: 15

15: 16

4: 2

13: 9

7: 4

19: 6

18: 7

5: 9

12: 10

9: 5

14: 17

11: 12

8: 16

17: 13

16: 7

2: 2

Press any key to continue...

 

Void outfromqueue (cycle head)

{

Cycle P = head;

Cycle pre = P;

Short int maxnum = 0, Count = 0, listlength = 20;

// Used for Count counting. When Count ++ reaches 20, the node password is output and the count is reset to 0.

// Listlength: the length of the linked list is 20.

Printf ("Please input the maxnum :");

Scanf ("% d", & maxnum );

Getchar ();

Printf ("node: Key/N ");

While (pre = P) & (P = p-> next )! = NULL ))

{

If (listlength <= 0) // when the 20 nodes in the linked list are out of the column, the next of the header node is empty, and a loop is introduced.

{

Head-> next = NULL;

}

If (P = head) // If P is the same as the header node pointer, it does not run

{

Continue;

}

Count ++;

 

If (maxnum = count)

{

Printf ("% 2D: % 2D/N", p-> node, p-> key );

Pre-> next = p-> next;

Free (P );

P = pre;/* I wrote P = pre-> next in this place. Later, after debugging, I found it was wrong! Change to P = pre. The result is correct */

Listlength --;

Count = 0;

}

}

}

 

Int main ()

{

Cycle head;

Srand (unsigned INT) Time (null ));

Clrscr ();

Head = create ();

Output (head); // checks the generation of linked lists.

Outfromqueue (head );

 

System ("pause ");

Return 0;

}

This program is running, but it still feels a bit problematic. I can see some help and see if I can find some problems and want to communicate with you. Thank you ~ Haha

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.