Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236687
  • 博文数量: 65
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 417
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-05 15:12
个人简介

路漫漫其修远兮,吾将上下而求索

文章分类

全部博文(65)

文章存档

2016年(1)

2015年(12)

2014年(34)

2013年(16)

2012年(2)

我的朋友

分类: LINUX

2015-03-03 11:23:39


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define SWAP(datatype, x, y) {datatype t = x; x = y; y = t;}
  4. typedef char datatype;

  5. struct List
  6. {
  7.     datatype data;
  8.     struct List *next;
  9. };

  10. struct List *init_list(struct List *list)
  11. {
  12.     list = (struct List *)malloc(sizeof(struct List));
  13.     return list;
  14. }

  15. void head_insert_node(struct List *prev, struct List *node)
  16. {
  17.     node->next = prev->next;
  18.     prev->next = node;
  19. }

  20. struct List * tail_add_list(struct List *tail, datatype value)
  21. {
  22.     struct List *node = (struct List *)malloc(sizeof(struct List));
  23.     node->data = value;
  24.     node->next = NULL;

  25.     tail->next = node;
  26.     tail = tail->next;

  27.     return tail;
  28. }

  29. void tail_add_node(struct List *head, datatype value)
  30. {
  31.     struct List *p = head;
  32.     struct List *node = (struct List *)malloc(sizeof(struct List));
  33.     node->data = value;
  34.     node->next = NULL;

  35.     while (p->next != NULL)
  36.     {
  37.         p = p->next;
  38.     }
  39.     
  40.     p->next = node;
  41. }

  42. void head_add_list(struct List *head, datatype value)
  43. {
  44.     struct List *node = (struct List *)malloc(sizeof(struct List));
  45.     node->data = value;
  46.     node->next = NULL;

  47.     head_insert_node(head, node);
  48. }

  49. void destroy_list(struct List *head)
  50. {
  51.     struct List *p = head;
  52.     struct List *q = NULL;

  53.     while (p->next != NULL)
  54.     {
  55.         q = p->next;
  56.         p->next = q->next;
  57.         free(q);
  58.     }
  59. }

  60. struct List *find_list(struct List *head, struct List **prev, datatype value)
  61. {
  62.     struct List *p = head;

  63.     while (p->next != NULL)
  64.     {
  65.         *prev = p;
  66.         p = p->next;
  67.         if (p->data == value)
  68.         {
  69.             return p;
  70.         }
  71.     }
  72.     return NULL;
  73. }

  74. int delete_list(struct List *head, datatype value)
  75. {
  76.     struct List *prev = NULL;
  77.     struct List *p = find_list(head, &prev, value);
  78.     static int count = 0;

  79.     while (p != NULL)
  80.     {
  81.         struct List *l = prev;
  82.         prev->next = p->next;    
  83.         p->next = NULL;
  84.         free(p);
  85.         count++;
  86.         p = find_list(l, &prev, value);
  87.     }
  88.     return count;
  89. }

  90. struct List *list_sort(struct List *head)
  91. {
  92.     struct List *list = head;
  93.     struct List *p = head->next;
  94.     struct List *q = p;
  95.     datatype min;

  96.     while (p != NULL && p->next != NULL)
  97.     {
  98.         min = p->data;
  99.         q = p->next;
  100.         while (q != NULL)
  101.         {
  102.             if (min > q->data)
  103.             {
  104.                 min = q->data;
  105.                 SWAP(datatype, p->data, q->data)
  106.             }
  107.             q = q->next;
  108.         }
  109.         p = p->next;
  110.     }

  111.     return list;
  112. }

  113. void order_insert_list(struct List *head, datatype value)
  114. {
  115.     struct List *p = head;
  116.     struct List *prev = p;
  117.     struct List *node = (struct List *)malloc(sizeof(struct List));
  118.     node->data = value;
  119.     node->next = NULL;

  120.     while (p->next != NULL)
  121.     {
  122.         prev = p;
  123.         p = p->next;
  124.         if (value < p->data)
  125.         {
  126.             node->next = prev->next;
  127.             prev->next = node;
  128.             return ;
  129.         }
  130.     }
  131.     p->next = node;
  132.     return ;
  133. }

  134. void modify_list(struct List *head, datatype src_value, datatype dst_value)
  135. {
  136.     struct List *p = head;

  137.     while (p->next != NULL)
  138.     {
  139.         p = p->next;
  140.         if (p->data == src_value)
  141.         {
  142.             p->data = dst_value;
  143.         }
  144.     }
  145. }

  146. struct List *print_list(struct List *head)
  147. {
  148.     struct List *p = head;

  149.     while (p->next != NULL)
  150.     {
  151.         p = p->next;
  152.         printf("%c ", p->data);
  153.     }    
  154.     putchar('\n');
  155. }

  156. int main()
  157. {
  158.     struct List *head = init_list(head);
  159.     struct List *prev = NULL;
  160.     struct List *tail = head;
  161.     datatype value;
  162. //按照输入到序生成链表(头插法)
  163. /*
  164.     while ((value = getchar()) != '\n')
  165.     {
  166.         head_add_list(head, value);    
  167.     }
  168. */
  169. //按输入顺序生成链表(尾插法)
  170.     while ((value = getchar()) != '\n')
  171.     {
  172.         tail = tail_add_list(tail, value);    
  173.     }

  174.     printf("原始数据:\n");
  175.     print_list(head);
  176. //查找操作
  177.     struct List *p = find_list(head, &prev, 'x');

  178.     if(p != NULL)
  179.     {
  180.         printf("node has founded\n");
  181.     }
  182.     else
  183.     {
  184.         printf("no node find\n");
  185.     }
  186. //删除操作
  187.     int del_num = delete_list(head, 'x');
  188.     printf("删除操作后:\n");
  189.     printf("del_num = %d\n", del_num);
  190.     print_list(head);
  191. //前插节点擦作
  192. //    head_add_list(head, 'o');
  193. //尾插节点擦作
  194.     tail_add_node(head, 'o');
  195.     printf("尾部添加节点:\n");
  196.     print_list(head);

  197. //修改操作
  198.     modify_list(head, 'd', 'D');
  199.     printf("修改操作后:\n");
  200.     print_list(head);
  201.     
  202. //链表排序    
  203.     head = list_sort(head);
  204.     printf("链表排序后:\n");
  205.     print_list(head);

  206.     order_insert_list(head, 'x');
  207.     printf("顺序添加节点后:\n");
  208.     print_list(head);

  209.     printf("destroy!\n");
  210.     destroy_list(head);
  211.     print_list(head);

  212.     return 0;
  213. }

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