Chinaunix首页 | 论坛 | 博客
  • 博客访问: 330996
  • 博文数量: 73
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1293
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-07 11:17
个人简介

爱运动,爱看书,爱生活!

文章分类

全部博文(73)

文章存档

2014年(7)

2013年(66)

分类: C/C++

2013-09-01 13:51:46

/*
队列的基本概念:模拟现实生活中的队列,队列中允许插入的一端叫做队尾,
                        允许删除的一端叫做队头
顺序队列:所有操作的时间复杂度是O(1),因为它没有任何循环语句
顺序队列存在的问题:
    假溢出:假溢出是队尾指针超出了顺序队列定义的最大存储空间,但
                实际上队列中还有剩余存储空间
假溢出解决办法:采用顺序循环队列解决
    设队尾指针为rear,队头指针为front,最大存储空间为maxsize,
     采用rear=(rear+1)%maxsize=0实现

另一个问题:
队列队满和队空状态均为rear=front=0
可采用的解决方法:
1.少用一个存储单元:判断条件:front==(rear+1)%maxsize
2.设置一个标志位tag,初始时tag=0;每当入队列成功时,tag=1,出队列时tag=0
队列空的条件:rear=front && tag=0
队列满的条件:rear=front && tag=1
3.设置一个计数器count,初始时count=0,每当入队列时count加1,出队列count减1
这样这个计数器不但具有了tag的功能,还有了计数的功能
队空的判断条件:count=0
队满的判断条件:count==maxsize或者count>0 && rear==front
下面的例子将采用第三中解决方法,简单而且不浪费存储空间
链式队列:时间复杂度除了撤销空间是O(n)以外,其他都是O(1)
*/
顺序队列的实现

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 10

  4. //构造队列
  5. typedef struct queue{
  6.     int que[MAXSIZE];
  7.     int rear;
  8.     int front;
  9.     int count;
  10. }queue;

  11. //初始化队列
  12. void init(queue *q)
  13. {
  14.     q->rear=0;
  15.     q->front=0;
  16.     q->count=0;
  17. }

  18. //判断非空否
  19. int isEmpty(queue *q)
  20. {
  21.     if (q->count==0) return 0;
  22.     else return 1;
  23. }

  24. //插入数据元素
  25. int insert(queue *q,int x)
  26. {
  27.     if(q->count==MAXSIZE){
  28.         printf("队列已满!\n" );
  29.         return 0;
  30.     }
  31.     q->que[q->rear]=x;//数据元素入队尾
  32.     q->rear=(q->rear+1)%MAXSIZE;//后移队尾指针
  33.     q->count++;//元素个数加一
  34.     return 1;
  35. }

  36. //删除数据元素
  37. int delete(queue *q)
  38. {
  39.     if(q->count==0){
  40.         printf("队列为空!\n");
  41.         return 0;
  42.     }
  43.     q->front=(q->front+1)%MAXSIZE;//队头指示器加一
  44.     q->count--;//计数器减一
  45.     return 1;
  46. }

  47. //取队头元素
  48. int get(queue *q)
  49. {
  50.     if(q->count==0){
  51.         printf("队列为空!\n");
  52.         exit(-1);
  53.     }
  54.     return q->que[q->front];
  55. }


  56. int main()
  57. {
  58.     queue q;
  59.     init(&q);
  60.     int i;
  61.     for (i= 0; i < MAXSIZE; ++i)
  62.     {
  63.         insert(&q,i);
  64.     }
  65.     
  66.     for (i = 0; q.count>0; ++i)
  67.     {
  68.         printf("%d\n", get(&q));
  69.         delete(&q);
  70.     }
  71.     
  72.     return 0;
  73. }
链式队列的实现
优点:没有最大容量限制,取数据元素相对方便

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 10

  4. //链表节点
  5. typedef struct qnode
  6. {
  7.     int data;
  8.     struct qnode *next;
  9. }qnode;

  10. //通常为了方便参数调用,把链式队列的头尾节点指针定义为一个单独的结构体
  11. typedef struct queue
  12. {
  13.     qnode *front;
  14.     qnode *rear;
  15. }queue;
  16. //初始化
  17. int init(queue *q)
  18. {
  19.     q->front=NULL;
  20.     q->rear=NULL;
  21. }
  22. //判断非空否
  23. int isEmpty(queue *q)
  24. {
  25.     if(q->front==NULL) return 0;
  26.     else
  27.         return 1;
  28. }

  29. //插入数据元素
  30. void insert(queue *q,int x)
  31. {
  32.     qnode *p=(qnode*)malloc(sizeof(qnode));//为新元素分配存储空间
  33.     //给新元素赋值
  34.     p->data=x;
  35.     p->next=NULL;
  36.     //加入队列
  37.     //1.队列非空
  38.     if(q->rear!=NULL) q->rear->next=p;//队尾加入新节点
  39.     q->rear=p;//新节点成为新的队尾元素
  40.     //2.队列为空时新节点成为新的头元素
  41.     if(q->front==NULL) q->front=p;
  42. }
  43. //删除数据元素
  44. int delete(queue *q)
  45. {
  46.     qnode *p;
  47.     if(q->front==NULL){
  48.         printf("队列为空!\n");
  49.         return ;
  50.     }
  51.     //让p指向头元素
  52.     p=q->front;
  53.     //出队列脱链,改变队头指针
  54.     q->front=q->front->next;
  55.     //若删除的节点为最后一个,置队尾指针为空
  56.     if(q->front==NULL) q->rear=NULL;

  57.     free(p);
  58.     return 1;

  59. }

  60. //取队头元素
  61. int get(queue *q)
  62. {
  63.     if(q->front==NULL){
  64.         printf("队列为空!\n");
  65.         return 0;
  66.     }
  67.     return q->front->data;
  68. }

  69. //撤销空间
  70. void destroy(queue *q)
  71. {
  72.     qnode *p,*p1;
  73.     p=q->front;
  74.     while(p!=NULL){
  75.         p1=p;
  76.         p=p->next;
  77.         free(p1);
  78.     }
  79. }
  80. int main()
  81. {
  82.     queue q;
  83.     init(&q);
  84.     int i;
  85.     for (i= 0; i < MAXSIZE; ++i)
  86.     {
  87.         insert(&q,i);
  88.     }
  89.     
  90.      while(q.front!=NULL)
  91.     {
  92.         printf("%d\n", get(&q));
  93.         delete(&q);
  94.     }
  95.     
  96.     return 0;
  97. }




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