[关闭]
@songpfei 2016-03-30T14:28:56.000000Z 字数 4932 阅读 1387

队列

数据结构


1. 队列的定义

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
是一种先进先出(FIFO—first in first out)线性表。

2. 队列的分类

2.1 顺序队列

2.1.1 顺序队列的概念

建立顺序队列结构必须为其静态分配或动态申请一片连续的存储空间,并设置两个指针进行管理。一个是队头指针front,它指向队头元素;另一个是队尾指针rear,它指向下一个入队元素的存储位置。

每次在队尾插入一个元素是,rear增1;每次哎队头删除一个元素时,front增1。随着插入和删除操作的进行,队列元素的个数不断变化,队列所占的存储空间也在为队列结构所分配的连续空间中移动。当front=rear时,队列中没有任何元素,称为空队列。当rear增加到指向分配的连续空间之外时,队列无法再插入新元素,但这时往往还有大量可用空间未被占用,这些空间是已经出队的队列元素曾经占用过得存储单元。

顺序队列中的溢出现象:
(1) "下溢"现象:当队列为空时,做出队运算产生的溢出现象。“下溢”是正常现象,常用作程序控制转移的条件。
(2)"真上溢"现象:当队列满时,做进栈运算产生空间溢出的现象。“真上溢”是一种出错状态,应设法避免。
(3)"假上溢"现象:由于入队和出队操作中,头尾指针只增加不减小,致使被删元素的空间永远无法重新利用。当队列中实际的元素个数远远小于向量空间的规模时,也可能由于尾指针已超越向量空间的上界而不能做入队操作。该现象称为"假上溢"现象。

参照:http://student.zjzk.cn/course_ware/data_structure/web/zhanhuoduilie/zhanhuoduilie3.2.2.1.htm

2.1.2顺序队列的c语言实现

队列有下面几个操作:

顺序队列的数组实现

  1. #ifndef _QUEUE_H
  2. #define _QUEUE_H
  3. #ifdef __cplusplus
  4. extern "C"{
  5. #endif
  6. #define ElementType int
  7. typedef struct tagQueue
  8. {
  9. int capacity;
  10. int front;
  11. int rear;
  12. ElementType* array;
  13. }Queue;
  14. Queue* CreatQueue(int max_elements);
  15. bool IsQueueEmpty(Queue* q);
  16. bool IsQueueFull(Queue* q);
  17. void EnQueue(Queue* q, ElementType val);
  18. ElementType DeQueue(Queue* q);
  19. void DistroyQueue(Queue* q);
  20. #ifdef __cplusplus
  21. }
  22. #endif
  23. #endif
  1. #include"queue.h"
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. Queue* CreatQueue(int max_elements)
  5. {
  6. Queue *q = (Queue*)malloc(sizeof(Queue));
  7. if (q == NULL)
  8. {
  9. printf("Queue Memory allocation failure\n");
  10. exit(-1);
  11. }
  12. q->array = (ElementType*)malloc(sizeof(ElementType)*max_elements);
  13. if (q->array == NULL)
  14. {
  15. printf("q->array Memory allocation failure\n");
  16. exit(-1);
  17. }
  18. q->capacity = max_elements;
  19. q->front = 0;
  20. q->rear = 0;
  21. return q;
  22. }
  23. bool IsQueueEmpty(Queue* q)
  24. {
  25. if (q->front == q->rear)
  26. return true;
  27. return false;
  28. }
  29. bool IsQueueFull(Queue* q)
  30. {
  31. if (q->rear >= q->capacity)
  32. return true;
  33. return false;
  34. }
  35. void EnQueue(Queue* q, ElementType val)
  36. {
  37. if (IsQueueFull(q))
  38. {
  39. printf("queue is full.");
  40. exit(-1);
  41. }
  42. q->array[q->rear] = val;
  43. q->rear++;
  44. }
  45. ElementType DeQueue(Queue* q)
  46. {
  47. if (IsQueueEmpty(q))
  48. {
  49. printf("queue is empty.");
  50. exit(-1);
  51. }
  52. q->front++;
  53. return q->array[q->front - 1];
  54. }
  55. void DistroyQueue(Queue* q)
  56. {
  57. if (q != NULL)
  58. {
  59. if (q->array != NULL)
  60. free(q->array);
  61. free(q);
  62. }
  63. }
  1. // test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include"queue.h"
  4. #include<stdio.h>
  5. int main()
  6. {
  7. Queue* q= CreatQueue(3);
  8. EnQueue(q, 10);
  9. EnQueue(q, 9);
  10. EnQueue(q, 8);
  11. printf("%d\n", DeQueue(q));
  12. printf("%d\n", DeQueue(q));
  13. printf("%d\n", DeQueue(q));
  14. DistroyQueue(q);
  15. return 0;
  16. }

