首页 > 代码库 > 循环队列

循环队列

////  main.cpp//  circleQueue////  Created by zhou on 14-6-19.//  Copyright (c) 2014年 zhou. All rights reserved.//#define Max 50typedef struct        //定义大小为50的队列结构体{    int elem[Max];    int front,rear;}Cirqueue;void Init(Cirqueue &q) //初始化{    q.front = q.rear = 0;}bool empty(Cirqueue &q) //队空{    if(q.front == q.rear)        return true;    else        return false;}bool enqueue(Cirqueue &q,int x)//入队{    if((q.rear+1)%Max == q.front)        return false;    q.elem[q.rear] = x;    q.rear = (q.rear+1)%Max;    return true;}bool dequeue(Cirqueue &q,int &x)//出队{    if(empty(q)) return false;    x = q.elem[q.front];    q.front = (q.front+1)%Max;    return true;}#include <iostream>using namespace std;int main(int argc, const char * argv[]){    Cirqueue q;    Init(q);    for(int i = 0;i<10;i++)        enqueue(q, i);    for(int i = 0;i<10;i++)    {        int x;        dequeue(q, x);        cout<<x<<" ";    }    cout<<endl;    return 0;}