Chinaunix首页 | 论坛 | 博客
  • 博客访问: 451941
  • 博文数量: 42
  • 博客积分: 1325
  • 博客等级: 中尉
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-13 18:00
个人简介

呵~~~呵~~~

文章分类

全部博文(42)

文章存档

2016年(3)

2015年(1)

2014年(2)

2013年(2)

2012年(7)

2011年(11)

2010年(3)

2009年(13)

我的朋友

分类: C/C++

2013-10-30 11:40:44

循环队列的特点什么的网上资料多,这里就直接上代码吧.
 
1 声明及定义
  1. typedef struct
  2. {
  3.     size_t size;                       /* 初始化后放入队列的容量 */

  4.     volatile unsigned int front;
  5.     volatile unsigned int rear;
  6. }circularqueuehead_t;

  7. typedef struct __attribute__((packed))
  8. {
  9.     unsigned char content[LENGTH];     /* 队列元素的内容 */
  10. }atom_t;

  11. static circularqueuehead_t *g_cq_head; /* 初始化后指向队列头 */
  12. static atom_t              *g_cq;      /* 初始化后指向队列本身 */
2 算法-1
  1. /************************************************
  2.  * Name        : cq_write/cq_read
  3.  * Description :
  4.  * Parameters  : atom_t *operand
  5.  * Retrun      : 0. write/read success
  6.  *               1. write - queue is full
  7.  *                  read  - queue is empty
  8.  * Developer   : zhonghaohua
  9.  * Created     : Wed Oct 30 10:59:46 2013
  10. ************************************************/
  11. int cq_write( atom_t *srt)
  12. {
  13.     unsigned int nextpost = ( g_cq_head->front + 1) % g_cq_head->size;

  14.     if( nextpost != g_cq_head->rear ) {
  15.         g_cq[nextpost] = *srt;
  16.         g_cq_head->front = nextpost;

  17.         return 0;
  18.     }
  19.     else {
  20.         return 1;
  21.     }
  22. }

  23. int cq_read( atom_t *srt)
  24. {
  25.     if( g_cq_head->rear != g_cq_head->front) {
  26.         g_cq_head->rear = (g_cq_head->rear + 1) % g_cq_head->size;
  27.         *srt = g_cq[g_cq_head->rear];

  28.         return 0;
  29.     }
  30.     else {
  31.         return 1;
  32.     }
  33. }
3 算法-2
    1. /************************************************
    2.  * Name        : cq_write/cq_read 
    3.  * Description : 
    4.  * Parameters  : atom_t *operand
    5.  * Retrun      : 0. write/read success 
    6.  *               1. write - queue is full
    7.  *                  read  - queue is empty
    8.  * Developer   : zhonghaohua
    9.  * Created     : Wed Oct 30 10:59:46 2013
    10. ************************************************/
  1. int cq_write( atom_t *sa)
  2. {
  3.     unsigned int nextpost = ( g_cq_head->front + 1) % g_cq_head->size;

  4.     if( nextpost != g_cq_head->rear ) {
  5.         g_cq[g_cq_head->front] = *sa;
  6.         g_cq_head->front = nextpost;

  7.         return 0;
  8.     }
  9.     else {
  10.         return 1;
  11.     }
  12. }

  13. int cq_read( atom_t *sa)
  14. {
  15.     if( g_cq_head->rear != g_cq_head->front) {
  16.         *sa = g_cq[g_cq_head->rear];
  17.         g_cq_head->rear = (g_cq_head->rear + 1) % g_cq_head->size;

  18.         return 0;
  19.     }
  20.     else {
  21.         return 1;
  22.     }
  23. }
4 说明
    算法-1 和 算法-2 差别不大
    相同:  1) 都以 front + 1 == rear 作为队列满的标记
           2) 都以 rear == front 作为队列空的标记
    差别:  1) 算法-1: write 先[front + 1] = *src,后(front++);  read 先(rear++),      后 *src = [rear].
           2) 算法-2: write 先[front] = *src,    后(front++);  read 先 *src = [rear],后(rear++).
    在front/rear都初始化为0时, 算法-1在第一次读写时, 队列[0]会被跳过.

END.
阅读(1429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~