queue的C++实现

来源:互联网 发布:c语言函数调用的例子 编辑:程序博客网 时间:2024/04/28 16:09
本人小白一枚,只想以此不断激励自己前行
#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

	
				
		
原创粉丝点击