(C语言队列的顺序实现(数据结构十)

梦里梦外; 2022-09-17 10:25 244阅读 0赞

1.数据类型定义

在代码中为了清楚的表示一些错误和函数运行状态,我们预先定义一些变量来表示这些状态。在head.h头文件中有如下定义:

  1. //定义数据结构中要用到的一些变量和类型
  2. #ifndef HEAD_H
  3. #define HEAD_H
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <stdlib.h>
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define OK 1
  10. #define ERROR 0
  11. #define INFEASIBLE -1
  12. #define OVERFLOW -2 //分配内存出错
  13. typedef int Status; //函数返回值类型
  14. typedef int ElemType; //用户定义的数据类型
  15. #endif

2.数据结构定义

  1. typedef struct Queue{
  2. ElemType *data;
  3. int head;
  4. int tail;
  5. int capacity;
  6. }Queue,*pQueue;

3.队列顺序实现

LinerQueue.h中实现代码如下:

  1. #ifndef LINARQUEUE_H
  2. #define LINARQUEUE_H
  3. #include "head.h"
  4. #define INIT_QUEUE_CAPACITY 100
  5. #define QUEUE_ICREMENT_SIZE 10
  6. typedef struct Queue{
  7. ElemType *data;
  8. int head;
  9. int tail;
  10. int capacity;
  11. }Queue,*pQueue;
  12. //初始化队列
  13. Status InitQueue(pQueue &Q){
  14. Q=(pQueue)malloc(sizeof(Queue));
  15. if(!Q) return OVERFLOW;
  16. Q->data=(ElemType*)malloc(INIT_QUEUE_CAPACITY*sizeof(ElemType));
  17. if(!Q->data) return OVERFLOW;
  18. Q->head=0;
  19. Q->tail=0;
  20. Q->capacity=INIT_QUEUE_CAPACITY;
  21. return OK;
  22. }
  23. //销毁队列
  24. Status DestroyQueue(pQueue &Q){
  25. free(Q->data);
  26. Q->head=0;
  27. Q->tail=0;
  28. Q->capacity=0;
  29. free(Q);
  30. Q=NULL;
  31. return OK;
  32. }
  33. //清空队列
  34. Status ClearQueue(pQueue &Q){
  35. if(Q==NULL) return ERROR;
  36. Q->head=0;
  37. Q->tail=0;
  38. Q->capacity=INIT_QUEUE_CAPACITY;
  39. return OK;
  40. }
  41. //队列是否为空
  42. Status QueueEmpty(pQueue Q){
  43. if(Q==NULL) return ERROR;
  44. return Q->head==Q->tail;
  45. }
  46. //队列长度
  47. Status QueueLength(pQueue Q){
  48. if(Q==NULL) return ERROR;
  49. return Q->tail-Q->head;
  50. }
  51. //取得队头数据
  52. Status GetHead(pQueue Q,ElemType &e){
  53. if(QueueLength(Q)==0) return ERROR;
  54. e=Q->data[Q->head];
  55. return OK;
  56. }
  57. //从队尾插入数据
  58. Status InsertQueue(pQueue &Q,ElemType e){
  59. if(QueueLength(Q)>=Q->capacity){
  60. Q->data=(ElemType*)realloc(Q->data,(Q->capacity+QUEUE_ICREMENT_SIZE)*sizeof(ElemType));
  61. if(!Q->data) return OVERFLOW;
  62. Q->capacity+=QUEUE_ICREMENT_SIZE;
  63. }
  64. Q->data[Q->tail++]=e;
  65. return OK;
  66. }
  67. //从队头删除数据
  68. Status DeleteQueue(pQueue &Q,ElemType &e){
  69. if(Q==NULL) return ERROR;
  70. GetHead(Q,e);
  71. for (int i=Q->head+1;i<=Q->tail;i++)
  72. {
  73. Q->data[i-1]=Q->data[i];
  74. }
  75. Q->tail--;
  76. return OK;
  77. }
  78. //用(*visit)()遍历队列
  79. Status QueueTraverse(pQueue &Q,Status (*visit)(ElemType)){
  80. for (int i=Q->tail-1;i>=Q->head;i--)
  81. {
  82. (*visit)(Q->data[i]);
  83. }
  84. return OK;
  85. }
  86. Status print(ElemType e){
  87. printf("%d->",e);
  88. return true;
  89. }
  90. //输出队列
  91. Status printQueue(pQueue Q){
  92. if(Q==NULL) return ERROR;
  93. printf("\ntail->");
  94. QueueTraverse(Q,print);
  95. printf("head\n");
  96. return true;
  97. }
  98. #endif

4.测试栈

  1. #include "LinerQueue.h"
  2. void main(){
  3. pQueue Q;
  4. InitQueue(Q);
  5. for (int i=0;i<10;i++)
  6. {
  7. InsertQueue(Q,i);
  8. }
  9. ElemType e2;
  10. DeleteQueue(Q,e2);
  11. printf("\n删除队列头:%d",e2);
  12. printQueue(Q);
  13. printf("\n队列长度:%d",QueueLength(Q));
  14. ElemType e;
  15. GetHead(Q,e);
  16. printf("\n队列头:%d",e);
  17. ClearQueue(Q);
  18. DestroyQueue(Q);
  19. }

5.测试结果

  1. 删除队列头:0
  2. tail->9->8->7->6->5->4->3->2->1->head
  3. 队列长度:9
  4. 队列头:1

发表评论

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

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

相关阅读