【数据结构】顺序队列_Queue

前端之家收集整理的这篇文章主要介绍了【数据结构】顺序队列_Queue前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "io.h"
  4. #include "math.h"
  5. #include "time.h"
  6.  
  7. #define OK 1
  8. #define ERROR 0
  9. #define TRUE 1
  10. #define FALSE 0
  11. #define MAXSIZE 20 /* 存储空间初始分配量 */
  12.  
  13. typedef int Status;
  14. typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */
  15.  
  16. /* 循环队列的顺序存储结构 */
  17. typedef struct
  18. {
  19. QElemType data[MAXSIZE];
  20. int front; /* 头指针 */
  21. int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
  22. }SqQueue;
  23.  
  24. Status visit(QElemType c)
  25. {
  26. printf("%d ",c);
  27. return OK;
  28. }
  29.  
  30. /* 初始化一个空队列Q */
  31. Status InitQueue(SqQueue *Q)
  32. {
  33. Q->front=0;
  34. Q->rear=0;
  35. return OK;
  36. }
  37.  
  38. /* 将Q清为空队列 */
  39. Status ClearQueue(SqQueue *Q)
  40. {
  41. Q->front=Q->rear=0;
  42. return OK;
  43. }
  44.  
  45. /* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
  46. Status QueueEmpty(SqQueue Q)
  47. {
  48. if(Q.front==Q.rear) /* 队列空的标志 */
  49. return TRUE;
  50. else
  51. return FALSE;
  52. }
  53.  
  54. /* 返回Q的元素个数,也就是队列的当前长度 */
  55. int QueueLength(SqQueue Q)
  56. {
  57. return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
  58. }
  59.  
  60. /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
  61. Status GetHead(SqQueue Q,QElemType *e)
  62. {
  63. if(Q.front==Q.rear) /* 队列空 */
  64. return ERROR;
  65. *e=Q.data[Q.front];
  66. return OK;
  67. }
  68.  
  69. /* 若队列未满,则插入元素e为Q新的队尾元素 */
  70. Status EnQueue(SqQueue *Q,QElemType e)
  71. {
  72. if ((Q->rear+1)%MAXSIZE == Q->front) /* 队列满的判断 */
  73. return ERROR;
  74. Q->data[Q->rear]=e; /* 将元素e赋值给队尾 */
  75. Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
  76. /* 若到最后则转到数组头部 */
  77. return OK;
  78. }
  79.  
  80. /* 若队列不空,则删除Q中队头元素,用e返回其值 */
  81. Status DeQueue(SqQueue *Q,QElemType *e)
  82. {
  83. if (Q->front == Q->rear) /* 队列空的判断 */
  84. return ERROR;
  85. *e=Q->data[Q->front]; /* 将队头元素赋值给e */
  86. Q->front=(Q->front+1)%MAXSIZE; /* front指针向后移一位置, */
  87. /* 若到最后则转到数组头部 */
  88. return OK;
  89. }
  90.  
  91. /* 从队头到队尾依次对队列Q中每个元素输出 */
  92. Status QueueTraverse(SqQueue Q)
  93. {
  94. int i;
  95. i=Q.front;
  96. while((i+Q.front)!=Q.rear)
  97. {
  98. visit(Q.data[i]);
  99. i=(i+1)%MAXSIZE;
  100. }
  101. printf("\n");
  102. return OK;
  103. }
  104.  
  105. int main()
  106. {
  107. Status j;
  108. int i=0,l;
  109. QElemType d;
  110. SqQueue Q;
  111. InitQueue(&Q);
  112. printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
  113.  
  114. printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ",MAXSIZE-1);
  115. do
  116. {
  117. /* scanf("%d",&d); */
  118. d=i+100;
  119. if(d==-1)
  120. break;
  121. i++;
  122. EnQueue(&Q,d);
  123. }while(i<MAXSIZE-1);
  124.  
  125. printf("队列长度为: %d\n",QueueLength(Q));
  126. printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
  127. printf("连续%d次由队头删除元素,队尾插入元素:\n",MAXSIZE);
  128. for(l=1;l<=MAXSIZE;l++)
  129. {
  130. DeQueue(&Q,&d);
  131. printf("删除的元素是%d,插入的元素:%d \n",d,l+1000);
  132. /* scanf("%d",&d); */
  133. d=l+1000;
  134. EnQueue(&Q,d);
  135. }
  136. l=QueueLength(Q);
  137.  
  138. printf("现在队列中的元素为: \n");
  139. QueueTraverse(Q);
  140. printf("共向队尾插入了%d个元素\n",i+MAXSIZE);
  141. if(l-2>0)
  142. printf("现在由队头删除%d个元素:\n",l-2);
  143. while(QueueLength(Q)>2)
  144. {
  145. DeQueue(&Q,&d);
  146. printf("删除的元素值为%d\n",d);
  147. }
  148.  
  149. j=GetHead(Q,&d);
  150. if(j)
  151. printf("现在队头元素为: %d\n",d);
  152. ClearQueue(&Q);
  153. printf("清空队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
  154. return 0;
  155. }
  156.  

猜你在找的数据结构相关文章