Chinaunix首页 | 论坛 | 博客
  • 博客访问: 478578
  • 博文数量: 285
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 629
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-14 17:53
个人简介

相信自己,快乐每一天

文章分类

全部博文(285)

分类: 架构设计与优化

2013-11-01 15:02:55

原文地址:循环链表之约瑟夫环 作者:scq2099yt

一、循环链表
        循环链表跟普通链表相比,特点在于表中最后一个节点的指针域指向头结点,整个链表形成一个环,因此,循环链表判空条件(head == head->next)也就不同于普通链表了(NULL == head->next),以不带头节点的单循环链表为例,其拓扑图如下图所示:

图1 带头节点的单循环链表

二、约瑟夫环
        约瑟夫环:已经n个人(以编号1,2,3,...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数(从1开始报数),数到m的人出列,他的下一个人又从1开始报数,数到m的人出列,如此这般重复下去,直到圆桌周围的人全部出列。
        约瑟夫环问题非常适合于循环链表的场景,求约瑟夫环中最后一个出列的数字,创建一个不带头节点的循环链表,确定第一个报数人的位置,每次走m步删除一个节点,最后剩下的就是所求,其算法实现如下:
        #include
        #include
        typedef struct node
        {
            int data;
            struct node *next;
        }list;

        list *create_list(int a[], int n)
        {
            if ((NULL ==a) || (n <= 0))
            {
                return NULL;
            }

            list *head=NULL, *p=NULL, *q=NULL;
            //构造第一个结点
            head = (node *)malloc(sizeof(node));
            head->data = a[0];
            head->next = NULL;
            q = head;
            //构造其它节点
            for (int i=1; i             {
                //构造节点
                p = (node *)malloc(sizeof(node));
                p->data = a[i];
                p->next = NULL;
                //将节点加入链表
                q->next = p;
                q = q->next;
            }
            q->next = head;

            return head;
        }

        void show_list(list *head)
        {
            if (NULL != head)
            {
                list *p=head;
                printf("%d ", p->data);
                while (p->next!= head)
                {   
                    p=p->next;
                    printf("%d ", p->data);
                }
                printf("\n");
            }
        }
        void delete_node(list *head, int k, int m)
        {
            if ((NULL == head) || (k < 0))
            {
                return;
            }

            list *p=head, *q=NULL;
            //算约瑟夫环大小
            int len = 1;
            while (p->next != head)
            {
                ++len;
                p = p->next;
            }
            p = head;

            //找报数的起点
            while (k-- > 1)
            {
                p = p->next;
            }
            q = p;

            while (len > 1)
            {
                //开始数m
                for(int i=1; i                 {
                    p = q;
                    printf("%d\n", p->data);
                    q = q->next;
                    printf("%d\n", q->data);
                }
                p->next = q->next;//删除节点 
                printf("delete node is: %d\n", q->data); 
                q = p->next;

                --len;
            }

            printf("last node is: %d\n", p->data); 
        }

        int main()
        {
            int a[10] = {1,2,3,4,5,6,7,8,9};
            list *head = NULL;
            head = create_list(a,9);
            show_list(head);
            delete_node(head, 1, 2);

            return 0;
        }




阅读(366) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~