Chinaunix首页 | 论坛 | 博客
  • 博客访问: 441936
  • 博文数量: 89
  • 博客积分: 2713
  • 博客等级: 少校
  • 技术积分: 938
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 21:19
个人简介

为了成为自由自在的人而奋斗!

文章分类

全部博文(89)

文章存档

2016年(5)

2015年(9)

2014年(2)

2013年(10)

2012年(1)

2011年(30)

2010年(32)

分类: C/C++

2011-09-07 11:38:32

oneway_list.h:
  1. #ifndef _ONEWAY_LIST_
  2. #define _ONEWAY_LIST_
  3.     
  4. #include <stdio.h>

  5. #define SUCC 1
  6. #define FAIL 0

  7. #define show_str(fmt, ...)\
  8.     printf("%%Tips: %s %d %s: " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
  9.     
  10. typedef struct node_s {
  11.     int integer;
  12.     struct node_s *next;
  13. }node_t;

  14. typedef struct list_s {
  15.     node_t *first, *last;
  16. }list_t;

  17. void destroy_list(list_t *lst);
  18. int insert_to_list(list_t *lst, int element);
  19. int del_from_list(list_t *lst, int element);
  20. void display_list(list_t *lst);

  21. #endif

oneway_list.c:

 

  1. /* create by fishmwei */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <memory.h>
  5.     
  6. #include "oneway_list.h"

  7. /**
  8.  * destroy_list- 清空链表
  9.  *
  10.  * @lst:要清空链表的指针
  11.  *
  12.  */
  13. void destroy_list(list_t *lst)
  14. {    
  15.     node_t *index, *index_n;
  16.     
  17.     if (lst == NULL) {
  18.         return;
  19.     }
  20.     
  21.     index = lst->first;
  22.     while (index) {
  23.         index_n = index->next;
  24.         free(index);
  25.         index = index_n;
  26.     }
  27.     
  28.     lst->first = NULL;
  29.     lst->last = NULL;
  30.     show_str("destroy list!\n");
  31. }

  32. /**
  33.  * insert_to_list - 往链表插入新元素
  34.  *
  35.  * @lst:插入到的链表的指针
  36.  * @element: 新元素值
  37.  *
  38.  * 成功返回SUCC,失败返回FAIL
  39.  */
  40. int insert_to_list(list_t *lst, int element)
  41. {
  42.     node_t *index, *new_node, *prev;
  43.     
  44.     if (lst == NULL) {
  45.         show_str("bad para\n");
  46.         return FAIL;
  47.     }
  48.     
  49.     new_node = (node_t *)malloc(sizeof(node_t));
  50.     if (new_node == NULL) {
  51.         show_str("alloc memory failed\n");
  52.         return FAIL;
  53.     }
  54.     memset(new_node, 0, sizeof(node_t));
  55.     new_node->integer = element;
  56.         
  57.     index = lst->first;
  58.     if (index == NULL) {
  59.         lst->first = new_node;
  60.         lst->last = new_node;
  61.         show_str("new node become the first node\n");
  62.         return SUCC;
  63.     }
  64.     
  65.     if (index->integer > element) {
  66.         new_node->next = lst->first;
  67.         lst->first = new_node;
  68.         show_str("new node become the first node\n");
  69.         return SUCC;
  70.     } else if (index->integer == element) {
  71.         show_str("repeat with the first node\n");
  72.         free(new_node);
  73.         return SUCC;
  74.     }
  75.     
  76.     prev = index;
  77.     index = index->next;
  78.     while (index) {
  79.         if (index->integer == element) {
  80.             show_str("repeat with other node\n");
  81.             free(new_node);
  82.             return SUCC;
  83.         } else if (index->integer > element) {
  84.             /* 插入之前 */
  85.             new_node->next = index;
  86.             prev->next = new_node;
  87.             show_str("insert before %d\n", index->integer);
  88.             return SUCC;
  89.         } else {
  90.          prev = index;
  91.             index = index->next;
  92.         }
  93.     }
  94.     
  95.     show_str("new node become the last node\n");
  96.     lst->last->next = new_node;
  97.     lst->last = new_node;
  98.     
  99.     return SUCC;
  100. }

  101. /**
  102.  * del_from_list -从链表中删除对应元素的成员
  103.  *
  104.  * @lst:链表指针
  105.  * @element: 要删除的元素
  106.  *
  107.  * 成功返回SUCC,失败返回FAIL
  108.  */
  109. int del_from_list(list_t *lst, int element)
  110. {
  111.     node_t *index, *prev;
  112.     
  113.     if (lst == NULL) {
  114.         show_str("bad para\n");
  115.         return FAIL;
  116.     }
  117.     
  118.     index = lst->first;
  119.     if (index == NULL) {
  120.         show_str("lst is NULL list\n");
  121.         return FAIL;
  122.     }
  123.     
  124.     if (index->integer == element) {
  125.         lst->first = index->next;
  126.         free(index);
  127.         show_str("del the first node\n");
  128.         return SUCC;
  129.     } else if (index->integer > element) {
  130.         show_str("no the elememt, del %d failed\n", element);
  131.         return FAIL;
  132.     }
  133.     
  134.     prev = index;
  135.     index = index->next;
  136.     while (index) {
  137.         if (index->integer == element) {
  138.             prev->next = index->next;
  139.             free(index);
  140.             show_str("del %d SUCCESS\n", element);
  141.             return SUCC;
  142.         } else if (index->integer > element) {
  143.             show_str("no the elememt, del %d failed\n", element);
  144.             return FAIL;
  145.         } else {
  146.          prev = index;
  147.             index = index->next;
  148.         }
  149.     }
  150.     
  151.     show_str("no the elememt, del %d failed\n", element);
  152.     
  153.     return FAIL;
  154. }

  155. /**
  156.  * display_list -显示链表中的所有元素
  157.  *
  158.  * @lst:链表的指针
  159.  *
  160.  */
  161. void display_list(list_t *lst)
  162. {
  163.     node_t *index;
  164.     
  165.     if (lst == NULL) {
  166.         show_str("bad para\n");
  167.         return;
  168.     }
  169.     
  170.     printf("list:\n");

  171.     if (lst->first == NULL) {
  172.         printf("lst is a NULL list\n");
  173.         return;
  174.     }
  175.     
  176.     index = lst->first;
  177.     while (index) {
  178.         printf("%d\t", index->integer);
  179.         index = index->next;
  180.     }
  181.     
  182.     printf("\n");
  183. }

  184. /* 测试主程序 */
  185. int main(void)
  186. {
  187.     int op, num;
  188.     list_t test_lst;
  189.     
  190.     test_lst.first = NULL;
  191.     test_lst.last = NULL;
  192.     
  193.     while (1) {
  194.         printf("\nop: 1.add 2.del 3.quit\n");
  195.         scanf("%d", &op);
  196.         switch (op) {
  197.         case 1:
  198.             printf("input int to add\n");
  199.             scanf("%d", &num);
  200.             insert_to_list(&test_lst, num);
  201.             display_list(&test_lst);
  202.             break;
  203.         case 2:
  204.             printf("input int to del\n");
  205.             scanf("%d", &num);
  206.             del_from_list(&test_lst, num);
  207.             display_list(&test_lst);
  208.             break;
  209.         case 3:
  210.             goto quit;
  211.         default:
  212.             show_str("input error\n");
  213.             break;
  214.         }
  215.     }
  216.     
  217. quit:
  218.     destroy_list(&test_lst);

  219.     return 0;
  220. }

 

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