队列(C++数组,初学者易懂)

class Queue //定义类队列
{
   public:
       Queue(int size);    //构造函数
       ~Queue();          //析构函数
       void EnQueue(int x);//入队
       int DeQueue();     //出队
       int GetQueue();    //获取队首元素
       bool Isfull();     //判断队列是否为满
       bool Isemputy();   //判断队列是否为空
       void input();      //输出队列中的元素
   private:
       int Queuesize;    //定义队列的长度
       int num;          //表示队列中的元素的数量
    int *data;           //定义data数组
    int front,rear;      //定义队首队尾指针的位置
};

#include"Queue.h"        //先从本文件开始查找,用#include<Queue.h>勉强也行,但是不推荐
#include<iostream>       
using namespace std;

Queue::Queue(int size)  //构造函数,动态分配空间给data数组,初始化变量
{
    Queuesize=size;
    data =new int [Queuesize];
    front=0;
    rear=0;
    num=0;
}
Queue::~Queue()        //析构函数,收回空间
{
    delete []data;
}
void Queue::EnQueue(int x)
{
        data[rear]=x;  //元素入队
   rear=(rear+1)%Queuesize;//防止超出内存
    num++;            //队列中元素+1
}
int Queue::DeQueue()
{
    return front++%Queuesize; //同rear
    num--;           //队列中元素-1
}
int Queue::GetQueue()
{
    cout<<"the first"<<data[front]<<endl; //输出队首元素
    return 0;
}
bool Queue::Isemputy()//判空
{
    return (num==0);  
}
bool Queue::Isfull()//判满
{
    return (num==Queuesize);
}
void Queue::input()//输出队列中的元素
{
    for(int i=0;i<rear;i++)
        cout<<data[i];
}

#include<iostream>
#include"Queue.h"
using namespace std;

int main()
{
   //自己使用代码
    return 0;
}
初学者所见,如有错误,请大佬帮忙指出,谢谢!


猜你喜欢

转载自blog.csdn.net/Mr_Feng07007/article/details/79658906