queue的C++实现

本人小白一枚,只想以此不断激励自己前行
#ifndef _QUEUE_H
#define _QUEUE_H

#include <iostream>
using namespace std;

typedef int ElementType;

class Queue
{
private:
	int Rear;
	int Size;
	int Capacity;
	ElementType *Array;
public:
	Queue(const int &num);
	~Queue();
	int IsEmpty();
	int IsFull();
	void MakeEmpty();
	void Enqueue(ElementType X);
	ElementType FrontElement();
	void Dequeue();
	void Print();
};

Queue::Queue(const int &num):Capacity(num),Size(0),Rear(0)
{
	Array=new ElementType[Capacity];
	for (int i=0;i<Capacity;i++)
	{
		Array[i]=0;
	}
}

Queue::~Queue()
{
	delete [] Array;
}

int Queue::IsEmpty()
{
	return Size==0;
}

void Queue::MakeEmpty()
{
	Size=0;
	Rear=0;
}

int Queue::IsFull()
{
	return Size==Capacity;
}

void Queue::Enqueue(ElementType X)
{
	if (IsFull ())
	{
		cout<<"Queue is Full!"<<endl;
	}
	else
	{
		Size++;
		Array[Rear]=X;
		if(Rear!=Capacity-1)
			Rear++;
	}
}

void Queue::Dequeue()
{
	if (IsEmpty())
	{
		cout<<"Queue is Empty!"<<endl;
	}
	else
	{
		for (int i=0;i<Size-1;i++)
		{
			Array[i]=Array[i+1];
		}
		Size--;
	}
}

ElementType Queue::FrontElement()
{
	if (!IsEmpty())
	{
		return Array[0];
	}
	else
	{
		cout<<"Queue is Empty!"<<endl;
		return 0;
	}
}

void Queue::Print()
{
	int i;
	for (i=0;i<Size;i++)
	{
		cout<<Array[i]<<endl;
	}
}

#endif


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值