Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138129
  • 博文数量: 25
  • 博客积分: 588
  • 博客等级: 中士
  • 技术积分: 292
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-10 18:18
文章分类

全部博文(25)

文章存档

2012年(7)

2011年(18)

我的朋友

分类: C/C++

2012-03-07 14:38:11

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

  3. using namespace std;

  4. struct node_t {
  5.     int data;
  6.     struct node_t *next;
  7. };
  8. struct list_t {
  9.     struct node_t* head;
  10.     struct node_t* tail;
  11.     int size;
  12. };

  13. struct list_t* init_list() {
  14.     struct list_t* list = (struct list_t*)malloc(sizeof(struct list_t));
  15.     list->head = list->tail = NULL;
  16.     list->size = 0;
  17.     return list;
  18. }

  19. int insert_list_end(node_t *node, struct list_t *list) {
  20.     if (list->tail == NULL) {
  21.         list->tail = list->head = node;
  22.     }
  23.     else {
  24.         list->tail->next = node;
  25.         list->tail = node;
  26.     }
  27.     list->size++;
  28.     return 1;
  29. }

  30. int insert_list_head(node_t *node, struct list_t *list) {
  31.     if (list->head == NULL) {
  32.         list->head = list->tail = node;
  33.     }
  34.     else {
  35.         node->next = list->head;
  36.         list->head = node;
  37.     }
  38.     list->size++;
  39.     return 1;
  40. }

  41. struct node_t* remove_list_head(struct list_t *list) {
  42.     node_t *node = NULL;
  43.     if (list->head != NULL) {
  44.         node = list->head;
  45.         if (list->head == list->tail) {
  46.             list->tail = list->head->next;
  47.         }
  48.         list->head = list->head->next;
  49.         list->size--;
  50.     }
  51.     return node;
  52. }
  53. struct node_t *remove_list_end(struct list_t *list) {
  54.     node_t *node = NULL;
  55.     if (list->tail != NULL) {
  56.         node = list->tail;
  57.         if (list->head == list->tail) {
  58.             list->head = list->tail->next;
  59.             list->tail = list->head;
  60.         }
  61.         else {
  62.             node_t *p = list->head;
  63.             while ( p->next != list->tail) {
  64.                 p = p->next;
  65.             }
  66.             p->next = list->tail->next;
  67.             list->tail = p;
  68.         }
  69.         list->size--;
  70.     }
  71.     return node;
  72. }

  73. int remove_list_node(struct node_t *node, struct list_t *list) {
  74.     if (node == list->head ) {
  75.         return (remove_list_head(list) != NULL);
  76.     }
  77.     else if (node == list->tail) {
  78.         return (remove_list_end(list) != NULL);
  79.     }
  80.     struct node_t *p= list->head;
  81.     while (p->next != NULL && p->next != node ) {
  82.         p = p->next;
  83.     }
  84.     if (p->next == NULL) {
  85.         return 0;
  86.     }
  87.     p->next = p->next->next;
  88.     list->size--;
  89.     return 1;
  90. }

  91. void output_list(struct list_t *list) {
  92.     node_t *node = list->head;
  93.     while (node != NULL) {
  94.         cout<<node->data<<" ";
  95.         node = node->next;
  96.     }
  97.     cout<<endl;
  98. }

  99. int main(int argc, char **argv) {
  100.     int a = 10;
  101.     if (argc > 1) {
  102.         a = atoi(argv[1]);
  103.     }
  104.     struct list_t* list = init_list();
  105.     cout<<"before insert:";
  106.     output_list(list);
  107.     for (int i = 0; i < a; i++) {
  108.         node_t *node = new node_t;
  109.         int n = rand() % (a*10);
  110.         node->data = n;
  111.         node->next = NULL;
  112.         insert_list_head(node, list);
  113.         node = new node_t;
  114.         node->data = n;
  115.         node->next = NULL;
  116.         insert_list_end(node, list);
  117.     }
  118.     cout<<"list.size:"<<list->size<<" after insert:";
  119.     output_list(list);
  120.     for (int i = 0; i < a; i++) {
  121.         delete remove_list_head(list);
  122.         delete remove_list_end(list);
  123.     }
  124.     cout<<"list.size:"<<list->size<<" after delete:";
  125.     output_list(list);
  126.     return 0;
  127. }
阅读(1251) | 评论(0) | 转发(0) |
0

上一篇:libevent中的小根堆

下一篇:epoll 简单封装

给主人留下些什么吧!~~