约瑟夫环问题

Cpasted just now: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 #include <stdio.h>
 #include <stdlib.h>
 typedef struct node
 {
    int data;
    struct node *next;
}list,node;

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;
            //构造其它节点
int i=1;
            for (i; i<n; 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
int  i;
                for( i; i<m; 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;
        }


Output:
1
2
3
4
5
6
7
8
9
10
1 2 3 4 5 6 7 8 9 
delete node is: 1
delete node is: 2
delete node is: 3
delete node is: 4
delete node is: 5
delete node is: 6
delete node is: 7
delete node is: 8
last node is: 1

 

posted @ 2013-06-01 22:09  hark.perfe  阅读(128)  评论(0编辑  收藏  举报