其他分享
首页 > 其他分享> > 循环队列

循环队列

作者:互联网

#include<cstdio>
#include <iostream>
#define MAxsize 5
using namespace std;
typedef struct {
    int Data[MAxsize];
    int front;
    int rear;
}SqQueue;
bool InitQueue(SqQueue& L)
{
    L.front = L.rear = 0;
    return true;
}
bool Push(SqQueue& L, int Elem)
{
    if ((L.rear + 1) % MAxsize == L.front) return false;//牺牲一个队列单元,表示队满;
    L.Data[L.rear] = Elem;
    L.rear = (L.rear + 1) % MAxsize;
    return true;
}
bool Pop(SqQueue& L, int& Elem)
{
    if (L.rear==L.front) return false;
    Elem = L.Data[L.front];
    L.front = (L.front + 1) % MAxsize;
    return false;
}
bool QueueEmpty(SqQueue L)
{
    if (L.front == L.rear) return true;
    return false;
}
int main()
{
    int Elem;
    SqQueue L;
    InitQueue(L);
    cin >> Elem;
    while (Elem != 000)
    {
        Push(L, Elem);
        cin >> Elem;
    }
    while (true)
    {
        if (QueueEmpty(L)) break;
        Pop(L, Elem);
        cout << Elem << endl;
    }
    return true;
}

 

标签:return,队列,SqQueue,Elem,int,循环,front,rear
来源: https://www.cnblogs.com/Mexcellent/p/15122131.html