魔术师发牌问题-循环链表解法
#include <stdio.h>#include <stdlib.h>
#define ElementType int
typedef struct Node {
ElementType data;
struct Node* next;
} Node, *LinkList;
void InitCreateLinkLoopList(LinkList* L);
void CreateLinkLoopList(LinkList* L, int n);
void Magician(LinkList* L);
void OutPut(LinkList* L);
int main()
{
LinkList* L = (LinkList*)malloc(sizeof(LinkList));
InitCreateLinkLoopList(L);
CreateLinkLoopList(L, 13);
Magician(L);
OutPut(L);
return 0;
}
void InitCreateLinkLoopList(LinkList* L) {
(*L) = (LinkList)malloc(sizeof(Node));
(*L)->data = 13;
(*L)->next = (*L);
}
void CreateLinkLoopList(LinkList* L, int n) {
LinkList p, r;
p = (*L);
for (int i = 0; i < n; i++) {
r = (LinkList)malloc(sizeof(Node));
r->next = p->next;
r->data = 0;
p->next = r;
p = r;
}
}
void Magician(LinkList* L) {
LinkList p, q, r;
p = (*L);
int times = 1;
int number = 1;
while (1) {
for (times = 1 ; times <= number; times++) {
p = p->next;
if (p->data != 0) {
times--;
}
}
p->data = number;
number++;
if (number == 14) {
break;
}
}
}
void OutPut(LinkList* L) {
LinkList p = (*L)->next;
LinkList r = (*L);
while (r != p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
页:
[1]