当前位置:   article > 正文

数据结构之循环队列(C语言)_数据结构循环队列代码

数据结构循环队列代码

一、循环队列的定义

循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。循环队列就是将队列存储空间的最后一个位置绕到第一个位置,形成逻辑上的环状空间,供队列循环使用。

二、代码功能

1、创建循环队列

2、循环队列的初始化

3、循环队列元素的入队

4、循环队列元素的出队

5、循环队列的输出

6、循环队列的测试

7、程序入口

8、运行结果


 

三、循环队列的代码

1、创建循环队列

  1. typedef struct CircleIntQueue
  2. {
  3. int data[TOTAL_SPACE];
  4. int head;
  5. int tail;
  6. }*CircleIntQueuePtr;

2、循环队列的初始化

  1. CircleIntQueuePtr initQueue()
  2. {
  3. CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
  4. resultPtr->head = 0;
  5. resultPtr->tail = 0;
  6. return resultPtr;
  7. }

3、循环队列元素的入队

  1. void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
  2. {
  3. if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head) {
  4. printf("Queue full.\r\n");
  5. return;
  6. } // Of if
  7. paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
  8. paraPtr->tail++;
  9. }

4、循环队列元素的出队

  1. int dequeue(CircleIntQueuePtr paraPtr)
  2. {
  3. int resultValue;
  4. if (paraPtr->head == paraPtr->tail) {
  5. printf("No element in the queue.\r\n");
  6. return -1;
  7. } // Of if
  8. resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
  9. paraPtr->head++;
  10. return resultValue;
  11. }

5、循环队列的输出

  1. void outputLinkQueue(CircleIntQueuePtr paraPtr)
  2. {
  3. int i;
  4. if (paraPtr->head == paraPtr->tail)
  5. {
  6. printf("Empty queue.");
  7. return;
  8. } // Of if
  9. printf("Elements in the queue: ");
  10. for (i = paraPtr->head; i < paraPtr->tail; i++)
  11. {
  12. printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
  13. } // Of for i
  14. printf("\r\n");
  15. }

6、循环队列的测试

  1. void testLinkQueue()
  2. {
  3. int i = 10;
  4. CircleIntQueuePtr tempPtr = initQueue();
  5. for (; i < 16; i ++)
  6. {
  7. enqueue(tempPtr, i);
  8. }//Of for i
  9. outputLinkQueue(tempPtr);
  10. for (i = 0; i < 6; i ++)
  11. {
  12. printf("dequeue gets %d\r\n", dequeue(tempPtr));
  13. }//Of for i
  14. enqueue(tempPtr, 8);
  15. outputLinkQueue(tempPtr);
  16. }

7、程序入口

  1. int main()
  2. {
  3. testLinkQueue();
  4. return 1;
  5. }

8、运行结果

  1. Queue full.
  2. Queue full.
  3. Elements in the queue: 10, 11, 12, 13,
  4. dequeue gets 10
  5. dequeue gets 11
  6. dequeue gets 12
  7. dequeue gets 13
  8. No element in the queue.
  9. dequeue gets -1
  10. No element in the queue.
  11. dequeue gets -1
  12. Elements in the queue: 8,

四、整体代码

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define TOTAL_SPACE 5
  4. /**
  5. * Circle int queue.
  6. */
  7. typedef struct CircleIntQueue
  8. {
  9. int data[TOTAL_SPACE];
  10. int head;
  11. int tail;
  12. }*CircleIntQueuePtr;
  13. /**
  14. * Initialize the queue.
  15. */
  16. CircleIntQueuePtr initQueue()
  17. {
  18. CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
  19. resultPtr->head = 0;
  20. resultPtr->tail = 0;
  21. return resultPtr;
  22. }// Of the first constructor
  23. /**
  24. * Enqueue.
  25. *
  26. * @param paraValue The value of the new node.
  27. */
  28. void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
  29. {
  30. if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head) {
  31. printf("Queue full.\r\n");
  32. return;
  33. } // Of if
  34. paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
  35. paraPtr->tail++;
  36. }// Of enqueue
  37. /**
  38. * Dequeue.
  39. *
  40. * @return The value at the head.
  41. */
  42. int dequeue(CircleIntQueuePtr paraPtr)
  43. {
  44. int resultValue;
  45. if (paraPtr->head == paraPtr->tail) {
  46. printf("No element in the queue.\r\n");
  47. return -1;
  48. } // Of if
  49. resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
  50. paraPtr->head++;
  51. return resultValue;
  52. }// Of dequeue
  53. /**
  54. * Output the queue.
  55. */
  56. void outputLinkQueue(CircleIntQueuePtr paraPtr)
  57. {
  58. int i;
  59. if (paraPtr->head == paraPtr->tail)
  60. {
  61. printf("Empty queue.");
  62. return;
  63. } // Of if
  64. printf("Elements in the queue: ");
  65. for (i = paraPtr->head; i < paraPtr->tail; i++)
  66. {
  67. printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
  68. } // Of for i
  69. printf("\r\n");
  70. }//Of outputLinkQueue
  71. /**
  72. * Unit test.
  73. */
  74. void testLinkQueue()
  75. {
  76. int i = 10;
  77. CircleIntQueuePtr tempPtr = initQueue();
  78. for (; i < 16; i ++)
  79. {
  80. enqueue(tempPtr, i);
  81. }//Of for i
  82. outputLinkQueue(tempPtr);
  83. for (i = 0; i < 6; i ++)
  84. {
  85. printf("dequeue gets %d\r\n", dequeue(tempPtr));
  86. }//Of for i
  87. enqueue(tempPtr, 8);
  88. outputLinkQueue(tempPtr);
  89. }//Of testLinkQueue
  90. /**
  91. * The entrance.
  92. */
  93. int main()
  94. {
  95. testLinkQueue();
  96. return 1;
  97. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/641255
推荐阅读
相关标签
  

闽ICP备14008679号