Joseph's problem is solved with a circular single -linked list with no head nodes

tags: Data structure C language  Linked list  data structure  C language

Problem Description:

There are n individuals sitting around a round table, and now the number of people from S, the number of people who count it to M, and then start the number of people from the next person. Column, ..., so repeated until all people are all listed. The Josephus question is: For any given N, S, M, find the sequence of N personnel obtained by the order of the column.

Now take n = 8, s = 1, m = 4 as an example. The solution process of the problem is shown in Figure 2-15. In the figure, the S1 refers to the starting number of the start. If the initial order is N1, N2, N3, N4, N5, N6, N7, N8. Then the problem is N4, N8, N5, N2, N1, N3, N7, N6.

We are now using a cycle list of non -head nodes to solve this problem.

/*
    Ask Joseph's issue of element sequence     
         Entry parameters: Points to the first node of the first node of the circular single linked list that has been stored in, starting position S, and steppe M.
         Export parameters: 1 means successful, 0 indicates that there are no elements in the table
*/
int josephus_LinkList(LinkList josephus_Link, int s, int m)
{ 
         Linklist p = Josephus_link; // p is used to store pointers at the node where the starting location is located, or to point the pointer to the node where the element is located. Initially pointing to the linked list the first node
         Linklist Pre = NULL; // Pre is used to store pointers to the front -drive node of the nodes where the element is located, which is convenient for subsequent listing to delete operations 
         int count; // used to count
    
         // Judging whether the table is empty
    if ( !josephus_Link )  
    {   
                 Printf ("No Element in the Table");
        return 0;
    }
    
         // Find the pointer to the starting position and temporarily use P storage (so you can use P to further find the pointer to the node where the element is available)
    for(count = 1; count < s; count++) 
        p = p->next;

         Printf ("Output Joseph Sequence:");
    
         // The condition of the cycle is when the current pointer is not pointing to yourself (in the cycle single chain list, when the pointer pointing to a node point to yourself, it means that the node is the only node)
    while (p != p->next)    
    {    
                 // Use the same way to find the pointer to the starting position earlier to find the pointer to the position to be available.
        for(count = 1; count < m; count++)  
        {  
                         // Pre is a pointer to the front -drive node of the node to be available to the node to be available, which is convenient for subsequent listing to delete
            pre = p; 
            p = p->next;
        } 
        
                 // List, the element to be listed out of the output
        printf("%d\t", p->data);

                 // List, delete the nodes where the element is located
                 // In the first step, the front -drive node of the node where the element is located is hung on the rear -drive node of the node where the element is located
        pre->next = p->next;

                 // Step 2, release the space distributed by the node where the element is located
        free(p);
        
                 // Point P to the rear -drive node of the node where the element is available (so P points to the new starting location)
        p = pre->next;
    }
 
         // When the pointer pointed to the pointer to a node, the While loop ends, and only the last node is left in the linked list
         // Delete the operation department of the last node
    printf("%d\t", p->data); 
    free(p);
    
    return 1; 
}

The code that can be run below (remove the Josephus_linkList () function defined above)

#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
    int data;
    struct Node * next;
}LNode, * LinkList;

 Linklist Create_linkList (); // Create a circular single -linked list without head nodes
 int JosePhus_linkList (linklist, int, int); // Joseph
 void show_linkList (linklist); // Traversing output cyclic single -linked tables

int main(void)
{
    LinkList Ll = NULL;
    Ll = create_linklist();

    show_linklist(Ll);

    josephus_LinkList(Ll, 1, 4);


    return 0;
}

LinkList create_linklist()
{
         int len; // The number of nodes used to store nodes
         int i; // for cycle variable in the loop
         int value; // Used to store the value domain of nodes

         Linklist ll = null; // LL is used to store pointers to the first node
         LinkList Pre = NULL; // Pre is used to store pointers to the front -drive node to point to LNEW to the node, and the initial point to the first node

         // Request the number of nodes in the user
         Printf ("Please enter the number of nodes: \ n");
    scanf("%d", &len);

    for (i = 0; i < len; i++)
    {
                 // Generate a new node every cycle
        LinkList LNew = (LinkList)malloc(sizeof(LNode));

                 // Determine whether the memory is allocated successfully
        if(!LNew)
        {
                         Printf ("Dynamic memory allocation fails, the program exits! \ n");
            exit(-1);
        }

                 // Require users to enter the value of new nodes
                 Printf ("Please enter the value of the pool dit node \ n", i+1);
        scanf("%d", &val);

                 // I = 0, that is, when the first runtime of the cycle (when the first node is generated)
        if(0 == i)
        {
                         // Save the pointer to the first node in LL, which is convenient for subsequent operations
            Ll = LNew;

                         // Initialize Pre, point it to the first node
            pre = Ll;
        }

                 // Storage the value entered by the user to the data domain
        LNew->data = val;

                 // Hang the original node on the newly generated node
        pre->next = LNew;

                 // The new node becomes the last node, which needs to point its pointer domain to the first node to form a circular single linked list
        LNew->next = Ll;
        
                 // Pre to move it later, so that it always points to the front -drive node of LNEW to the node, which is convenient for the front -drive node (original node) of LNEW to the node (the original node)
        pre = LNew;
    }

         // Reminder the successful creation of the linked list
         Printf ("Links are successful! \ n");

         // Back to the pointer to the first node
    return Ll;   
}

 // The traversal output of the cycle single chain list
void show_linklist(LinkList Ll)
{
    LinkList p = Ll;

    if(!Ll)
        return;

    while(true)
    {
        printf("%d ", p->data);

        p = p->next;

        if(p == Ll)
            break;
    }

    printf("\n");

    return;
}

Intelligent Recommendation

Joseph's problem (circular linked list solution)

From the question, you can draw a picture of n people in a circle When kicking a person, we can point him to the next person in front of him, so that we can kick this person, just like kicking 7 , The...

Solving Joseph's Problem with Circular Singly Linked List

Problem description: N people form a circle, start counting from the first person, those who report to m go out of the circle, the rest continue to report counting from 1, and those who report to m go...

Joseph's Problem of C++ Circular Linked List

I learned the circular linked list the day before yesterday, and I have never cared about it anymore. I applied it today, and I feel troubled to write it... The difference between circular linked list...

Java circular linked list and Joseph's problem

Circular linked list and Joseph's problem Continuing the single-join table in the previous article, this article mainly writes about the circular linked list and the Joseph problem Circular linked lis...

One-way circular linked list and Joseph's problem

One-way circular linked list (CircleSingleLinkedList) One-way circular linked list, that is, one-way circular linked list, that is, on the basis of the singly linked list, the next pointer of the tail...

More Recommendation

Solving Joseph's suicide problem with circular linked list

1. The source of the problem: It is said that the famous Jewish historian Josephus has the following story: After the Romans occupied Chotapat, 39 Jews hid in a cave with Josephus and his friends. 39 ...

Joseph's Problem---One-way Circular Linked List

include using namespace std; define N 41 define M 3 struct node{ int value; struct node * next; }; typedef struct node* Node; bool initNode(Node *head); bool insert_tail(Node head, int value); void pr...

Circular linked list to solve Joseph's problem

Title description   There are n people in a circle, arranged in order. Start counting from the first person (from 1 to 3). Anyone who reports to 3 will withdraw from the circle and ask which...

Solve Joseph's problem with circular linked list

This question is actually a change of the Joseph question. The idea is clearest if you use a circular linked list to solve the problem. The code is as follows, pay attention to the explanation of the ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top