魔术师发牌问题-循环链表

/*
先建立循环表,初始为零
把1---13给循环表元素
*/
#include <malloc.h>
#include <iostream>
using namespace std;
typedef  struct tyust
{
	int data;
	struct tyust *next;
}Node,*link;

//返回首地址
link Init()
{
	int i=0;
	Node *head,*p,*q;  //创建头节点
	head=(link)malloc(sizeof(Node));
	q=head;
	while(i<13)
	{
		p=(link)malloc(sizeof(Node));
		p->data=0;//初始值0
		head->next=p;
		head=p;
		i++;
	}
	p->next=q->next;
	return q->next;
}
void main()
{
	int j=0,i=2;
	link p,q;
	p=Init();
	q=p;
	p->data=1;
	
	while(i<14)
	{
		j=0;
		while(j<i)
		{
			p=p->next;
			j++;
			if(p->data)j--;	
			

		}
		p->data=i;	
		i++;
		
	}
	for(int num=0;num<13;num++)
	{		
		cout<<"黑桃:"<<q->data<<endl;
		q=q->next;
	}
}

猜你喜欢

转载自blog.csdn.net/shuiyihang0981/article/details/82255446