#include <stdio.h>#include<stdlib.h>#defineOK 1#defineERROR 0#defineTRUE 1#defineFALSE 0#defineMAXSIZE 20typedefintStatus;typedefintQelemtype;typedefstruct{qelemtype data[maxsize]; intFront; intRear;} Sqqueue; Status Initqueue (Sqqueue*p); Status queuelength (Sqqueue Q); Status EnQueue (Sqqueue*q,qelemtype e); Status DeQueue (Sqqueue*q,qelemtype *e); Status Queuetraverse (Sqqueue Q);intMain () {Sqqueue q; intN; Initqueue (&q); EnQueue (&q,666); Queuetraverse (q); printf ("%d\n", DeQueue (&q,&N)); printf ("%d\n", N); return 0;} Status Initqueue (Sqqueue*p) {Q->front=0; Q->rear=0; returnOK;} Status queuelength (Sqqueue Q) {return(q.rear-q.front+maxsize)%MAXSIZE;} Status EnQueue (Sqqueue*Q,qelemtype e) { if((q->rear+1)%maxsize==q->front)returnERROR; Q->data[q->rear]=e; Q->rear= (q->rear+1)%maxsize;//forgot to think about rear==maxsize-1;!!!!!!!!!!. returnOK;} Status DeQueue (Sqqueue*q,qelemtype *e) { if(q->front==q->rear)returnERROR; *e=q->data[q->Front]; Q->front= (q->front+1)%MAXSIZE; returnOK;} Status Queuetraverse (Sqqueue Q) {inth; H=Q.front; while(h!=q.rear) {printf ("%d", Q.data[h]); H= (H +1)%MAXSIZE; } returnOK;}
Actual BUG:
1.QueueTraverse (Q), does not define H, the Q.front as a variable, resulting in, the q.front subscript changed/(ㄒoㄒ)/~~
2.q->rear changes should be considered when special circumstances such as rear==maxsize-1 .
(source code see Big Data structure) linear table-sequential storage structure of circular queue