请问我编写的这个实现 约瑟夫环 的问题哪里错了?
程序代码:
#include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; typedef struct Node ListNode; typedef struct Node *LinkList; LinkList CreatCyclelist(int n)//创建一个不带头节点的循环单链表 { LinkList head = NULL; int e; int i; ListNode *p, *q; q = NULL; for (i = 1; i <= n; i++) { printf("请输入元素:\n"); scanf_s("%d",&e); if (i == 1) { head = (LinkList)malloc(sizeof(ListNode)); head->data = e; head->next = NULL; q = head; } else { p = (LinkList)malloc(sizeof(ListNode)); p->data = e; p->next = NULL; q = p; } } if (q != NULL) q->next = head; int m,counter=2; ListNode *s; ListNode *Q; s = Q = NULL; s = head->next; printf("请输入一个正整数作为报数上限值"); scanf_s("%d",&m); if (m == 1) { q->next = head->next; free(head); } else { while (counter < m) { s = (LinkList)malloc(sizeof(ListNode)); s=s->next; counter++; } if (counter == m) { Q = (LinkList)malloc(sizeof(ListNode)); Q = s; s = head; while (s->next != Q) s = s->next; s->next = Q->next; free(Q); } } return head; } void DisplayCycleList(LinkList head) { ListNode *p; p = head; if (p == NULL) { printf("该链表是空表"); return; } while (p->next != head) { printf("%d", p->data); p = p->next; } printf("%4d", p->data); } main() { int n; LinkList h; printf("请输入顺序链表的表尾值:"); scanf_s("%d",&n); h = CreatCyclelist(n); DisplayCycleList(h); return 0; }