The C data structure circular linked list implements the Joseph ring

  • 2020-05-19 05:14:13
  • OfStack

The C data structure circular linked list implements the Joseph ring

This article code in turbo C 2.0 running through the environment, and get the correct result, this procedure to implement Joseph ring with a circular linked list, namely m people stand in a circle, since someone (1) queue number off, agreed the first n personal dequeue, starting from a certain number of his next again starting from 1 to 1, but a reported n dequeue again, the results of this program for a dequeue order,


#include<stdio.h>
#include<conio.h>
#define OK  1
#define NULL 0
typedef int status;
typedef int  ElemType;
typedef struct LNode{
        ElemType data;
        struct LNode *next;
        }LNode,*LinkList;
LinkList L;
status CreateList_L(LinkList *L,int m)
{LNode *p,*q;
 int i;
 *L=(LinkList)malloc(sizeof(LNode)) ;
 q=*L;
 q->data=1;
 for(i=2;i<=m;i++)
 {p=(LinkList)malloc(sizeof(LNode));
  p->data=i;
  p->next=NULL;
  q->next=p;
  q=p;
 }
  q->next=*L;
  return OK;
}
status function(LinkList *L,int m,int n)
{LNode *p,*q;
int i,j=1,k=1;
p=*L;
q=p;
do
{p=q->next;j++;
if(j%n==0)
{printf("%3d",p->data);
q->next=p->next;
k++;
free(p);
}
else q=p;
}while(k<=m);
return OK;
}
void main()
{int m,n;
 clrscr();
 gotoxy(5,8);
 printf("***************************************************\n");
 gotoxy(5,9);
 printf("**** the list's length is :          ****\n");
 gotoxy(35,9);
 scanf("%d",&m);
 gotoxy(5,10);
 printf("****the xunhuan's length is :         ****\n");
 gotoxy(35,10);
 scanf("%d",&n);
 gotoxy(5,11);
 printf("***************************************************\n");
 CreateList_L(&L, m);
 function(&L,m,n);
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: