魔术师

线性表应用:魔术师发牌与拉丁(Latin)方阵(循环链表)

人走茶凉 提交于 2020-03-14 12:40:30
题目描述: 有黑桃1到13,13张牌,成某种顺序,魔术师可以从1开始数 ,数1,背面朝上的13张牌第一张就是1,然后放到桌面上,然后从1开始数,把第一张放在所有牌下面,数到2,翻开,就是2,再放到桌子上,以此此类推 #include<stdio.h> #include<stdlib.h> #define LEN sizeof(LinkList) #define cardNum 13 typedef struct node { int data; struct node *next; }LinkList; LinkList *creatList() { LinkList *head = NULL; LinkList *s,*r; r = head; for(int i=1; i <= cardNum; i++) { s = (LinkList *)malloc(LEN); s->data = 0; if(head == NULL) { head = s; } else { r->next = s; } r = s; } r->next = head; return head; } void MagicCard(LinkList *head) { int i; int countNum = 2; LinkList *p; p = head; p->data = 1; while(1)

魔术师发牌问题 (C语言实现) ------- 算法笔记008

谁都会走 提交于 2020-01-30 06:04:13
问题背景 实质还是循环双向链表的应用,多了一个判断循环的步骤 实现代码 # include <stdio.h> # include <stdlib.h> # define numPokers 13 typedef struct node { int data ; struct node * next ; } Node , * List ; //定义一组牌A-K ;A用1表示,对应的J,Q,K也是相应的数字表示 int pokers [ 13 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 } ; //创建一组空牌;并未赋值 List createPoker ( List head ) { int num = 13 ; int i ; List p = head ; for ( i = 0 ; i < num ; i ++ ) { List new = ( List ) malloc ( sizeof ( Node ) ) ; new -> data = 0 ; //每张牌初始赋值为0 p -> next = new ; p = new ; } p -> next = head -> next ; p = head -> next ; free ( head ) ; return p ; } int main (