Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2645
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-08 10:46
文章分类
文章存档

2016年(1)

2015年(1)

我的朋友
最近访客

分类: C/C++

2016-01-06 17:34:37


点击(此处)折叠或打开

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: Queue.c
  5.  *
  6.  * Description:
  7.  * Complete 4 interfaces for a circular queue:
  8.  * 1) Whether it is an empty circular queue;
  9.  * 2) Whether it is a full circular queue;
  10.  * 3) Add data into circular queue;
  11.  * 4) Remove data from circular queue.
  12.  *
  13.  * =====================================================================================
  14.  */


  15. #include <stdio.h>
  16.  
  17. #include <stdlib.h>

  18. #include <string.h>



  19. //****************************************************************************/

  20. // Private Constants and Macros

  21. //****************************************************************************/

  22. #define TRUE 1

  23. #define FALSE 0



  24. //*************************************************/

  25. // Private Type Definitions

  26. //*************************************************/

  27. typedef unsigned int UInt32;

  28. typedef unsigned short UInt16;

  29. typedef unsigned char UInt8;

  30. typedef int Int32;

  31. typedef short Int16;

  32. typedef char Int8;

  33. typedef char Bool;



  34. //! Circular queue elements definition

  35. typedef struct {
  36.     UInt32 Front; // Front of queue

  37.     UInt32 Rear; // Rear of queue

  38.     UInt32 Count; // How many member in this queue

  39.     UInt32 Data[QUEUE_SIZE]; // An arrary to store the queue data

  40. }Queue_t;



  41. /* Size of circular queue */

  42. #define QUEUE_SIZE 5

  43. /* Circular queue */

  44. Queue_t Queue;



  45. /**************************************************/

  46. /*!

  47. * \brief Initialize circular queue

  48. *

  49. * \iparam Q = which circular queue is going to be intialized

  50. *

  51. * \return Nothing

  52. *

  53. **************************************************/

  54. void InitQueue(Queue_t *Q)

  55. {

  56.     Q->Front = 0;

  57.     Q->Rear = 0;

  58.     Q->Count = 0;

  59.     memset(Q, 0, sizeof(Q->Data));

  60. }



  61. /**************************************************/

  62. /*!

  63.  * \brief To judge if it's an empty circular queue

  64.  *

  65.  * \iparam Q = which circular queue

  66.  *

  67.  * \return TURE(Empty queue) or FALSE (Not empty)

  68.  *

  69.  **************************************************/



  70. Bool IsEmpty(Queue_t *Q) {

  71.     if (Q->Count == 0) {

  72.         printf("Queue is empty!\n");

  73.         return TRUE;

  74.     }

  75.     else {

  76.         return FALSE;

  77.     }

  78. }



  79. /**************************************************/

  80. /*!

  81.  * \brief To judge if it's a full circular queue

  82.  *

  83.  * \iparam Q = which circular queue

  84.  *

  85.  * \return TURE(full queue) or FALSE (Not full)

  86.  *

  87.  **************************************************/

  88. Bool IsFull(Queue_t *Q) {

  89.     if (Q->Count == QUEUE_SIZE) {

  90.         printf("Queue is full!\n");

  91.         return TRUE;

  92.     }

  93.     else {

  94.         return FALSE;

  95.     }

  96. }



  97. /**************************************************/

  98. /*!

  99. * \brief Add a new data into circular queue

  100. * If it's a full queue, it couldn't involve any data any more;

  101. * If not, this data can be added in this queue.

  102. *

  103. * \iparam Q = which circular queue

  104. * \iparam Data = A new data which is going to be added into

  105. * the circular queue

  106. *

  107. * \return -1 (Fail to be added into queue)

  108. * or 0 (Succeed to be added into queue)

  109. *

  110. **************************************************/

  111. Int32 AddQueue(Queue_t *Q, UInt32 Data)
  112. {
  113.     if (IsFull(Q)) {

  114.         return -1;

  115.     }

  116.     Q->Data[Q->Rear] = Data;

  117.     Q->Rear = (Q->Rear + 1) % QUEUE_SIZE;

  118.     Q->Count++;

  119.     return 0;

  120. }



  121. /**************************************************/

  122. /*!

  123. * \brief Remove an existed data from circular queue

  124. * If it's an empty queue, there is no data going to be

  125. * removed. If not, an existed data will be removed from

  126. * it successfully.

  127. *

  128. * \iparam Q = which circular queue

  129. * \iparam *Data = the address where the data is going to
  130. * be removed from queue
  131. *

  132. * \return -1 (Fail to be removed from queue)

  133. * or 0 (Succeed to be removed from queue)

  134. *

  135. **************************************************/


  136. Int32 RemoveQueue(Queue_t *Q, UInt32 *Data)

  137. {
  138.     if (IsEmpty(Q)) {

  139.         return -1;

  140.     }
  141.     
  142.     *Data = Q->Data[Q->Front];

  143.     Q->Front = (Q->Front + 1) % QUEUE_SIZE;

  144.     Q->Count--;
  145.     
  146.     return 0;

  147. }

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

上一篇:单片机定时器时间的设置

下一篇:没有了

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