队列的简单操作

来源:互联网 发布:手机淘宝改好评链接 编辑:程序博客网 时间:2024/05/22 06:30

      队列,是先进先出(FIFO)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。
queue.h

Code:
  1. #ifndef _QUEUE_H   
  2. #define _QUEUE_H   
  3. #define Type int    
  4.   
  5. #ifdef _MSC_VER   
  6.   
  7. #define bool int   
  8. #define true 1   
  9. #define false 0   
  10.   
  11. #else   
  12.   
  13. #include <stdbool.h>   
  14. #include <stdint.h>   
  15.   
  16. #endif   
  17.   
  18. struct queue;   
  19.   
  20. struct queue *init_queue();   
  21. void destory_queue(struct queue *q);   
  22. void clear_queue(struct queue *q);   
  23.   
  24. bool en_queue(struct queue *q, Type data);   
  25. bool de_queue(struct queue *q);   
  26.   
  27. Type gethead(struct queue *q);   
  28.   
  29. bool queue_empty(struct queue *q);   
  30. int queue_size(struct queue *q);   
  31.   
  32. void print_queue(struct queue *q);   
  33.   
  34. #endif  

queue.c

Code:
  1. #include "queue.h"   
  2. #include <stdio.h>   
  3. #include <stdlib.h>   
  4. #include <assert.h>   
  5.   
  6. struct queue_node {   
  7.     Type data;   
  8.     struct queue_node  *next;   
  9. };   
  10.   
  11. struct queue {   
  12.     struct queue_node *front;   
  13.     struct queue_node *rear;   
  14. };   
  15.   
  16. struct queue *init_queue()   
  17. {   
  18.     struct queue *q = (struct queue *)malloc(sizeof(struct queue));   
  19.     if (q == NULL)   
  20.         return NULL;   
  21.     q->front = q->rear = (struct queue_node *)malloc(sizeof(struct queue_node));   
  22.     if (q->front == NULL)   
  23.         return NULL;   
  24.     q->front->next = NULL;   
  25.     return q;   
  26. }   
  27.   
  28. void destory_queue(struct queue *q)   
  29. {   
  30.     assert(q != NULL);   
  31.     while (q->front != NULL) {/*also free head node*/  
  32.         q->rear = q->front->next;   
  33.         free (q->front);   
  34.         q->front = q->rear;   
  35.     }   
  36.     free(q);   
  37.     q = NULL;   
  38. }   
  39.   
  40. void clear_queue(struct queue *q)   
  41. {   
  42.     assert(q != NULL);   
  43.     struct queue_node *h, *p;   
  44.     h = q->front->next;   
  45.     q->rear = q->front;   
  46.     q->front->next = NULL;     
  47.     while (h != NULL) {   
  48.         p = h;   
  49.         h = h->next;   
  50.         free(p);   
  51.     }          
  52. }   
  53.   
  54. bool en_queue(struct queue *q, Type data)   
  55. {   
  56.     assert(q != NULL);   
  57.     struct queue_node *p = (struct queue_node *)malloc(sizeof(struct queue_node));   
  58.     if (p == NULL)   
  59.         return false;   
  60.     p->data = data;   
  61.     p->next = NULL;   
  62.     q->rear->next = p;   
  63.     q->rear = p;   
  64.     return true;   
  65. }   
  66.   
  67. bool de_queue(struct queue *q)   
  68. {   
  69.     assert(q != NULL);   
  70.     if (q->front == q->rear)   
  71.         return false;   
  72.     struct queue_node *p = q->front->next;   
  73.     q->front->next = p->next;   
  74.     if (q->rear == p)   
  75.         q->rear = q->front;/*null*/  
  76.     free(p);   
  77.     return true;   
  78. }   
  79.   
  80. bool queue_empty(struct queue *q)   
  81. {   
  82.     assert(q != NULL);   
  83.     if (q->front->next == NULL)   
  84.         return true;   
  85.     else  
  86.         return false;   
  87. }   
  88.   
  89. int queue_size(struct queue *q)   
  90. {   
  91.     struct queue_node *p = q->front;   
  92.     int i = 0;   
  93.     while (p != q->rear) {   
  94.         i++;   
  95.         p = p->next;   
  96.     }   
  97.     return i;   
  98. }   
  99.   
  100. Type gethead(struct queue *q)   
  101. {   
  102.     assert(q != NULL);   
  103.     if (q->front != q->rear)   
  104.         return q->front->next->data;   
  105.     else  
  106.         return 0;   
  107. }   
  108.   
  109. void print_queue(struct queue *q)   
  110. {   
  111.     assert(q != NULL);   
  112.     struct queue_node *p = q->front;   
  113.     while (p->next != NULL) {   
  114.         p = p->next;   
  115.         printf("%d ", p->data);   
  116.     }   
  117. }  
     


主函数

Code:
  1. #include <stdio.h>   
  2. #include "queue.h"   
  3.   
  4. int main()   
  5. {   
  6.     struct queue *q = init_queue();   
  7.     en_queue(q, 2);   
  8.     en_queue(q, 1);   
  9.     en_queue(q, 3);   
  10.     en_queue(q, 5);   
  11.     de_queue(q);   
  12.     print_queue(q);   
  13.     destory_queue(q);   
  14.     return 0;   
  15. }