#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
int main()
{
struct Node *head, *s, *q, *t;
head = (struct Node*)malloc(sizeof(struct Node));
q = (struct Node*)malloc(sizeof(struct Node));
t = (struct Node*)malloc(sizeof(struct Node));
int n, m, count = 0, i;
printf("輸入猴子個數n m號:");
scanf("%d,%d", &n, &m);
for (i = 1; i <= n; i++)
{
s = (struct Node *)malloc(sizeof(struct Node));
s->data = i;
s->next = NULL;
if (i == 1)
{
head = s;
q = head;//head作爲表頭結點,q總是指向鏈表的最後一個結點
}
else
{
q->next = s;
q = q->next;
}
}
q->next = head;
printf("出隊前:");
q = head;
while (q->next != head)
{
printf("%d", q->data);
q = q->next;
}
printf("%d\n", q->data);
printf("出隊後:");
q = head;
do{
count++;
if (count == m - 1)
{
t = q->next;
q->next = t->next;
count = 0;
printf("%d", t->data);
}
q = q->next;
} while (q->next != q);
printf("%d\n", q->data);
system("pause");
return 0;
}
數據結構——圍圈報數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.