#include<stdio.h>
#include<stdlib.h>
#define CardNumber 13
typedef struct node
{
int data;
struct node *next;
}sqlist,*linklist;
//创建13个节点的循环链表
linklist createLinkList()
{
linklist head=NULL;
linklist s,r;
r=head;
for(int i=1;i<=CardNumber;i++)
{
s=(linklist)malloc(sizeof(sqlist));
s->data=0;
if(head==NULL)
head=s;
else
r->next=s;
r=s;
}
r->next=head;
return head;
}
//发牌顺序
void Magician(linklist head)
{
linklist p;
int countnumber=2;
p=head;
p->data=1;
while(1)
{
for(int j=0;j<countnumber;j++)
{
p=p->next;
if(p->data!=0)
{
p=p->next;
j--;
}
}
if(p->data==0)
{
p->data=countnumber;
countnumber++;
if(countnumber==14)
break;
}
}
}
void DestoryList(linklist *list)
{
linklist p=*list;
linklist q=NULL;
int i=1;
while(i<=CardNumber)
{
q=p;
p=p->next;
free(q);
i++;
}
}
int main()
{
linklist p;
p=createLinkList();
Magician(p);
printf("按下列顺序排放:\n");
for(int i=0;i<CardNumber;i++)
{
printf("黑桃%d",p->data);
p=p->next;
}
DestoryList(&p);
return 0;
}
循环链表解决魔术师发牌问题
最新推荐文章于 2020-04-16 11:54:44 发布