2.2 循环队列

2.2.1 概念

在实际使用队列时,为了使队列空间能重复使用,往往对队列的使用方法稍加改进:无论插入或删除,一旦rear指针增1或front指针增1 时超出了所分配的队列空间,就让它指向这片连续空间的起始位置。自己真从MaxSize-1增1变到0,可用取余运算rear%MaxSize和front%MaxSize来实现。这实际上是把队列空间想象成一个环形空间,环形空间中的存储单元循环使用,用这种方法管理的队列也就称为循环队列。处了一些简单应用之外,真正实用的队列时循环队列。
在循环队列中,当队列为空时,有front=rear,而当所有队列空间全占满时,也有front=rear。为了区别这两种情况,规定循环队列最多只能有MaxSize-1个队列元素,当循环队列中只剩下一个空存储单元时,队列就已经满了。因此,队列判空的条件时front=rear,而队列判满的条件时front=(rear+1)%MaxSize

2.2.2 循环队列的C语言实现

2.3 队列的链表实现

  1. #ifndef _QUEUE_H
  2. #define _QUEUE_H
  3. #ifdef __cplusplus
  4. extern "C"{
  5. #endif
  6. typedef int ElementType;
  7. typedef struct tagListNode
  8. {
  9. ElementType element;
  10. struct tagListNode* pNextNode;
  11. }ListNode;
  12. typedef struct tagQueue
  13. {
  14. ListNode* front;
  15. ListNode* rear;
  16. }Queue;
  17. Queue* CreatQueue();
  18. bool IsQueueEmpty(Queue* q);
  19. void EnQueue(Queue* q, ElementType val);
  20. ElementType Front(Queue* q);
  21. void DeQueue(Queue* q);
  22. void DistroyQueue(Queue* q);
  23. #ifdef __cplusplus
  24. }
  25. #endif
  26. #endif
  1. #include"queue.h"
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #define FatalError(str) fprintf(stderr,"%s\n",str),exit(1)
  5. Queue* CreatQueue()//建立一个空队列
  6. {
  7. Queue *q = (Queue*)malloc(sizeof(Queue));
  8. if (q == NULL)
  9. FatalError("Queue Memory allocation failure");
  10. q->front =q->rear = NULL;
  11. return q;
  12. }
  13. bool IsQueueEmpty(Queue* q)
  14. {
  15. return q->front==NULL;
  16. }
  17. void EnQueue(Queue* q, ElementType val)
  18. {
  19. ListNode* node = (ListNode*)malloc(sizeof(ListNode));
  20. if (node==NULL)
  21. FatalError("List Node Memory allocation failure");
  22. node->element = val;
  23. node->pNextNode = NULL;
  24. if (IsQueueEmpty(q))
  25. q->front = node;//将节点插入空队列
  26. else
  27. q->rear->pNextNode = node;//链到原队尾节点后
  28. q->rear = node;//队尾指针指向新的尾
  29. }
  30. void DeQueue(Queue* q)
  31. {
  32. if (IsQueueEmpty(q))
  33. FatalError("queue is empty.");
  34. ListNode* node = q->front;
  35. q->front = node->pNextNode;
  36. if (q->rear == node)//原队列只有一个节点
  37. q->rear = NULL;
  38. free(node);
  39. }
  40. ElementType Front(Queue* q)
  41. {
  42. if (IsQueueEmpty(q))
  43. FatalError("queue is empty.");
  44. return q->front->element;
  45. }
  46. void DistroyQueue(Queue* q)
  47. {
  48. if (q != NULL)
  49. {
  50. while(!IsQueueEmpty(q))
  51. DeQueue(q);
  52. free(q);
  53. }
  54. }`此处输入代码`
  1. // huawei_test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include"queue.h"
  4. #include<stdio.h>
  5. int main()
  6. {
  7. Queue* q= CreatQueue();
  8. EnQueue(q, 10);
  9. EnQueue(q, 9);
  10. EnQueue(q, 8);
  11. printf("%d\n", Front(q));
  12. DeQueue(q);
  13. printf("%d\n", Front(q));
  14. DeQueue(q);
  15. printf("%d\n", Front(q));
  16. DeQueue(q);
  17. DeQueue(q);
  18. DistroyQueue(q);
  19. return 0;
  20. }

3. 队列的STL实现

queue
queue模板类的定义在头文件中。

与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque类型。

定义queue对象的示例代码如下:

queue q1;

queue q2;

queue的基本操作有:

入队,如例:q.push(x); 将x接到队列的末端。

出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

访问队首元素,如例:q.front(),即最早被压入队列的元素。

访问队尾元素,如例:q.back(),即最后被压入队列的元素。

判断队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()

4. 优先队列

4.1 定义:

普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (largest-in,first-out)的行为特征。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注