标签: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 20 typedef 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时。
标签:status int init maxsize als one length class ini
原文地址:http://www.cnblogs.com/LuRenJiang/p/6341070.html