zoukankan      html  css  js  c++  java
  • 循环队列

    1. #include "stdio.h"
    2. #include "stdlib.h"
    3. #include "math.h"
    4. #include "time.h"
    5. #define OK 1
    6. #define ERROR 0
    7. #define TRUE 1
    8. #define FALSE 0
    9. #define MAXSIZE 20 /* 存储空间初始分配量 */
    10. typedef int Status;
    11. typedef int QElemType; /* QElemType 类型根据实际情况而定,这里假设为 int */
    12. /* 循环队列的顺序存储结构 */
    13. typedef struct {
    14. QElemType data[MAXSIZE];
    15. int front;/* 头指针 */
    16. int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
    17. } SqQueue;
    18. /* 打印 */
    19. Status visit(QElemType c) {
    20. printf("%d ", c);
    21. return OK;
    22. }
    23. /* 初始化一个空队列 Q */
    24. /* 存储栈的数组已经定义,所以这里只操作指针。 */
    25. Status InitQueue(SqQueue *Q) {
    26. Q->front = 0;
    27. Q->rear = 0;
    28. return OK;
    29. }
    30. /* 将 Q 清为空队列 */
    31. Status ClearQueue(SqQueue *Q) {
    32. Q->front = Q->rear = 0;
    33. return OK;
    34. }
    35. /* 若队列 Q 为空队列,则返回 TRUE,否则返回 FALSE */
    36. Status QueueEmpty(SqQueue Q) {
    37. if (Q.front == Q.rear) /* 队列空的标志 */
    38. return TRUE;
    39. else
    40. return FALSE;
    41. }
    42. /* 返回 Q 的元素个数,也就是队列的当前长度 */
    43. int QueueLength(SqQueue Q) {
    44. return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
    45. /*
    46. c语言中的%可用于模运算,即求余数。
    47. 表达式 a % b 就是计算 a 除以 b 得出的余数。
    48. 例如: 4 % 3 的结果是 1
    49. */
    50. }
    51. /* 若队列不空,则用 e 返回 Q 的队头元素,并返回 OK,否则返回 ERROR */
    52. Status GetHead(SqQueue Q, QElemType *e) {
    53. if (Q.front == Q.rear) /* 队列空 */
    54. return ERROR;
    55. *e = Q.data[Q.front];
    56. return OK;
    57. }
    58. /* 若队列未满,则插入元素 e 为 Q 新的队尾元素 */
    59. Status EnQueue(SqQueue *Q, QElemType e) {
    60. if ((Q->rear + 1) % MAXSIZE == Q->front) /* 队列满的判断 */
    61. return ERROR;
    62. Q->data[Q->rear] = e;
    63. /* 将元素 e 赋值给队尾 */
    64. Q->rear = (Q->rear + 1) % MAXSIZE;/* rear 指针向后移一位置, */
    65. /* 若到最后则转到数组头部 */
    66. return OK;
    67. }
    68. /* 若队列不空,则删除 Q 中队头元素,用 e 返回其值 */
    69. Status DeQueue(SqQueue *Q, QElemType *e) {
    70. if (Q->front == Q->rear)
    71. return ERROR;
    72. /* 队列空的判断 */
    73. *e = Q->data[Q->front];
    74. /* 将队头元素赋值给 e */
    75. Q->front = (Q->front + 1) % MAXSIZE; /* front 指针向后移一位置, */
    76. /* 若到最后则转到数组头部 */
    77. return OK;
    78. }
    79. /* 从队头到队尾依次对队列 Q 中每个元素输出 */
    80. Status QueueTraverse(SqQueue Q) {
    81. int i;
    82. i = Q.front;
    83. while ((i + Q.front) != Q.rear) {
    84. visit(Q.data[i]);
    85. i = (i + 1) % MAXSIZE;
    86. }
    87. printf(" ");
    88. return OK;
    89. }
    90. int main() {
    91. Status j;
    92. int i = 0, l;
    93. QElemType d;
    94. SqQueue Q;
    95. InitQueue(&Q);
    96. printf("初始化队列后,队列空否?%u(1:空 0:否) ", QueueEmpty(Q));
    97. printf("请输入整型队列元素(不超过%d 个),-1 为提前结束符: ", MAXSIZE - 1);
    98. do {
    99. /* scanf("%d",&d); */
    100. d = i + 100;
    101. if (d == -1)
    102. break;
    103. i++;
    104. EnQueue(&Q, d);
    105. } while (i < MAXSIZE - 1);
    106. printf("队列长度为: %d ", QueueLength(Q));
    107. printf("现在队列空否?%u(1:空 0:否) ", QueueEmpty(Q));
    108. printf("连续%d 次由队头删除元素,队尾插入元素: ", MAXSIZE);
    109. for (l = 1; l <= MAXSIZE; l++) {
    110. DeQueue(&Q, &d);
    111. printf("删除的元素是%d,插入的元素:%d ", d, l + 1000);
    112. /* scanf("%d",&d); */
    113. d = l + 1000;
    114. EnQueue(&Q, d);
    115. }
    116. l = QueueLength(Q);
    117. printf("现在队列中的元素为: ");
    118. QueueTraverse(Q);
    119. printf("共向队尾插入了%d 个元素 ", i + MAXSIZE);
    120. if (l - 2 > 0)
    121. printf("现在由队头删除%d 个元素: ", l - 2);
    122. while (QueueLength(Q) > 2) {
    123. DeQueue(&Q, &d);
    124. printf("删除的元素值为%d ", d);
    125. }
    126. j = GetHead(Q, &d);
    127. if (j)
    128. printf("现在队头元素为: %d ", d);
    129. ClearQueue(&Q);
    130. printf("清空队列后, 队列空否?%u(1:空 0:否) ", QueueEmpty(Q));
    131. return 0;
    132. }





  • 相关阅读:
    decimal赋值
    "errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [d0tQ_02368635
    scope.row中属性值展示
    echarts.baidu 官网
    PowerDesigner使用
    解决 office2016 意外的严重错误 不能启动程序
    Jenkins 关闭和重启
    解决 weblogic poi3.9 报错 a different type with name "javax/xml/namespace/QName"
    WINDOWS7 下 xclient 802.1x 客户端 停止运行的解决办法
    前台进程转后台运行
  • 原文地址:https://www.cnblogs.com/zhuzhenfeng/p/4626556.html
Copyright © 2011-2022 走看看