#include<iostream>
using namespace std;

typedef int ElemType;

#define MAXSIZE 5
typedef struct
{
    ElemType data[MAXSIZE];
    int front;
    int rear;
}SqQueue;


void init(SqQueue &q)
{
    q.front = q.rear = 0;
}

bool empty(SqQueue &q)
{
    if(q.front == q.rear)
        return 1;
    else
        return 0;
}

bool enqueue(SqQueue &q,ElemType x)
{
    if((q.rear + 1) % MAXSIZE == q.front)
        return 0;
    q.data[q.rear] = x;
    q.rear = (q.rear + 1) % MAXSIZE;
    return 1;
}

ElemType dequeue(SqQueue &q)
{
    if(q.front == q.rear)
        return INT_MAX;
    ElemType x = q.data[q.front];
    q.front = (q.front + 1) % MAXSIZE;
    return x;
}

ElemType size(SqQueue &q)
{
    return q.rear - q.front;
}

ElemType back(SqQueue q)
{
    return q.data[q.rear-1];
}

int main()
{
    SqQueue q;
    init(q);
    enqueue(q,1);
    enqueue(q,3);
    enqueue(q,5);
    enqueue(q,7);
    enqueue(q,9);
    enqueue(q,10);
    cout<<q.front<<endl;
    cout<<q.rear<<endl;
    cout<<size(q)<<endl;    //牺牲了一个空间,所以最大size是MAXSIZE-1
    cout<<dequeue(q)<<endl;
    cout<<dequeue(q)<<endl;
    cout<<dequeue(q)<<endl;
    cout<<dequeue(q)<<endl;
    cout<<dequeue(q)<<endl;
    cout<<back(q)<<endl;
    cout<<empty(q)<<endl;
    cout<<q.front<<endl;
    cout<<q.rear<<endl;
    system("pause");
    return 0;
}

版权声明:本文为akmfwei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/akmfwei/p/13268691.html