魔术师发牌问题练习

#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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值