首页 > 编程语言 > 详细

纸牌翻牌排序

时间:2020-07-18 22:44:43      阅读:67      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <stdlib.h>


typedef struct node
{
    int name;
    int number;
    struct node *next;
}person;

//初始化循环列表
person * initLink(int n)
{
    person *head = (person *)malloc(sizeof(person));
    head->number = 1;
    head->next = NULL;
    
    person *cycle = head;
    for(int i=2;i<=n;i++)
    {
        person *body = (person *)malloc(sizeof(person));
        body->number = i;
        body->next = NULL;
        cycle->next = body;
        cycle = cycle->next;
    }
    cycle->next = head;//收尾相连
    
    return head;
}

//打印循环列表数据
void printf_list(person *s)
{
    person *p = s->next;
    
    while(p->number !=1)
    {
        p = p->next;
    }
    printf("-> [name:%d,number:%d]\r\n",p->name,p->number);
    p = p->next;
    
    while(p!= s)
    {
        printf("-> [name:%d,number:%d]\r\n",p->name,p->number);
        p = p->next;
    }    
}

//间隔k个,总数m
void write_list(person *s ,int k,int m)
{
    person *p = s;
    
    for(int i=1;i<=m;i++)
    {
        
        for(int j=0;j<k;j++)
        {
            while(p->name != 0)
            {
                p = p->next;
            }
            p=p->next;
        }
        while(p->name != 0)
        {
            p = p->next;
        }
        
        p->name = i;
    }
}


int main(int argc ,char* argv)
{
    //初始化 10 个元素的列表
    person *head = initLink(10);
    //间隔1张,总数为10
    write_list(head,1,10);
    //输出顺序
    printf_list(head);
    

    return 0;
}

 

纸牌翻牌排序

原文:https://www.cnblogs.com/doitjust/p/13337061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!