Data Structure--Linear Chain Table and Its Application--Joseph Ring

Data Structure - Linear Chain Table and Its Application - Joseph Rings

[Experimental Purpose]

Help students master the basic operation of linear tables in order and chain storage structure, with the operation and application of various chain tables as the focus.

[Experimental Contents and Requirements]

1. Description of the problem: A description of the Joseph problem is numbered 1,2,3,...n individuals of n sit in a clockwise circle, each holding a password (positive integer).Start by choosing a positive integer as the upper limit M. Start from the first person and report clockwise from 1. Stop reporting at M.The person who reported m listed his password as the value of the new m, starting from the next person clockwise, counting from 1 again, and so on until all the people listed.Try to design a program to find out the order of the columns.
2. Basic operation: Simulate this process by using one-way circular chain table storage structure, print each person's number in the order listed.
3. Test data: m has an initial value of 20; n=7,7 passwords are 3,1,7,2,4,8,4, followed by 6 (the correct list order should be 6,1,4,7,2,3,5).
4. Implement tips: After the program runs, the user is required to specify an initial maximum number of reports, and then read each person's password.You can set n < 30.There is no need for a "header node" in the looping list used in this topic. Note the boundaries between empty and non-empty tables.
5. Selection: union, intersection and difference of sets.

[Experimental data]:

#include <malloc.h>
#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
typedef int Status;
typedef double ElemType;
//-----------------------------------
//Define a one-way circular chain table
typedef struct LNode
{
    int number;
    int data;
    struct LNode *next;
} LNode, *LinkList;
//-----------------------------------
LinkList EvaluList(int n);//Tail insert assignment to one-way circular chain list
int size(LinkList L);//Number of nodes in the chain table
Status ScanList(LinkList L);//Traversing a one-way circular chain table
Status Joseph(LinkList &L,int m);//Realization of Joseph Ring

//---------------------------------------------------------------
LinkList EvaluList(int n)
{
    if(n==0)
        return NULL;
    int key;
    cout<<"Enter the password of the first person ";
    cin>>key;
    LinkList L=new LNode;
    L->data=key;
    L->number=1;
    L->next=L;
    for(int i=2; i<=n; i++)
    {
        LinkList p=new LNode;
        int key;
        cout<<"Enter #"<<i<<"Personal password ";
        cin>>key;
        p->data=key;
        p->number=i;
        p->next=L->next;
        L->next=p;
        L=L->next;
    }
    cout<<endl;
    L=L->next;
    return L;
}
//------------------------- Find the number of nodes in the list of chains---------------------
int size(LinkList L)
{
    if(L==NULL)
        return 0;
    int i=1;
    LinkList p=L->next;
    while(p!=L)
    {
        i++;
        p=p->next;
    }
    return i;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
Status ScanList(LinkList L)
{
    LinkList p=L;
    if(p==NULL)
    {
        cout<<"Number of people is empty"<<endl;
        return FALSE;
    }
    cout<<"Password for Person 1 ";
    cout<<p->data<<endl;
    p=p->next;
    while(p!=L)
    {
        cout<<"No."<<p->number<<"Personal password ";
        cout<<p->data<<endl;
        p=p->next;
    }
    cout<<endl;
    return TRUE;
}
//------------------ Realization of Joseph Ring-------------------------
Status Joseph(LinkList &L,int m)
{
    if(L==NULL)
    {
        cout<<"Number empty, end of listing"<<endl;
        return FALSE;
    }
    LinkList p=L;
    cout<<"Password is: "<<m<<endl;
    while(p->next!=L)
        p=p->next;
    for(int n=size(L); n>0 ; n--)
    {
        for(int i=1; i<=m%n-1; i++)
            p=p->next;
            m=p->next->data;
        cout<<"Password is: "<<m<<"       The column number is: ";
        cout<<p->next->number<<endl;
        LinkList q=p->next;
        p->next=q->next;
        free(q);
    }
    return OK;
}
//-------------------------------------------------
int main()
{
    int m,n;
    cout<<"Please enter your initial password(positive integer)And number"<<endl;
    cin>>m>>n;
    cout<<endl<<"Please enter"<<n<<"Personal password"<<endl<<endl;
    LinkList L=EvaluList(n);
    cout<<n<<"Personal password is"<<endl;
    ScanList(L);
    cout<<n<<"Individuals are listed in the order"<<endl;
    Joseph(L,m);
}

[Experiments]

Seven original articles were published. 2. Visits 213
Private letter follow

Posted on Thu, 06 Feb 2020 22:39:29 -0500 by jbruns