参考代码如下:
#define ERROR 0
#define OK 1
#include <stdio.h>
#include <stdlib.h>
struct CirNode /*定义每个结点的类型 */
{
int data;
int num;
struct CirNode *next;
};
int a[30];
struct CirNode *CreateList(int n)
{
struct CirNode *L, *p, *q;
int i;
int j = 1;
L = q = (struct CirNode *)malloc(sizeof(struct CirNode));
if (!q)
return ERROR;
printf("shu ru mei ge ren de mi ma:\n");
q->num = j;
printf("%d.data=", j);
scanf("%d", &q->data);
j++;
q->next = L;
for (i = 1; i < n; i++)
{
p = (struct CirNode *)malloc(sizeof(struct CirNode));
printf("shu ru mei ge ren de mi ma:\n");
p->num = j;
printf("%d.data=", j);
scanf("%d", &p->data);
j++;
q->next = p;
p->next = L;
q = q->next;
}
return (L);
}
struct CirNode *DeleteList(struct CirNode *L, int m, int n)
{
int i, j = 0;
int k = n;
struct CirNode *p, *q, *pre;
pre = p = L;
do
{
i = 1;
while (i < m - 1)
{
p = p->next;
i++;
}
if (m == 1)
{
while (pre->next != p)
pre = pre->next;
m = p->data;
a[j] = p->num;
q = p;
pre->next = p->next;
p = p->next;
free(q);
L = pre = p;
k--;
j++;
}
else
{
q = p->next;
m = q->data;
a[j] = q->num;
p->next = q->next;
p = q->next;
free(q);
L = pre = p;
k--;
j++;
}
} while (k > 1);
a[j] = p->num;
printf("xian zai suo you ren chu lie.\n");
return (L);
}
int main()
{
struct CirNode *L;
int i, n, m;
printf("======This is a Joseph problem======\n");
printf("qing shu ru ren shu,n=");
scanf("%d", &n);
if (n > 30 || n < 1)
{
printf("ERROR! Please input again,n=");
scanf("%d", &n);
}
L = CreateList(n);
printf("Please input the first code,m=");
scanf("%d", &m);
if (m < 1)
{
printf("ERROR! Please input again,m=");
scanf("%d", &m);
}
DeleteList(L, m, n);
printf("chu dui ren de shun xu31:\n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
