数据结构 循环队列的基本操作

举报
悦来客栈的老板 发表于 2020/12/30 01:06:25 2020/12/30
【摘要】 #include <stdio.h>#include <stdlib.h> #define MAXQSIZE 1000#define OK 1#define ERROR 0 #define OVERFLOW -2 typedef int QElemType; typedef struct{ QElemType *base; int front; i...

  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXQSIZE 1000
  4. #define OK 1
  5. #define ERROR 0
  6. #define OVERFLOW -2
  7. typedef int QElemType;
  8. typedef struct
  9. {
  10. QElemType *base;
  11. int front;
  12. int rear;
  13. }SqQueue;
  14. int InitQueue(SqQueue &Q)
  15. {
  16. Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
  17. if (!Q.base)
  18. {
  19. exit(OVERFLOW);
  20. }
  21. Q.front = Q.rear = 0;
  22. return OK;
  23. }
  24. int QueueLength(SqQueue Q)
  25. {
  26. return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
  27. }
  28. int EnQueue(SqQueue &Q, QElemType e)
  29. {
  30. if ((Q.rear +1) % MAXQSIZE == Q.front)
  31. {
  32. return ERROR;
  33. }
  34. Q.base[Q.rear] = e;
  35. Q.rear = (Q.rear +1) % MAXQSIZE;
  36. return OK;
  37. }
  38. int DeQueue (SqQueue &Q, QElemType &e)
  39. {
  40. if (Q.front == Q.rear )
  41. {
  42. return ERROR;
  43. }
  44. e = Q.base [Q.front];
  45. Q.front = (Q.front+1) % MAXQSIZE;
  46. return OK;
  47. }
  48. int isQueueEmpty(SqQueue Q)
  49. {//队列是否为空
  50. if (Q.front == Q.rear )
  51. {
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. int isQueueFull(SqQueue Q)
  57. {
  58. if ((Q.rear + 1 ) % MAXQSIZE == Q.front)
  59. {
  60. return OK;
  61. }
  62. return ERROR;
  63. }
  64. void Print_Q(SqQueue Q)
  65. {
  66. int i;
  67. if ((Q.rear +1) % MAXQSIZE == Q.front)
  68. {
  69. return;
  70. }
  71. for (i=0; i<QueueLength(Q); i++)
  72. {
  73. printf("%d ",Q.base[Q.front+i]);
  74. }
  75. printf("\n");
  76. }
  77. int GetHead(SqQueue &Q, QElemType &e)
  78. {
  79. if (Q.front == Q.rear )
  80. {
  81. return ERROR;
  82. }
  83. e = Q.base[Q.front];
  84. return OK;
  85. }
  86. int main()
  87. {
  88. SqQueue Q;
  89. int i,n;
  90. InitQueue(Q);
  91. scanf("%d",&n);
  92. for (i=1; i<=n; i++)
  93. {
  94. EnQueue(Q,i);
  95. }
  96. Print_Q(Q);
  97. DeQueue(Q,n);
  98. DeQueue(Q,n);
  99. DeQueue(Q,n);
  100. DeQueue(Q,n);
  101. Print_Q(Q);
  102. GetHead(Q,i);
  103. printf("%d\n",i);
  104. return 0;
  105. }

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/17249153

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。