#include "stdio.h"
#include "stdbool.h"
#include "stdlib.h"
#include "stdarg.h"
#include "string.h"
#include "math.h"
#include "time.h"
#include "errno.h"
typedef int Status;
typedef int Elemtype;
typedef struct node
{
Elemtype data;
struct node *next;
}Node;
typedef struct node* LinkCList;
LinkCList CreatCycleList();
void Magic(LinkCList CL) ;
void freelist(LinkCList *CL);
int main(void)
{
LinkCList CL;
int i = 0;
CL = CreatCycleList();
Magic(CL);
for(i = 0; i < 13;i++)
{
printf("黑桃%d ",CL->data);
CL = CL->next;
}
freelist(&CL);
return 0;
}
LinkCList CreatCycleList()
{
LinkCList head,rear,temp;
int i;
head = NULL;
rear = head;
for(i = 0;i < 13 ;i++)
{
temp = (LinkCList)malloc(sizeof(Node));
temp->data =0;
if(head == NULL)
{
head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
rear->next = head;
}
return head;
}
void Magic(LinkCList CL)
{
LinkCList p;
int i ,n = 2;
p = CL;
p->data = 1;
while(1)
{
for(i = 0 ;i < n;i++)
{
p = p->next;
if(p->data != 0)//这张牌已经被抽掉,+1次循环找下一个空位
{
i--;
}
}
p->data = n;
n++;
if(n > 13)
{
break;
}
}
}
void freelist(LinkCList *CL)
{
LinkCList temp = *CL;
LinkCList buff[13];
int i = 0;
for(i = 0;i < 13;i++)
{
buff[i] = temp;
temp = temp->next;
}
for(i = 0;i < 13;i++)
{
free(buff[i]);
}
*CL = 0;
}
魔术师发牌问题练习
最新推荐文章于 2021-05-18 23:20:21 发布