Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68596
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 154
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-12 22:56
个人简介

不卑不亢

文章分类

全部博文(17)

文章存档

2016年(1)

2015年(13)

2014年(3)

分类: C/C++

2015-03-21 14:17:25


点击(此处)折叠或打开

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

  3. typedef struct _dlink_node_ {
  4.     int data;
  5.     struct _dlink_node_ *pre;
  6.     struct _dlink_node_ *next;
  7. }dlinknode_st;

  8. dlinknode_st *dlist_init();
  9. int dlist_free(dlinknode_st *h);
  10. int head_insert(dlinknode_st *h, int value);
  11. int dlist_show(dlinknode_st *h);
  12. int dlist_delete(dlinknode_st *h, int obj);
  13. dlinknode_st *dlist_locate(dlinknode_st *h, int obj);
  14. int dlist_search(dlinknode_st *h, int obj);
  15. int dlist_modify(dlinknode_st *h, int old, int new);

  16. int main()
  17. {
  18.     dlinknode_st *list = NULL;
  19.     int obj;

  20.     if (NULL == (list = dlist_init()))
  21.         return -1;

  22.     puts("listshow:");
  23.     head_insert(list, 100);
  24.     head_insert(list, 200);
  25.     head_insert(list, 300);
  26.     head_insert(list, 200);
  27.     head_insert(list, 300);
  28.     head_insert(list, 200);
  29.     head_insert(list, 300);
  30.     head_insert(list, 200);
  31.     dlist_show(list);

  32.     puts("delete:");
  33.     dlist_delete(list, 200);
  34.     dlist_show(list);
  35.     

  36.     puts("modify:");
  37.     dlist_modify(list, 100, 1000);
  38.     dlist_show(list);

  39.     obj = 100;
  40.     puts("search:");
  41.     if (dlist_search(list, obj))
  42.         printf("%d is exist!\n", obj);
  43.     else
  44.         printf("%d is not exist\n", obj);

  45.     dlist_free(list);

  46.     return 0;
  47. }

  48. dlinknode_st *dlist_init()
  49. {
  50.     dlinknode_st *h = NULL;

  51.     if (NULL == (h = (dlinknode_st *)malloc(sizeof(dlinknode_st)))) {
  52.         puts("fail to create!");
  53.         return NULL;
  54.     }

  55.     h->data = 0;
  56.     h->next = NULL;
  57.     h->pre = NULL;

  58.     return h;
  59. }

  60. int dlist_free(dlinknode_st *h)
  61. {
  62.     dlinknode_st *p = h, *tmp = NULL;

  63.     while (NULL != p) {
  64.         tmp = p;
  65.         p = p->next;
  66.         free(tmp);
  67.         tmp = NULL;
  68.     }

  69.     return 0;
  70. }

  71. int head_insert(dlinknode_st *h, int value)
  72. {
  73.     dlinknode_st *new = NULL;

  74.     if (NULL == (new = (dlinknode_st *)malloc(sizeof(dlinknode_st)))) {
  75.         puts("fail to create!");
  76.         return -1;
  77.     }

  78.     new->data = value;

  79.     new->next = h->next;
  80.     if (NULL != new->next)
  81.         new->next->pre = new;
  82.     new->pre = h;
  83.     h->next = new;
  84.     
  85.     return 0;
  86. }

  87. int dlist_show(dlinknode_st *h)
  88. {
  89.     dlinknode_st *p = h;

  90.     while (NULL != p->next) {
  91.         printf("%5d", p->next->data);
  92.         p = p->next;
  93.     }

  94.     putchar(10);

  95.     while (NULL != p->pre) {
  96.         printf("%5d", p->data);
  97.         p = p->pre;
  98.     }

  99.     putchar('\n');

  100.     return 0;
  101. }

  102. int dlist_delete(dlinknode_st *h, int obj)
  103. {
  104.     dlinknode_st *p = h, *tmp = NULL;

  105.     while (NULL != p->next) {
  106.         if (p->next->data == obj) {
  107.             tmp = p->next;
  108.             p->next = tmp->next;
  109.             tmp->next->pre = p;
  110.             free(tmp);
  111.             tmp = NULL;
  112.         } else
  113.             p = p->next;
  114.     }

  115.     return 0;
  116. }

  117. dlinknode_st *dlist_locate(dlinknode_st *h, int obj)
  118. {
  119.     dlinknode_st *p = h;

  120.     while (NULL != p->next && p->next->data != obj)
  121.         p = p->next;

  122.     if (NULL == p->next)
  123.         return NULL;
  124.     
  125.     return p;
  126. }

  127. int dlist_search(dlinknode_st *h, int obj)
  128. {
  129.     if (NULL == dlist_locate(h, obj))
  130.         return 0;

  131.     return 1;
  132. }

  133. int dlist_modify(dlinknode_st *h, int old, int new)
  134. {
  135.     dlinknode_st *p = h;

  136.     while (NULL != p->next) {
  137.         if (p->next->data == old)
  138.             p->next->data = new;
  139.         
  140.         p = p->next;
  141.     }

  142.     return 0;
  143. }

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