魔术师发牌问题简单实现-循环链表

#include
#include


#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 {
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 {
printf("黑桃%d",p->data);
p=p->next;


}


DestoryList(&p);
return 0;
}

你可能感兴趣的:(基本算法,struct,typedef,魔术师发牌问题,循环链表)