循环顺序队列

#include <iostream>
using namespace std;
#define MAX 4
typedef struct queuearr
{
    int arr[MAX];
    int cap;
    int size;
    int head;
    int tail;
}QUEUE;
QUEUE* create_queue()
{
    QUEUE* queue = new QUEUE;
    queue->cap = MAX;
    queue->size = 0;
    queue->head = 0;
    queue->tail = 0;
}
void queue_push(QUEUE* queue, int data)
{
    if(queue->cap == queue->tail)
    {
        //cout<<"has been full"<<endl;
        queue->tail = 0;
    }
    queue->size++;
    queue->arr[queue->tail++] =data;
}
int queue_pop(QUEUE* queue)
{
    if(queue->cap ==queue->head)
    {
        //cout<<"has been empty"<<endl;
        queue->head = 0;
    }
    queue->size--;
    return queue->arr[queue->head++];
}
bool queue_full(QUEUE* queue)
{
    if(queue->size==queue->cap)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool queue_empty(QUEUE*  queue)
{
    if(queue->size ==0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void destroy_queue(QUEUE* queue)
{
    delete queue;
}
int main()
{
    QUEUE* queue = create_queue();
    for(int i=10;i<=40;i+=10)
    {
        if(!queue_full(queue))
        {
            queue_push(queue,i);
        }
    }
    for(int i=0; i<2;i++)
    {
        if(!queue_empty(queue))
        {
            cout<<queue_pop(queue)<<" ";
        }
    }
    cout<<endl;
    for(int i=50;i<=60;i+=10)
    {
        if(!queue_full(queue))
        {
            queue_push(queue,i);
        }
    }
    for(int i=0; i<4;i++)
    {
        if(!queue_empty(queue))
        {
            cout<<queue_pop(queue)<<" ";
        }
    }
    cout<<endl;
    return 0;
}

上一篇:分布式系统中的CAP 理论


下一篇:队列操作的实现