当前位置:   article > 正文

队列(循环顺序队列、链队列、循环链表队列)_循环链队

循环链队

目录

一、循环顺序队列

1.1 假溢出现象

1.2 循环顺序队列 

1.3 算法实现

1.3.1 初始化队列

1.3.2 入队操作 

1.3.3 出队操作 

1.3.4 读取队头元素 

1.3.5 判空操作

1.3.6 判满操作 

二、链队列

2.1 初始化队列

2.2 入队操作

2.3 出队操作

2.4 读取队头元素

2.5 判空操作

2.6 销毁操作 

三、循环链表队列


一、循环顺序队列

1.1 假溢出现象


1.2 循环顺序队列 

循环顺序队列:将顺序队列的数组看成一个环状空间,即规定最后一个单元的后继为第一个单元。

虽然不存在物理的循环操作,但是可以通过软件方法实现 —— 使用求模操作

求模操作:(4+1)mod 5 = 0


1.3 算法实现

1.3.1 初始化队列

  1. void InitQueue(QueuePtr Q) {
  2. Q->front = Q->rear = 0;
  3. }

1.3.2 入队操作 

  1. bool EnterQueue(QueuePtr Q, QueueElementType x) {
  2. if (IsFull(Q)) return false;
  3. /* 生成新结点 */
  4. Q->element[Q->rear] = x;
  5. /* 重新设置队尾 */
  6. Q->rear = (Q->rear + 1) % MAXSIZE;
  7. return true;
  8. }

1.3.3 出队操作 

  1. bool DeleteQueue(QueuePtr Q, QueueElementType *x) {
  2. if (IsEmpty(Q)) return false;
  3. /* 将出队的元素存放到x所指向的变量中 */
  4. *x = Q->element[Q->front];
  5. /* 重新设置队头 */
  6. Q->front = (Q->front + 1) % MAXSIZE;
  7. return true;
  8. }

1.3.4 读取队头元素 

  1. bool GetHead(QueuePtr Q, QueueElementType *x) {
  2. if (IsEmpty(Q)) return false;
  3. /* 将队头元素存放到x所指向的变量中 */
  4. *x = Q->element[Q->front];
  5. return true;
  6. }

1.3.5 判空操作

  1. bool IsEmpty(QueuePtr Q) {
  2. /*
  3. * if (Q->front == Q->rear)
  4. * return true;
  5. * else
  6. * return false;
  7. */
  8. return Q->front == Q->rear;
  9. }

1.3.6 判满操作 

  1. bool IsFull(QueuePtr Q) {
  2. /*
  3. * if ((Q->rear + 1) % MAXSIZE == Q->front)
  4. * return true;
  5. * else
  6. * return false;
  7. */
  8. return (Q->rear + 1) % MAXSIZE == Q->front;
  9. }

二、链队列

链队列:用链表表示的队列。

  1. /* 定义结点类型 */
  2. typedef struct Node {
  3. QueueElementType data; /* 数据域 */
  4. struct Node *next; /* 指针域 */
  5. } LinkQueueNode, *LinkQueueNodePtr;
  6. /* 定义链表类型 */
  7. typedef struct {
  8. LinkQueueNode *front; /* 头指针 */
  9. LinkQueueNode *rear; /* 尾指针 */
  10. } LinkQueue, Queue, *QueuePtr;

2.1 初始化队列

  1. void InitQueue(QueuePtr Q) {
  2. /* 生成头结点,同时设置头指针 */
  3. Q->front = (LinkQueueNodePtr)malloc(sizeof(LinkQueueNode));
  4. Q->front->next = NULL;
  5. /* 设置尾指针 */
  6. Q->rear = Q->front;
  7. }

2.2 入队操作

  1. bool EnterQueue(QueuePtr Q, QueueElementType x) {
  2. /* 生成新结点 */
  3. LinkQueueNode * NewNode = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
  4. NewNode->data = x;
  5. NewNode->next = NULL;
  6. if (NewNode == NULL)
  7. return false;
  8. /* 通过尾插法实现入队 */
  9. Q->rear->next = NewNode;
  10. Q->rear = NewNode;
  11. return true;
  12. }

2.3 出队操作

情况1:队列中有不止一个元素。

情况2:队列中只有一个元素。

  1. bool DeleteQueue(QueuePtr Q, QueueElementType *x) {
  2. if (IsEmpty(Q)) return false;
  3. /* 将队列Q的队头元素出队 */
  4. LinkQueueNodePtr p = Q->front->next;
  5. Q->front->next = p->next;
  6. /* 如果队列中只有一个元素p,则p出队后成为空队 */
  7. if (Q->rear == p)
  8. Q->rear = Q->front; /* 重新设置尾指针 */
  9. /* 将出队的元素存放到x所指向的变量中 */
  10. *x = p->data;
  11. free(p);
  12. return true;
  13. }

2.4 读取队头元素

  1. bool GetHead(QueuePtr Q, int *x) {
  2. if (IsEmpty(Q)) return false;
  3. /* 将队头元素存放到x所指向的变量中 */
  4. *x = Q->front->data;
  5. return true;
  6. }

2.5 判空操作

  1. bool IsEmpty(QueuePtr Q) {
  2. /*
  3. * if (Q->front == Q->rear)
  4. * return true;
  5. * else
  6. * return false;
  7. */
  8. return Q->front == Q->rear;
  9. }

2.6 销毁操作 

  1. void ClearQueue(QueuePtr Q) {
  2. LinkQueueNodePtr p = Q->front, q;
  3. /* p在前,q在后 */
  4. while (p != NULL) {
  5. q = p;
  6. p = p->next;
  7. free(q);
  8. }
  9. }

三、循环链表队列

假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点。

  1. /* 定义结点和链表的类型 */
  2. typedef struct _QueueNode {
  3. ElemType data;
  4. struct _QueueNode *next;
  5. } LinkQueueNode, *LinkQueue;
  6. /* 初始化队列 */
  7. bool init_queue(LinkQueue *LQ) {
  8. *LQ = (LinkQueue)malloc(sizeof(LinkQueueNode));
  9. if (*LQ == NULL) return false;
  10. (*LQ)->next = (*LQ);
  11. return true;
  12. }
  13. /* 入队操作 */
  14. bool enter_queue(LinkQueue *LQ, ElemType x) {
  15. LinkQueue p = (LinkQueue)malloc(sizeof(LinkQueueNode));
  16. if (p == NULL) return false;
  17. p->data = x;
  18. p->next = (*LQ)->next;
  19. (*LQ)->next = p;
  20. (*LQ) = p;
  21. return true;
  22. }
  23. /* 出队操作 */
  24. bool leave_queue(LinkQueue *LQ, ElemType *x) {
  25. LinkQueueNode * head = (*LQ)->next, p;
  26. if ((*LQ) == head) return false;
  27. p = head->next;
  28. head->next = p->next;
  29. if (p->next == head)
  30. (*LQ) = head;
  31. *x = p->data;
  32. free(p);
  33. return true;
  34. }
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号