Re: Urgent help: print elements in queue
Sorry about that.
<code>
#include <cstddef>
#include <new>
class FullQueue{};
class EmptyQueue{};
template<class ItemType>
class Queue
{
private:
int front; // front position
int rear; // rear position
int maxQue; // maximun number of
elements in the queue,
// including one
reserved element
ItemType* items; // it points to a
dynamically-allocated array
public:
Queue(int max); // parameterized class
constructor
Queue(); // default constructor;
queue created empty
Queue(const Queue<ItemType> &x); // copy
constructor;implicitly called
// for pass by value
void MakeEmpty(); // queue is made empty
bool IsEmpty(); // test if the queue is
empty
bool IsFull(); // test if the queue is
full
int length(); // return the number of
elements in the queue
void Print(); // print the value of
all elements in the
// queue from the front
to rear
void Enqueue(ItemType x); // insert x to the rear
of the queue
// Pre: the queue is
not empty
void Dequeue(ItemType &x); // delete the element
from the front of the queue
// Pre: the queue is
not empty
~Queue(); // destructor: memory
for dynamic array needs to be deallocated
};
template<class ItemType>
Queue<ItemType>::Queue()
{
items = new ItemType[maxQue];
front = maxQue;
rear = maxQue;
}
template<class ItemType>
Queue<ItemType>::Queue(int max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue -1;
items = new ItemType[maxQue];
}
template<class ItemType>
Queue<ItemType>::Queue(const Queue<ItemType> &x)
{
front = x.front;
rear = x.rear;
int count = x.count;
for(int i=0;i<x.count;++i)
{
items[i] = x.items[i];
}
}
template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{
rear = maxQue - 1;
front = maxQue - 1;
}
template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{
return (rear==front);
}
template<class ItemType>
bool Queue<ItemType>::IsFull()
{
return ((rear+1) % maxQue == front);
}
template<class ItemType>
int Queue<ItemType>::length()
{
return 0;
}
template<class ItemType>
void Queue<ItemType>::Print()
{
int current;
current = rear;
do{
current = (current + 1) % maxQue;
std::cout << items[current] << std::endl;
}while(current != front);
}
template<class ItemType>
void Queue<ItemType>::Enqueue(ItemType x)
{
if(IsFull())
throw FullQueue();
else
{
rear = (rear+1)% maxQue;
items[rear]=x;
}
}
template<class ItemType>
void Queue<ItemType>::Dequeue(ItemType &x)
{
if(IsEmpty())
throw EmptyQueue();
else
{
front=(front +1)% maxQue;
x =items[front];
}
}
template<class ItemType>
Queue<ItemType>::~Queue()
{
delete[]items;
}
// main file
#include <cstdlib>
#include <cstddef>
#include <new>
#include <iostream>
#include "Queue.h"
using namespace std;
int main()
{
Queue<int> IntQueue;
int x;
try{
IntQueue.Dequeue(x);
}
catch(FullQueue exceptionObject)
{
cerr << "FullQueue exception thrown.(int) " << endl;
}
catch(EmptyQueue exceptionObject)
{
cerr << "EmptyQueue exception thrown.(int) " << endl;
}
IntQueue.Enqueue(1);
IntQueue.Enqueue(2);
IntQueue.Enqueue(3);
IntQueue.Enqueue(4);
cout << "int length 3 = " << "4" << endl;//IntQueue.length() <<
endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << "4" << endl;;//IntQueue.length() <<
endl;
IntQueue.Print();
Queue<float> FloatQueue;
float y;
try{
FloatQueue.Dequeue(y);
}
catch(FullQueue exceptionObject)
{
cerr << "FullQueue exception thrown.(float)" << endl;
}
catch(EmptyQueue exceptionObject)
{
cerr << "EmptyQueue exception thrown.(float)" << endl;
}
cout << endl;
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
FloatQueue.Print();
system("pause");
return 0;
</code>