標籤:status int init maxsize als one length class ini
#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 20typedef int Status;typedef int QElemType;typedef struct{ QElemType data[MAXSIZE]; int front; int rear;}SqQueue;Status InitQueue(SqQueue *Q);Status QueueLength(SqQueue Q);Status EnQueue(SqQueue *Q,QElemType e);Status DeQueue(SqQueue *Q,QElemType *e);Status QueueTraverse(SqQueue Q);int main(){ SqQueue q; int n; InitQueue(&q); EnQueue(&q,666); QueueTraverse(q); printf("%d\n",DeQueue(&q,&n)); printf("%d\n",n); return 0;}Status InitQueue(SqQueue *Q){ Q->front=0; Q->rear=0; return OK;}Status QueueLength(SqQueue Q){ return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;}Status EnQueue(SqQueue *Q,QElemType e){ if((Q->rear+1)%MAXSIZE==Q->front) return ERROR; Q->data[Q->rear]=e; Q->rear=(Q->rear+1)%MAXSIZE;//忘了考慮rear==MAXSIZE-1;!!!!!!!!!! return OK;}Status DeQueue(SqQueue *Q,QElemType *e){ if(Q->front==Q->rear) return ERROR; *e=Q->data[Q->front]; Q->front=(Q->front+1)%MAXSIZE; return OK;}Status QueueTraverse(SqQueue Q){ int h; h=Q.front; while(h!=Q.rear) { printf("%d ",Q.data[h]); h=(h+1)%MAXSIZE; } return OK;}
實戰BUG:
1.QueueTraverse(Q),沒有定義h,把Q.front當變數了,結果導致,Q.front的下標改變了/(ㄒoㄒ)/~~
2.Q->rear改變時要考慮特殊情況如rear==MAXSIZE-1時。
(原始碼見大話資料結構)線性表—迴圈隊列的順序儲存結構