web123456

Data structure-Joseph Ring (Round Link List)

n data elements form a ring, counting from any position in the ring, counting to m, taking the element out of the table, repeating the above process until only one element remains in the table.
Tip: Use a recurrent single linked list with headless nodes to realize the storage of n elements.

Sample:
enter:
10 3 1 //The number of the number of people who delisted are respectively, and the number of the number of people who start counting.
Output:
3 6 9 2 7 1 8 5 10 4 //The number of the person who delisted is given in the order of delisting.

#include<>
 #include<>
 #define ERROR 0
 #define OK 1
 typedef int ElemType; /*Define the type of table element*/
 typedef struct LNode /*Single-linked table storage for linear table*/
 {
     ElemType data;
     struct LNode *next;
 } LNode,*LinkList;

 LinkList Creat(int t)
 {
     int n;
     LinkList p1,p2,head;
     p1=(LinkList)malloc(sizeof(LNode));
     p2=(LinkList)malloc(sizeof(LNode));
     head = NULL;
     n = 1;
     while(n<=t)
     {
         p1->data = n;
         if(n==1)
             head = p1;
         else
             p2->next = p1;
         p2 = p1;
         if(n!=t)
             p1 = (LinkList)malloc(sizeof(LNode));
         n++;
     }
     p2->next = head;
     return head;

 }
 LinkList Start(LinkList L,int k)
 {
     int i;
     i=1;
     While(i!=k)
     {
         L=L->next;
         i++;
     }
     return L;

 }
 LinkList Delete(int n,int m,LinkList L)
 {
     int i,j,k;
     LinkList p;
     p=L;
     k=0;
     While(n>1)
     {
         j=0;
         for(i=1+k; i<=n; i++)
         {
             if((i+1)%m==0)
             {
                 printf("%d",p->next->data);
                 p->next=p->next->next;
                 i++;
                 j++;

             }
             p=p->next;
             k=i%m;
         }
         n=n-j;
     }
     printf("%d\n",p->data);
     return p;
 }
 int main()
 {
     int m,n,k;
     scanf("%d%d%d",&n,&m,&k);
     LinkList L,cur;
     L=Creat(n);
     cur=Start(L,k);
     Delete(n,m,cur);
     return 0;
 }