循环顺序队列c++实现

来源:互联网 发布:淘宝高端fake鞋店铺 编辑:程序博客网 时间:2024/05/16 09:08

一种重要的线性表叫做队列,是一种标准的FIFO类型的,就是先进先出,在计算机领域,这种数据结构有很广泛的应用,有一种一般形式的数组的队列实现,但是这种队列耗费空间,今天给大家介绍一种充分利用空间的队列,循环队列。


#include <iostream>

#include <cstdio>
#include <cstring>
#include <cstdlib>


using namespace std;


//循环队列最多100个元素
#define MAX 100
//循环队列的数据类型
typedef int QueueType;


class Queue
{
private:
    QueueType data[MAX];
    int front;//指向头部的下标
    int rear;//指向尾部的后一个,当rear=front的时候就代表队列为空
public:
    Queue();
    bool Empty();
    bool Full();
    bool DeQueue(QueueType &e);
    bool EnQueue(QueueType e);
    bool GetTop(QueueType &e);
};


Queue::Queue()
{
    this->front = this->rear = 0;
}
bool Queue::Empty()
{
    if(this->rear == this->front)
        return true;
    return false;
}
bool Queue::Full()
{
    if((this->rear+1)%MAX == this->front)
        return true;
    return false;
}
bool Queue::DeQueue(QueueType &e)
{
    if(Empty())
        return false;
    e = this->data[this->front];
    this->front = (this->front+1)%(MAX);
    return true;
}
bool Queue::EnQueue(QueueType e)
{
    if(Full())
        return false;
    this->data[this->rear] = e;
    this->rear = (this->rear+1)%MAX;
    return true;
}
bool Queue::GetTop(QueueType &e)
{
    if(Empty())
        return false;
    e = this->data[this->front];
    return true;
}


int main()
{
    return 0;
}
0 0
原创粉丝点击