Chinaunix首页 | 论坛 | 博客
  • 博客访问: 298415
  • 博文数量: 134
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:02
文章分类

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-08-01 14:16:15

原文地址:文件操作_链表_练习_0309 作者:丫叩酱


点击(此处)折叠或打开

  1. //Josephus.c

  2. //链表实现约瑟夫环问题
  3. //先建立一个单链表,在其最后,tail->next = head;

  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define PERSONS 10
  7. #define COUNTER 4

  8. typedef struct node
  9. {
  10.     int data;
  11.     struct node *next;
  12. }node_t;

  13. node_t *create(node_t *head)
  14. {
  15.     int i;
  16.     node_t *cur, *tail;

  17.     for (i = 0; i < PERSONS; i++)
  18.     {
  19.         cur = malloc(sizeof(node_t));
  20.         if(cur == NULL)
  21.         {
  22.             printf("malloc fail\n");
  23.             exit(1);
  24.         }

  25.         cur->data = i + 1;
  26.         cur->next = NULL;
  27.     
  28.         if(head == NULL)
  29.             head =tail = cur;
  30.         else
  31.         {
  32.             tail->next = cur;
  33.             tail = cur;
  34.         }
  35.     }

  36.     tail->next = head;
  37.     return head;
  38. }

  39. void Josephus(node_t *head)
  40. {
  41.     node_t *pre, *cur, *del;
  42.     int remain = PERSONS, step= 1;

  43.     pre = cur = head;
  44.     while(remain > 1 )
  45.     {
  46.         if(step != COUNTER)
  47.         {
  48.             step ++;
  49.             pre = cur;
  50.             cur = cur->next;
  51.         }
  52.         else
  53.         {
  54.             step = 1;
  55.             printf("killed %d\n", cur->data);

  56.             del = cur;
  57.             pre->next = cur = cur->next;
  58.             free(del);
  59.             remain--;
  60.         }
  61.     }

  62.     printf("\nsurvived %d\n", cur->data);
  63.     free(cur);
  64. }

  65. void show(node_t *head)
  66. {
  67.     node_t *cur;

  68.     for(cur = head; cur->next != head; cur = cur->next)
  69.     {
  70.         printf("-%d-", cur->data);
  71.     }
  72.     printf("-%d-", cur->data);
  73.     putchar('\n');
  74. }

  75. void myfree(node_t *head)
  76. {
  77.     node_t *cur, *nextp;

  78.     for(cur = head; cur->next != head; cur = nextp)
  79.     {
  80.         nextp = cur->next;
  81.         free(cur);
  82.     }
  83.     free(cur);
  84. }

  85. int main(int argc, const char *argv[])
  86. {
  87.     node_t *head = NULL;
  88.     
  89.     head = create(head);
  90.     show(head);
  91.     myfree(head);

  92.     head = NULL;
  93.     head = create(head);
  94.     Josephus(head);
  95.     return 0;
  96. }

