Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4241616
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-05-12 16:28:51

代码:
循环队列:首尾相接,front rear


1.初始化 front=rear=0 ,表示 空队列
2.当如果插入 ‘a’,front=0,rear=1;
3.当如果插入 ‘b’‘c’‘d’后,front=0,rear=4
4.当 ‘a’ 出队列后,front=1,rear=4
5.当 ‘b’‘c‘ ’d‘出队列后 ,front=4,rear=4

这里说明下:
  1. 当 front=rear时,表示 空队列
  2. 当  rear+1 % QUEUE_SIZE == front ,表示 满
     上面的 在第 3 步,就表示满队列了。。。
      front=0,rear=4,队列满时,留了一个元素位置,。
      如果不留这个空位置,则必然有 front=rear,就不能判断 是空,还是 满了
     所以,我们留了一个 空位置,判断 是否满

初始化:

queue.h

  1. #ifndef _QUEUE_H
  2. #define _QUEUE_H

  3. typedef char ElemType;

  4. #define QUEUE_SIZE 10
  5. typedef struct _queue
  6. { queue.rar  
  7.     ElemType *head; //head 指向队列数据 头
  8.     int front,rear;
  9. }QUEUE;

  10. QUEUE *init_queue();
  11. int in_queue(QUEUE *q,ElemType *e);
  12. int out_queue(QUEUE *q,ElemType *e);
  13. int get_head(QUEUE *q,ElemType *e);
  14. int is_empty(QUEUE *q);

  15. #endif
queue.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "queue.h"

  4. QUEUE *init_queue()
  5. {
  6.     QUEUE *q=NULL;
  7.     q=(QUEUE *)malloc(sizeof(QUEUE)); //申请 *head front rear 空间
  8.     if(q==NULL)
  9.         exit(1);
  10.     
  11.     q->head=(ElemType *)malloc(sizeof(ElemType)*QUEUE_SIZE);//QUEUE_SIZE=10
  12.     if(q->head==NULL)
  13.     {
  14.         free(q->head);
  15.         exit(1);
  16.     }
  17.         
  18.     q->rear=q->front=0; //0 表示空
  19.     return q;
  20. }
  21. /*
  22.     环形队列首尾相连,当队头 front=QUEUE_SIZE - 1 后,再前进一个位置就自动到0,
  23.         可以利用除法取余的运算 % 来实现
  24. */
  25. /*队列 进
  26.     队头front进1 : front=(front+1)%QUEUE_SIZE;    
  27.     队尾rear进1 : rear=(rear+1)%QUEUE_SIZE;
  28. */
  29. int in_queue(QUEUE *q,ElemType *e)
  30. {
  31.     if((q->rear+1)%QUEUE_SIZE==q->front) //队满
  32.         return 1;
  33.     *(q->head+q->rear)=*e; //指针方式 实现
  34.     //或者 q->head[q->rear]=*e; 数组方式 实现
  35.     q->rear=(q->rear+1)%QUEUE_SIZE;// 队尾前进 一位
  36.     
  37.     return 0; //成功返回
  38. }


  39. int out_queue(QUEUE *q,ElemType *e)
  40. {
  41.     if(q->rear==q->front)// 队列空
  42.         return 1; //翻译
  43.     *e=*(q->head+q->front); //先传值,然后,将 front 加 1,
  44.     //*e=(q->head[q->front]); //数组 方式 传值;
  45.     q->front=(q->front+1)%QUEUE_SIZE;
  46.     
  47.     return 0;
  48. }

  49. //获取 头元素数据
  50. int get_head(QUEUE *q,ElemType *e)
  51. {
  52.     if(q->front==q->rear) //队列为空
  53.         return 1;
  54.     *e=*(q->head+q->front); //指针方式获取
  55. //    *e=q->head[q->front]; //数组方式 获取
  56.     return 0; //成功返回
  57. }

  58. //判断 队列空
  59. int is_empty(QUEUE *q)
  60. {
  61.     if(q->rear==q->front)
  62.         return 0; //为空 ,返回 0
  63.     else
  64.         return 1;
  65. }
main.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "queue.h"

  4. int main()
  5. {
  6.     ElemType ch1='a';
  7.     ElemType ch2='b';
  8.     ElemType ch;
  9.     QUEUE *queue;
  10.     queue=init_queue();

  11.     printf("in a data\n");
  12.     in_queue(queue,&ch1);
  13.     printf("front-data: %c\n",*(queue->head+queue->front));
  14.     printf("front=%d,rear=%d\n\n",queue->front,queue->rear);

  15.     printf("in a data\n");
  16.     in_queue(queue,&ch2);
  17.     printf("front-data: %c\n",*(queue->head+queue->front));
  18.     printf("front=%d,rear=%d\n\n",queue->front,queue->rear);

  19.     printf("\nout a data\n");
  20.     out_queue(queue,&ch);
  21.     printf("the out data is %c:\n",ch);
  22.     printf("front-data: %c\n",*(queue->head+queue->front));
  23.     printf("front=%d,rear=%d\n",queue->front,queue->rear);
  24.     
  25.     return 0;
  26. }



显示:

  1. ywx@yuweixian:~/yu/data-struct/book/queue$ ./main
  2. in a data
  3. front-data: a
  4. front=0,rear=1

  5. in a data
  6. front-data: a
  7. front=0,rear=2


  8. out a data
  9. the out data is a:
  10. front-data: b
  11. front=1,rear=2


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