顺序环形队列的各种基本运算
#include<iostream> using namespace std; #define MaxSize 50 typedef char ElemType; typedef struct { ElemType data[MaxSize]; int front ,rear; }SqQueue; void InitQueue(SqQueue *&q) { q=(SqQueue *)malloc (sizeof(SqQueue)); q->front=q->rear=0; } void ClearQueue(SqQueue *&q) { free(q); } int QueueEmpty(SqQueue *&q) { return (q->rear==q->front); } int enQueue(SqQueue * &q,ElemType e) { if(q->rear+1%MaxSize==q->front) //队满 return 0; q->rear=(q->rear+1)%MaxSize; q->data[q->rear]=e; return 1; } int deQueue(SqQueue *&q,ElemType &e) { if(q->rear==q->front) //队空 return 0; q->front=(q->front+1)%MaxSize; e=q->data[q->front]; return 1; } void main() { ElemType e='0'; SqQueue *q; cout<<"初始化队列;"<<endl; InitQueue(q); cout<<"判断队列是否为空:"; if(QueueEmpty(q)) cout<<"空;"<<endl; else cout<<"非空;"<<endl; cout<<"将元素a,b,c进队:"<<endl; enQueue(q,'a'); enQueue(q,'b'); enQueue(q,'c'); deQueue(q,e); cout<<"出队元素为:"<<e<<endl; ClearQueue(q); }