基于动态数组的队列实现

//--------------DQueue.h------------------
#include<iostream>

#ifndef DQUEUE
#define DQUEUE

typedef int QueueElement;

class Queue
{
public:
	Queue(int numElements=128);
	Queue(const Queue & original);
	~Queue();
    const Queue & operator=(const Queue & rightHandSide);
	bool empty() const;
	void enqueue(const QueueElement & value);
	void display(ostream & out) const;
	QueueElement front() const;
	void dequeue();
private:
	int myFront,
	    myBack;
	int myCapacity;
	QueueElement *myArray;
};
#endif

//-------------DQueue.cpp-------------------
#include<iostream>
#include<cassert>
#include<new>
using namespace std;

#include"DQueue.h"

Queue::Queue(int numElements)
{
	assert(numElements>0);
	myCapacity=numElements;
	myArray=new (nothrow) QueueElement[myCapacity];
	if(myArray!=0)
	{
		myFront=0;
		myBack=0;
	}
	else
	{
		cerr<<"Inadequate memory to allocate stack \n";
		exit(1);
	}
}

Queue::Queue(const Queue & original)
	:myCapacity(original.myCapacity),
	 myFront(original.myFront),myBack(original.myBack)
{
	myArray=new (nothrow) QueueElement[myCapacity];
	if(myArray!=0)
		for(int pos=myFront;pos!=myBack;pos=(pos+1)%myCapacity)
			myArray[pos]=original.myArray[pos];
	else
	{
		cerr<<"***Inadequate memory to allocate stack ***\n";
		exit(1);
	}
}
Queue::~Queue()
{
	delete [] myArray;
}

const Queue & Queue::operator=(const Queue & rightHandSide)
{
	if(this!=&rightHandSide)
	{
		if(myCapacity!=rightHandSide.myCapacity)
		{
			delete [] myArray;
			myCapacity=rightHandSide.myCapacity;
			myArray=new (nothrow) QueueElement[myCapacity];
			if(myArray==0)
			{
				cerr<<"";
					exit(1);
			}
		}
		myFront=rightHandSide.myFront;
		myBack=rightHandSide.myBack;

		for(int pos=myFront;pos!=myBack;pos=(pos+1)%myCapacity)
			 myArray[pos]=rightHandSide.myArray[pos];	
	}
	return *this;
}


bool Queue::empty() const
{
	return (myFront==myBack);
}

void Queue::enqueue(const QueueElement & value)
{
	int newBack=(myBack+1)%myCapacity;
	if(newBack!=myFront)
	{
		myArray[newBack]=value;
		myBack=newBack;
	}
	else
	{
		cerr<<"***The queue is full---can't add new value***\n";
		exit(1);
	}
}

void Queue::display(ostream & out) const
{
	for(int i=myFront;i!=myBack;i=(i+1)%myCapacity)
		out<<myArray[i]<<" ";
	cout<<endl;
}

QueueElement Queue::front() const
{
	if(!empty())
	    return (myArray[myFront]);
	else
	{
		cerr<<"***The queue is empty--returning a garbage value***\n";
		QueueElement garbage;
		return garbage;
	}
}

void Queue::dequeue()
{
	if(!empty())
	{
		myFront=(myFront+1)%myCapacity;
	}
	else
	{
		cerr<<"***The queue is empty,can't remove a value.***\n";		
	}
}

//-----------DQueue_main.cpp--------------
#include<iostream>
using namespace std;

#include"DQueue.h"

int main()
{
	cout<<"Enter the queue's capacity: ";
    int cap;
    cin>>cap;
	Queue q(cap);
	/*
	//
	//
	                */
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值