点击(此处)折叠或打开

  1. //Josephus.c

  2. //建第一个节点时,将head->next = head; tail = head;
  3. //再将新的节点插入到tail后,tail->next = cur; cur->next = head; tail = cur;
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. #define PERSONS 10
  7. #define COUNTER 4

  8. typedef struct node
  9. {
  10.     int data;
  11.     struct node *next;
  12. }node_t;

  13. node_t *create(node_t *head)
  14. {
  15.     node_t *cur, *tail;
  16.     int i;

  17.     for(i = 0; i < PERSONS; i++)
  18.     {
  19.         cur = malloc(sizeof(node_t));

  20.         if(cur == NULL)
  21.         {
  22.             printf("malloc fail\n");
  23.             exit(1);
  24.         }

  25.         cur->data = i + 1;
  26.         if(head == NULL)
  27.         {
  28.            head = cur;
  29.            tail = head;
  30.            head->next = head;
  31.         }
  32.         else
  33.         {
  34.             tail->next = cur;
  35.             tail = cur;
  36.         }
  37.         cur->next = head;
  38.     }

  39.     return head;
  40. }

  41. void Josephus2(node_t *head)
  42. {
  43.     node_t *pre, *cur, *del;
  44.     int remain = PERSONS, step = 1;

  45.     pre = cur = head;
  46.     while(remain > 1)
  47.     {
  48.         if(step != COUNTER)
  49.         {
  50.             step ++;
  51.             pre = cur;
  52.             cur = cur->next;
  53.         }
  54.         else
  55.         {
  56.             remain --;
  57.             step = 1;
  58.             printf("kill %d\n", cur->data);

  59.             del = cur;
  60.             pre->next = cur = cur->next;
  61.             free(del);
  62.         }
  63.     }
  64.     printf("\nsurvived %d\n", cur->data);
  65.     free(cur);
  66. }

  67. void show(node_t *head)
  68. {
  69.    node_t *cur;
  70.    for(cur = head; cur->next != head; cur = cur->next)
  71.    {
  72.         printf("-%d-", cur->data);
  73.    }
  74.    printf("-%d-", cur->data);
  75.    putchar('\n');
  76. }

  77. void myfree(node_t *head)
  78. {
  79.     node_t *cur, *nextp;

  80.     for(cur = head; cur->next != head; cur = nextp)
  81.     {
  82.         nextp = cur->next;
  83.         free(cur);
  84.     }
  85.     free(cur);
  86. }
  87. int main(void)
  88. {
  89.     node_t *head = NULL;

  90.     head = create(head);
  91.     show(head);
  92.     myfree(head);

  93.     head = NULL;
  94.     head = create(head);
  95.     Josephus2(head);
  96.     return 0;
  97. }

点击(此处)折叠或打开

  1. //is_ring_link.c

  2. //判断一个链表是否有环

  3. #include <stdio.h>
  4. #include <stdlib.h>

  5. #define NUM 10

  6. typedef struct node
  7. {
  8.     int data;
  9.     struct node *next;
  10. }node_t;

  11. node_t *create(node_t *head)
  12. {
  13.     node_t *cur, *tail;
  14.     int i;

  15.     for(i = 0; i < NUM; i++)
  16.     {
  17.         cur = malloc(sizeof(node_t));
  18.         if(cur == NULL)
  19.         {
  20.             printf("malloc fail\n");
  21.             exit(1);
  22.         }

  23.         cur->data = i + 1;

  24.         if(head == NULL)
  25.             head = tail = cur;
  26.         else
  27.         {
  28.             tail->next = cur;
  29.             tail = cur;
  30.         }
  31.         cur->next = NULL;
  32.     }
  33.     cur->next = head;

  34.     return head;
  35. }

  36. void is_ring(node_t *head)
  37. {
  38.     node_t *pre, *cur;

  39.     pre = cur = head;
  40.     while(1)
  41.     {
  42.         pre = pre->next;
  43.         cur = cur->next->next;

  44.         if(pre == NULL || cur == NULL)
  45.         {
  46.             printf("no ring\n");
  47.             break;
  48.         }
  49.         if(pre == cur)
  50.         {
  51.             printf("is ring\n");
  52.             break;
  53.         }
  54.     }

  55. }

  56. void show(node_t *head)
  57. {
  58.     node_t *cur;
  59.     for(cur = head; cur->next != head; cur = cur->next)
  60.     {
  61.         printf("-%d-", cur->data);
  62.     }
  63.     printf("-%d-\n", cur->data);
  64. }

  65. void myfree(node_t *head)
  66. {
  67.     node_t *cur, *nextp;

  68.     for(cur = head; cur->next != head; cur = nextp)
  69.     {
  70.         nextp = cur->next;
  71.         free(cur);
  72.     }
  73.     free(cur);
  74. }
  75. int main(int argc, const char *argv[])
  76. {
  77.     node_t *head = NULL;

  78.     head = create(head);
  79.     is_ring(head);
  80.     show(head);
  81.     myfree(head);
  82.     return 0;
  83. }

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