队列

  • 队列初始化
  • 入队列
  • 出队列
  • 队列判空
  • 获取队首元素
  • 获取队尾元素
  • 获取队列中元素个数
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4.   
  5. typedef int DataType;  
  6.   
  7. /** 
  8. *队列中的节点 
  9. */  
  10. typedef struct Node {  
  11.     DataType _data;  
  12.     struct Node* _pNext;  
  13. }*PNode, Node;  
  14.   
  15.   
  16. /** 
  17. *队列 
  18. pHead 队头指针 
  19. pTail 队尾指针 
  20. size 队列大小 
  21. */  
  22. typedef struct Queue {  
  23.     struct Node* pHead;  
  24.     struct Node* pTail;  
  25. }Queue;  
  26.   
  27. /** 
  28. 初始化队列 
  29. */  
  30. void QueueInit(Queue* queue);  
  31. /** 
  32. 入队 
  33. */  
  34. void QueuePush(Queue* queue, DataType data);  
  35. /** 
  36. 出队列 
  37. */  
  38. int QueuePop(Queue* queue, DataType* data);  
  39.   
  40. /** 
  41. 判空 
  42. */  
  43. int QueueEmpty(Queue* queue);  
  44.   
  45. /**  
  46. 取队头元素  
  47. */  
  48. DataType QueueFront(Queue* q);  
  49.   
  50. /** 
  51. 取队尾元素 
  52. */   
  53. DataType QueueBack(Queue* q);  
  54.   
  55. /** 
  56. 获取队列中元素的个数 
  57. */   
  58. int QueueSize(Queue* q);  
  59.   
  60.   
  61. void TestQueue();  
  62.   
  63. int main() {  
  64.     TestQueue();  
  65.     return 0;  
  66. }  
  67.   
  68.   
  69. void TestQueue() {  
  70.     Queue* queue = (Queue*)malloc(sizeof(Queue));  
  71.     QueueInit(queue);  
  72.     QueuePush(queue, 4);  
  73.     QueuePush(queue, 5);  
  74.     QueuePush(queue, 6);  
  75.     printf("队列中元素个数为%d\n\n", QueueSize(queue));  
  76.   
  77.     printf("队列是否为空:%s\n", QueueEmpty(queue) == 1 ? "为空" : "不为空");  
  78.   
  79.     printf("队头为%d\n", QueueFront(queue));  
  80.     printf("队尾为%d\n", QueueBack(queue));  
  81.     int a;  
  82.     QueuePop(queue, &a);  
  83.     printf("\n出队列%d\n", a);  
  84.   
  85.     QueuePop(queue, &a);  
  86.     printf("出队列%d\n", a);  
  87.   
  88.     QueuePop(queue, &a);  
  89.     printf("出队列%d\n", a);  
  90.   
  91.     printf("队列中元素个数为%d\n\n", QueueSize(queue));  
  92.   
  93.     printf("队列是否为空:%s\n\n", QueueEmpty(queue) == 1 ? "为空" : "不为空");  
  94. }  
  95.   
  96. void QueueInit(Queue* queue)   
  97. {  
  98.     if (queue == NULL)  
  99.         return;  
  100.   
  101.     queue->pHead = NULL;  
  102.     queue->pTail = NULL;  
  103. }  
  104.   
  105. void QueuePush(Queue* queue, DataType data) {  
  106.     if (queue == NULL)  
  107.         return;  
  108.   
  109.     PNode node = (PNode)malloc(sizeof(Node));  
  110.     if (node == NULL)  
  111.         return;  
  112.   
  113.     node->_pNext = NULL;  
  114.     node->_data = data;  
  115.   
  116.     if (queue->pHead == NULL) {  
  117.         queue->pHead = node;  
  118.         queue->pTail = node;  
  119.     }  
  120.     else {  
  121.         queue->pTail->_pNext = node;  
  122.         queue->pTail = node;  
  123.     }  
  124. }  
  125.   
  126. int QueuePop(Queue* queue, DataType* data) {  
  127.     if (queue == NULL)  
  128.         return 0;  
  129.     if (queue->pHead == NULL)  
  130.         return 0;  
  131.   
  132.     *data = queue->pHead->_data;  
  133.   
  134.     PNode pre = queue->pHead;  
  135.     queue->pHead = queue->pHead->_pNext;  
  136.     pre->_pNext = NULL;  
  137.   
  138.     free(pre);  
  139.   
  140.     return 1;  
  141. }  
  142.   
  143. int QueueEmpty(Queue* queue) {  
  144.     if (queue->pHead == NULL)  
  145.         return 1;  
  146.     return 0;  
  147. }  
  148.   
  149. // 取队头元素   
  150. DataType QueueFront(Queue* q) {  
  151.     if (q == NULL || q->pHead == NULL)  
  152.         return -1;  
  153.     return q->pHead->_data;  
  154. }  
  155.   
  156. // 取队尾元素   
  157. DataType QueueBack(Queue* q) {  
  158.     if (q == NULL || q->pTail == NULL)  
  159.         return -1;  
  160.     return q->pTail->_data;  
  161. }  
  162.   
  163. // 获取队列中元素的个数   
  164. int QueueSize(Queue* q) {  
  165.   
  166.     if (q == NULL || q->pHead == NULL)  
  167.         return 0;  
  168.   
  169.     PNode cur = q->pHead;  
  170.   
  171.     int count = 1;  
  172.   
  173.     while (cur != q->pTail) {  
  174.         cur = cur->_pNext;  
  175.         count++;  
  176.     }  
  177.   
  178.     return count;  
  179. }  

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值