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

不卑不亢

文章分类

全部博文(17)

文章存档

2016年(1)

2015年(13)

2014年(3)

分类: C/C++

2015-03-21 14:24:17


点击(此处)折叠或打开

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

  3. #define QUEUE_DEBUG 1

  4. typedef struct _qnode_ {
  5.     int data;
  6.     struct _qnode_ *next;
  7. }qnode_t;

  8. typedef struct _queue_ {
  9.     qnode_t *head;
  10.     qnode_t *tail;
  11.     int clen;
  12.     int tlen;
  13. }queue_t;

  14. queue_t *queue_init(int len);
  15. qnode_t *create_qnode(int value);
  16. int queue_destory(queue_t *queue);
  17. int queue_enqueue(queue_t *queue, int value);
  18. int queue_dequeue(queue_t *queue, int *value);
  19. int queue_isfull(queue_t *queue);
  20. int queue_isempty(queue_t *queue);
  21. #if QUEUE_DEBUG
  22. void _queue_debug_(queue_t *queue);
  23. #endif

  24. int main()
  25. {
  26.     int value = 100;
  27.     queue_t *queue = NULL;

  28.     queue = queue_init(10);

  29.     puts("Enqueue:");
  30.     while (0 == queue_enqueue(queue, value)) {
  31.         printf("%5d", value);
  32.         value += 100;
  33.     }
  34.     putchar('\n');

  35. #if QUEUE_DEBUG
  36.     _queue_debug_(queue);
  37. #endif

  38.     puts("Dequeue:");
  39.     while (0 == queue_dequeue(queue, &value))
  40.         printf("%5d", value);
  41.     putchar('\n');
  42.     
  43. #if QUEUE_DEBUG
  44.     _queue_debug_(queue);
  45. #endif

  46.     queue_destory(queue);

  47.     return 0;
  48. }

  49. queue_t *queue_init(int len)
  50. {
  51.     queue_t *queue = NULL;

  52.     queue = (queue_t *)malloc(sizeof(queue_t));
  53.     queue->tlen = len;
  54.     queue->clen = 0;
  55.     queue->head = create_qnode(0);
  56.     queue->tail = queue->head;

  57.     return queue;
  58. }

  59. int queue_destory(queue_t *queue)
  60. {
  61.     qnode_t *p = queue->head;
  62.     qnode_t *tmp = NULL;

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

  71. qnode_t *create_qnode(int value)
  72. {
  73.     qnode_t *node = NULL;

  74.     node = (qnode_t *)malloc(sizeof(qnode_t));
  75.     node->data = value;
  76.     node->next = NULL;

  77.     return node;
  78. }

  79. int queue_enqueue(queue_t *queue, int value)
  80. {
  81.     if (queue_isfull(queue))
  82.         return -1;

  83.     qnode_t *new = NULL;
  84.     new = create_qnode(value);
  85.     queue->tail->next = new;
  86.     queue->tail = new;

  87.     queue->clen++;

  88.     return 0;
  89. }

  90. int queue_dequeue(queue_t *queue, int *value)
  91. {
  92.     qnode_t *p = queue->head;

  93.     if (queue_isempty(queue))
  94.         return -1;

  95.     queue->head = p->next;
  96.     free(p);

  97.     *value = queue->head->data;
  98.     queue->clen--;

  99.     return 0;
  100. }

  101. int queue_isfull(queue_t *queue)
  102. {
  103.     if (queue->clen >= queue->tlen)
  104.         return 1;
  105.     return 0;
  106. }

  107. int queue_isempty(queue_t *queue)
  108. {
  109.     if (queue->head == queue->tail)
  110.         return 1;
  111.     return 0;
  112. }

  113. #if QUEUE_DEBUG
  114. void _queue_debug_(queue_t *queue)
  115. {
  116.     qnode_t *p = queue->head->next;
  117.     puts("------------------ QUEUE_DEBUG ------------------");
  118.     printf("tlen: %d\tclen: %d\n", queue->tlen, queue->clen);
  119.     while (NULL != p) {
  120.         printf("%5d", p->data);
  121.         p = p->next;
  122.     }
  123.     puts("\n-------------------------------------------------");
  124. }
  125. #endif

阅读(1105) | 评论(0) | 转发(0) |
0

上一篇:循环队列

下一篇:顺序栈

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