问题:主要是队列为空和满的条件.
为空:cqueue->front==cqueue->rear 为满(cqueue->rear+1)==cqueue->front
代码:
#include#include using namespace std;#define MAXSIZE 20typedef struct CQueue{ int front; int rear; int data[MAXSIZE];}*CirQueue;void initCQueue(CirQueue &cqueue){ cqueue=(CirQueue)malloc(sizeof(struct CQueue)); if(!cqueue) { cout<<"allocate fail"< front=cqueue->rear=0; }}void enCQueue(CirQueue cqueue,int elem){ if((cqueue->rear+1)%MAXSIZE==cqueue->front) { cout<<"循环队列已满"< data[cqueue->rear%MAXSIZE]=elem; cqueue->rear++; }}int deCQueue(CirQueue cqueue){ int elem; if(cqueue->front==cqueue->rear) { cout<<"队列已空"< data[cqueue->front%MAXSIZE]; cqueue->front++; } return elem;}int main(){ CirQueue cqueue; int arr[]={1,2,3,4,5,6,7,9,0}; initCQueue(cqueue); cout<<"循环队列入队:1 2 3 4 5 6 7 9 0:"< front!=cqueue->rear) { cout< <<" "; } cout<
运行结果: