C data structure loop link list implements Joseph's ring
The codes in this article are all run in the environment of turbo C 2.0 and get the correct results. This program uses a circular linked list to realize the Joseph ring, that is, there is an m individual standing in a circle, starting from a person (the first in the queue), and agreeing to delist the nth person starting from a certain number, and his next person reporting from the beginning, and then another person reporting n is delisted. The result of this program is the order of delisting of the personnel.
#include<> #include<> #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 it can help you. Thank you for your support for this site!