[关闭]
@yanglt7 2018-10-21T15:50:47.000000Z 字数 3503 阅读 676

05_队列

数据结构和算法


5.1 队列的定义

<--出队列 A1 A2 A3 ...... An <--入队列
队头 队尾

5.2 队列的链式存储结构

  1. typedef struct{
  2. ElemType data;
  3. struct QNode *next;
  4. } QNode, *QueuePrt;
  5. typedef struct
  6. {
  7. QueuePrt front, rear; //队头、尾指针
  8. } LinkQueue;
头结点 --> A1 --> A2 --> ... --> An ^
front rear
头结点 ^
front rear

5.2.1 创建一个队列

  1. InitQueue(LinkQueue *q)
  2. {
  3. q->front = q->rear = (QueuePrt)malloc(sizeof(QNode));
  4. if( !q->front )
  5. exit(0);
  6. q->front->next = NULL;
  7. }

5.2.2 入队列操作

头结点 --> e1 e2
front rear p
头结点 --> e1 --> e2
front rear p
头结点 --> e1 --> e2
front rear
  1. InsertQueue(LinkQueue *q, ElemType e)
  2. {
  3. QueuePrt p;
  4. p = (QueuePrt)malloc(sizeof(QNode));
  5. if( p == NULL )
  6. exit(0);
  7. p->data = e;
  8. p->next = NULL;
  9. q->rear->next = p;
  10. q->rear = p;
  11. }

5.2.3 出队列操作

头结点 --> e1 --> e2
front rear
头结点 e1 e2
front *e = e1 rear
头结点 --> e1
front rear
头结点 ^ e1
front rear *e = e1
  1. DeleteQueue(LinkQueue *q, ElemType *e)
  2. {
  3. QueuePrt p;
  4. if( q->front == q->rear )
  5. return;
  6. p = q->front->next;
  7. *e = p->data;
  8. q->front->next = p->next;
  9. if( q->rear == p )
  10. q->rear = q->front;
  11. free(p);
  12. }

5.2.4 销毁一个队列

  1. DestroyQueue(LinkQueue *q)
  2. {
  3. while( q->front )
  4. {
  5. q->rear = q->front->next;
  6. free( q->front );
  7. q->front = q->rear;
  8. }
  9. }

5.2.5 运用

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef char ElemType;
  4. typedef struct{
  5. ElemType data;
  6. struct QNode *next;
  7. } QNode, *QueuePrt;
  8. typedef struct
  9. {
  10. QueuePrt front, rear; //队头、尾指针
  11. } LinkQueue;
  12. void InitQueue(LinkQueue *q)
  13. {
  14. q->front = q->rear = (QueuePrt)malloc(sizeof(QNode));
  15. if( !q->front )
  16. exit(0);
  17. q->front->next = NULL;
  18. }
  19. void InsertQueue(LinkQueue *q, ElemType e)
  20. {
  21. QueuePrt p;
  22. p = (QueuePrt)malloc(sizeof(QNode));
  23. if( p == NULL )
  24. exit(0);
  25. p->data = e;
  26. p->next = NULL;
  27. q->rear->next = p;
  28. q->rear = p;
  29. }
  30. void DeleteQueue(LinkQueue *q, ElemType *e)
  31. {
  32. QueuePrt p;
  33. if( q->front == q->rear )
  34. return;
  35. p = q->front->next;
  36. *e = p->data;
  37. q->front->next = p->next;
  38. if( q->rear == p )
  39. q->rear = q->front;
  40. free(p);
  41. }
  42. void DestroyQueue(LinkQueue *q)
  43. {
  44. while( q->front )
  45. {
  46. q->rear = q->front->next;
  47. free( q->front );
  48. q->front = q->rear;
  49. }
  50. }
  51. int main()
  52. {
  53. LinkQueue q;
  54. ElemType c;
  55. InitQueue(&q);
  56. printf("请输入字符并以#作为结束标志: \n");
  57. scanf("%c", &c);
  58. while( c != '#' )
  59. {
  60. InsertQueue(&q, c);
  61. scanf("%c", &c);
  62. }
  63. printf("输出的字符串为: \n");
  64. while( q.front != q.rear )
  65. {
  66. DeleteQueue(&q, &c);
  67. printf("%c", c);
  68. }
  69. printf("\n");
  70. return 0;
  71. }

5.3 队列的顺序存储结构

5.3.1 循环队列定义

  1. #define MAXSIZE 100
  2. typedef struct
  3. {
  4. ElemType *base;//用于存放分配基地址
  5. int front;
  6. int rear;
  7. } cycleQueue;

5.3.2 初始化循环队列

  1. InitQueue(cycleQueue *q)
  2. {
  3. q->base = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
  4. if( !q->base )
  5. exit(0);
  6. q->front = q->rear = 0;
  7. }

5.3.3 入队列操作

  1. InsertQueue(cycleQueue *q, ElemType e)
  2. {
  3. if( (q->rear+1)%MAXSIZE == q->front )
  4. return; //队列已满
  5. q->base[q->rear] = e;
  6. q->rear = (q->rear+1) % MAXSIZE;
  7. }

5.3.4 出队列操作

  1. DeleteQueue(cycleQueue *q, ElemType *e)
  2. {
  3. if( q->front == q->rear )
  4. return; //队列为空
  5. *e = q->base[q->front];
  6. q->front = (q->front+1) % MAXSIZE;
  7. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注