#include <corecrt_malloc.h>
#include <stdio.h>
constexpr auto OK = 1;
constexpr auto Error = 0;
typedef int Status;
typedef int ElemType;
//定义双链表结构体
typedef struct Temp
{
ElemType SequenceNumber; //顺序号
ElemType Data; //牌号
struct Temp* Next;
}CircularLinkedList;
//初始化循环链表
//n:牌数
Status InitLinkedList(CircularLinkedList*& list,int n )
{
if (n < 1)
return Error;
list = (CircularLinkedList*)malloc(sizeof(Temp));
if (!list)
return Error;
CircularLinkedList* pNode = NULL, * pHead = NULL;
for (int i = 1;i <=n;i++)
{
if (pNode)
{
CircularLinkedList* newlist = NULL;
newlist = (CircularLinkedList*)malloc(sizeof(Temp));
if (!newlist)
return Error;
pNode->Next = newlist;
//newlist->SequenceNumber = i;
newlist->Data = 0;
pNode = newlist;
//free(newlist);
}
else {
pNode = list;
}
if (pHead == NULL)
{
pHead = list;
}
}
pNode->Next = pHead;
return OK;
}
//魔术操作
Status Licensing(CircularLinkedList* poker)
{
poker->Data = 1;
int pokerNum = 2;
CircularLinkedList* pokerItem = poker;
while (1)
{
for (int j=0;j < pokerNum;j++)
{
pokerItem = pokerItem->Next;
if (pokerItem->Data!=0)
{
//pokerItem = pokerItem->Next;
j--;
}
}
pokerItem->Data = pokerNum;
pokerNum++;
if (pokerNum == 14)
{
break;
}
}
return OK;
}
//销毁链表
Status Distroy(CircularLinkedList* poker,int n)
{
CircularLinkedList* NextOne = poker,*del;
for (int i = 0;i < n;i++)
{
del = NextOne;
NextOne = del->Next;
free(del);
}
return OK;
}
//主函数
void PokerOperate()
{
CircularLinkedList* list = NULL;
InitLinkedList(list, 13);
Licensing(list);
for (int i = 0;i < 13;i++)
{
printf("黑桃%d", list->Data);
list = list->Next;
}
Distroy(list,13);
}
开始是0,结尾是1。