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

约定不等于承诺〃 2022-09-19 01:30 249阅读 0赞
  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. }

发表评论

表情:
评论列表 (有 0 条评论,249人围观)

还没有评论,来说两句吧...

相关阅读

    相关 基本操作 数据结构

    队列(queue)也是线性表的一种特殊情况,其所有的插入均限定在表的一端进行,而所有的删除则限定在表的另一端进行。允许插入的一端称队尾(rear),允许删除的一端称队头(fro

    相关 8584 循环基本操作

    题目描述 Description 创建一个空的循环队列,并实现入队、出队、返回队列的长度、返回队头元素、队列的遍历等基本算法。请将下面的程序补充完整。 \includ

    相关 数据结构系列-基本操作

    队列(Queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。允许插入的端是队尾,允许删除的端是队头。 所以说队列是一个先进先出的线性表,相应的也有顺序存储和

    相关 循环基本操作

    循环队列设置不同队空与队满条件的解决方案 为了解决顺序队列假溢出的问题,我们采用的方案是少用一个元素空间,此时的指针状态是队尾指针加1才与队头指针重合,于是此时队满的判定