洗牌程序

洗牌程序。

用链表实现。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct tcard {
	char number;
	char color;
	struct tcard * next;
} card;


card * init_card(void)
{
	card * head = (card *) NULL;
	card * tail = (card *) NULL;
	card * temp = (card *) NULL;
	char card_color[4] = { '\03', '\04', '\05', '\06' };										//花色
	char card_number[14] = { 'A','2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K','Z'};// Z 代表王 0 代表10

	int i, j;

	for(i = 0; i < 4; i++){							//四种花色的牌
		for(j = 0; j < 13; j++){					//十三种点数的牌
			temp = (card *) malloc (sizeof(card));
			if((card *) NULL == temp){
				printf("malloc memory for temp failed!\n");
				exit(1);
			} else {
				temp->color = card_color[i];
				temp->number = card_number[j];
				temp->next = (card *) NULL;
			}

			if(head == (card *) NULL){
				head = temp;
				tail = temp;
			} else {
				tail->next = temp;
				tail = tail->next;
			}
		}
	}

	temp = (card *) malloc (sizeof(card));
	if(temp == (card *) NULL){
		printf("malloc memory for temp failed!\n");
		exit(0);
	} else {
		temp->color = card_color[0];
		temp->number = card_number[13];
		temp->next = (card *) NULL;
		tail->next = temp;
		tail = tail->next;
	}//大王

	temp = (card *) malloc (sizeof(card));
	if(temp == (card *) NULL){
		printf("malloc memory for temp failed!\n");
		exit(0);
	} else {
		temp->color = card_color[3];
		temp->number = card_number[13];
		temp->next = (card *) NULL;
		tail->next = temp;
		tail = tail->next;
	}//小王

	return head;
}

card * flush_card(card * head)
{
	card * result = (card *) NULL;
	card * result_tail = (card *) NULL;
	card * now = (card *) NULL;
	card * pre = (card *) NULL;

	srand((unsigned) time (NULL));//随机种子
	int number = 0;
	int total = 54;
	int count;
	//一共54张牌
	for(; total > 0; total--){
		count = 1;
		now = head;
		pre = (card *) NULL;
		number = (rand() % total) + 1;		//随机拿出第几张牌

		while(count != number){
			pre = now;
			now = now->next;
			count++;
		}//now指向的就是需要拿出的牌

		if(result == (card *) NULL){		//如果结果链表为空

			result = now;					//结果链表的第一张为now指向的牌
			result_tail = now;				//结果链表的最后一张为now指向的牌
			result->next == (card *) NULL;	//结果链表的最后一直的next指向的为NULL

			if(pre == (card *) NULL){
				head = head->next;
			}		//如果拿出的是第一张牌
					//那么head就应该指向第二章牌
					//取出第一张牌
					//此时pre指向的就是NULL

			if(now->next != (card *) NULL){
				pre->next = now->next;
				now->next = (card *) NULL;
				pre = (card *) NULL;
			}		//如果取出的不是最后一张牌
					//pre指向now->next 此时now就被去掉了
					//防止一张牌取了两次

		} else {							//如果结果链表不为空
			if(pre == (card *) NULL){
				head = head->next;
			}		//如果是第一张牌 同上

			result_tail->next = now;			//结果链表的最后一张指向now
			result_tail = result_tail->next;	//结果链表指向最后一张的指针移动,始终指向最后一张牌
			
			if(now->next != (card *) NULL){		//如果取出的不是最后一张
				if(pre != (card *) NULL)		//如果取出的不是第一张
					pre->next = now->next;		//去掉now指向的牌
				now->next = (card *) NULL;		//置now->next 为NULL
				pre = (card *) NULL;			//置pre为NULL
			}
		}
	}
	return result;
}

void print_card(card * head)
{
	int count = 1;		//第几张
	while(head != (card *) NULL){
		printf("%d\tThe color is %c, the number is %c\n", count++, head->color, head->number);
		head = head->next;
	}					//输出牌
}

int main(int argc, char * argv[])
{
	card * head = (card *) NULL;	//第一张牌
	head = init_card();				//初始化牌
	head = flush_card(head);		//洗牌
	print_card(head);				//输出洗好的牌
	system("pause");
	return 0;
}


不晓得有木有bug。。随意写的。。有bug请提出。。。Thank you....


执行结果如下:



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值