//--------------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;
}