순환 대기 열

4816 단어 대열
//

//  main.cpp

//  circleQueue

//

//  Created by zhou on 14-6-19.

//  Copyright (c) 2014  zhou. All rights reserved.

//

#define Max 50

typedef 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;

}

좋은 웹페이지 즐겨찾기