当前位置:   article > 正文

循环队列的长度、入队、出队和取队头元素_c语言编程实现循环队列的创建、入队、出队和取队头元素;

c语言编程实现循环队列的创建、入队、出队和取队头元素;

一、队列的定义:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXQSIZE 100 //队列可能达到的最大长度
  4. typedef int QElemType;
  5. typedef int Status;
  6. #define OK 1
  7. #define OVERFLOW 0
  8. #define ERROR 0
  9. typedef struct
  10. {
  11. QElemType *base;//存储空间的基地址;
  12. int front;//头指针
  13. int raer;//尾指针
  14. }SqQueue;//循环队列;

二、初始化:

①为队列分配一个容量为MAXQSIZE的数组空间,base指向首地址;

②将头指针和尾指针置为0,表队空;

  1. Status InitQueue(SqQueue &Q)
  2. {//构造一个空队列Q;
  3. Q.base=new QElemType[MAXQSIZE];//为队列分配一个最大容量位MAXQSIZE的数组空间;
  4. if(!Q.base) exit(OVERFLOW);//存储分配失败
  5. Q.front=Q.raer=0;//将头指针和尾指针置为0,队列为空;
  6. return OK;
  7. }

三、求队列长度:

①将头指针减去尾指针加上最大数组空间然后进行取余

  1. void QueueLength(SqQueue Q)
  2. {//返回Q的元素个数,即队列的长度;
  3. int x=(Q.raer-Q.front+MAXQSIZE)%MAXQSIZE;
  4. printf("队列长度为:%d\n",x);
  5. }

四、入队:

①判断队列是否满,若满则返回ERROR;

②将新元素插入队尾;

③队尾元素+1;

  1. Status EnQueue(SqQueue &Q,QElemType e)
  2. {//插入元素为e位Q的新的队尾元素
  3. if((!Q.raer+1)%MAXQSIZE==Q.front)
  4. //若尾指针在循环意义上加1后等于头指针,表明队满
  5. return ERROR;
  6. Q.base[Q.raer]=e;//新元素插入队尾
  7. Q.raer=(Q.raer+1)%MAXQSIZE;//队尾指针加1
  8. return OK;
  9. }

五、出队:

①判断队列是否为空,若空则返回ERROR;

②保存队头元素;

③队头指针+1;

  1. Status DeQueue(SqQueue &Q,QElemType &e)
  2. {//删除Q的队头元素,用 e返回其值
  3. if(Q.front==Q.raer) return ERROR;//队空 ;
  4. e=Q.base[Q.front];//保存队头元素 ;
  5. printf("将元素%d出队\n",e);
  6. Q.front=(Q.front+1)%MAXQSIZE;//队头指针加1
  7. return OK;
  8. }

六、取队头元素:

①Q.base为队头序号,直接利用Q.base输出;

  1. QElemType GetHead(SqQueue Q)
  2. {//返回Q的队头元素,不修改队头指针
  3. printf("队头元素为:");
  4. if(Q.front!=Q.raer) //队列非空
  5. return Q.base[Q.front];//返回
  6. }

七、主函数:

  1. int main()
  2. {
  3. SqQueue Q;
  4. int x,e;
  5. InitQueue(Q);
  6. EnQueue(Q,0);
  7. EnQueue(Q,1);
  8. EnQueue(Q,2);
  9. EnQueue(Q,3);
  10. QueueLength(Q);
  11. DeQueue(Q,e);
  12. DeQueue(Q,e);
  13. QueueLength(Q);
  14. int k=GetHead(Q) ;
  15. printf("%d",k);
  16. return 0;
  17. }

八、完整代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXQSIZE 100 //队列可能达到的最大长度
  4. typedef int QElemType;
  5. typedef int Status;
  6. #define OK 1
  7. #define OVERFLOW 0
  8. #define ERROR 0
  9. typedef struct
  10. {
  11. QElemType *base;//存储空间的基地址;
  12. int front;//头指针
  13. int raer;//尾指针
  14. }SqQueue;//循环队列;
  15. Status InitQueue(SqQueue &Q)
  16. {//构造一个空队列Q;
  17. Q.base=new QElemType[MAXQSIZE];//为队列分配一个最大容量位MAXQSIZE的数组空间;
  18. if(!Q.base) exit(OVERFLOW);//存储分配失败
  19. Q.front=Q.raer=0;//将头指针和尾指针置为0,队列为空;
  20. return OK;
  21. }
  22. void QueueLength(SqQueue Q)
  23. {//返回Q的元素个数,即队列的长度;
  24. int x=(Q.raer-Q.front+MAXQSIZE)%MAXQSIZE;
  25. printf("队列长度为:%d\n",x);
  26. }
  27. Status EnQueue(SqQueue &Q,QElemType e)
  28. {//插入元素为e位Q的新的队尾元素
  29. if((!Q.raer+1)%MAXQSIZE==Q.front)
  30. //若尾指针在循环意义上加1后等于头指针,表明队满
  31. return ERROR;
  32. Q.base[Q.raer]=e;//新元素插入队尾
  33. Q.raer=(Q.raer+1)%MAXQSIZE;//队尾指针加1
  34. return OK;
  35. }
  36. Status DeQueue(SqQueue &Q,QElemType &e)
  37. {//删除Q的队头元素,用 e返回其值
  38. if(Q.front==Q.raer) return ERROR;//队空 ;
  39. e=Q.base[Q.front];//保存队头元素 ;
  40. printf("将元素%d出队\n",e);
  41. Q.front=(Q.front+1)%MAXQSIZE;//队头指针加1
  42. return OK;
  43. }
  44. QElemType GetHead(SqQueue Q)
  45. {//返回Q的队头元素,不修改队头指针
  46. printf("队头元素为:");
  47. if(Q.front!=Q.raer) //队列非空
  48. return Q.base[Q.front];//返回
  49. }
  50. int main()
  51. {
  52. SqQueue Q;
  53. int x,e;
  54. InitQueue(Q);
  55. EnQueue(Q,0);
  56. EnQueue(Q,1);
  57. EnQueue(Q,2);
  58. EnQueue(Q,3);
  59. QueueLength(Q);
  60. DeQueue(Q,e);
  61. DeQueue(Q,e);
  62. QueueLength(Q);
  63. int k=GetHead(Q) ;
  64. printf("%d",k);
  65. return 0;
  66. }

九、运行结果:

 

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

闽ICP备14008679号