Hello, I need help reversing elements in a queue using Recursion. My problem is that when I print out the reverse, it does not show the numbers on the screen. However my program compiles and runs. My question is...How come the numbers in reverse are not showing? Thanks for anyone who helps!
Current output:
The original elements in the queue are... 5 10 15 20
The reverse elements in the queue are...
Expected output:
The original elements in the queue are... 5 10 15 20
The reverse elements in the queue are... 20 15 10 5
Program:
#include <fstream>
#include <sstream>
#include "queue.h"
int ReverseQueue(int numbers);
using namespace std;
int main()
{
ifstream inData("input.txt");
ofstream outData("output.txt");
stringstream ss;
string line;
int numbers;
outData<<"The original elements in the queue are... ";
while(getline(inData, line))
{
ss << line;
}
while(ss>>numbers)
{
outData<<" "<<numbers;
}
outData<<endl;
outData<<"The reverse elements in the queue are... ";
while(getline(inData, line))
{
ss << line;
}
while(ss>>numbers)
{
ReverseQueue(numbers);
outData<<" "<<numbers;
}
inData.close();
outData.close();
system ("PAUSE");
return 0;
}
int ReverseQueue(int numbers)
{
queueType<int> Queue1;
queueType<int> Queue2;
queueType<int> Queue3;
while (!Queue1.isEmptyQueue())
{
Queue1.deleteQueue();
Queue2.addQueue(numbers);
Queue3.addQueue(numbers);
}
while (!Queue3.isEmptyQueue())
{
Queue3.deleteQueue();
Queue1.addQueue(numbers);
}
return ReverseQueue(numbers);
}
queue.h
#ifndef H_queueType
#define H_queueType
#include <iostream>
#include <cassert>
using namespace std;
template<class Type>
class queueType // public queueADT<Type>
{
public:
const queueType<Type>& operator=(const queueType<Type>&);
bool isEmptyQueue() const;
bool isFullQueue() const;
void initializeQueue();
Type front();
Type back();
void addQueue(const Type& queueElement);
void deleteQueue();
queueType(int queueSize = 100);
queueType(const queueType<Type>& otherQueue);
~queueType();
bool operator== (const queueType<Type>&);
bool operator!= (const queueType<Type>&);
void deleteBackOfQueue();
void deleteFrontOfQueue();
void printQueue();
private:
int maxQueueSize;
int count;
int queueFront;
int queueRear;
Type *list;
bool isEqual(const queueType<Type>&);
};
template<class Type>
bool queueType<Type>::isEmptyQueue() const
{
return (count == 0);
}
template<class Type>
bool queueType<Type>::isFullQueue() const
{
return (count == maxQueueSize);
}
template<class Type>
void queueType<Type>::initializeQueue()
{
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
}
template<class Type>
Type queueType<Type>::front()
{
assert(!isEmptyQueue());
return list[queueFront];
}
template<class Type>
Type queueType<Type>::back()
{
assert(!isEmptyQueue());
return list[queueRear];
}
template<class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
if (!isFullQueue())
{
queueRear = (queueRear + 1) % maxQueueSize;
count++;
list[queueRear] = newElement;
}
else
cout<< "Cannot add to a full queue."<<endl;
}
template<class Type>
void queueType<Type>::deleteQueue()
{
if (!isEmptyQueue())
{
count--;
queueFront = (queueFront + 1) % maxQueueSize;
}
else
cout <<"Cannot remove from an empty queue"<<endl;
}
template<class Type>
queueType<Type>::queueType(int queueSize)
{
if (queueSize <= 0)
{
cout<<"Size of the array to hold the queue must "
<<"be positive."<<endl;
cout<<"Creating an array of size 100."<<endl;
maxQueueSize = 100;
}
else
maxQueueSize = queueSize;
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
list = new Type[maxQueueSize];
}
template<class Type>
queueType<Type>::~queueType()
{
delete [] list;
}
template<class Type>
bool queueType<Type>::isEqual(const queueType<Type>& otherQueue)
{
bool bRet = false;
if (otherQueue.maxQueueSize == maxQueueSize && otherQueue.queueFront == queueFront)
{
bRet = true;
for (int j = 0; j < queueFront; ++j)
{
if (otherQueue.list[j] != list[j])
{
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
}
}
}
return bRet;
}
template<class Type>
bool queueType<Type>::operator==(const queueType<Type>& otherQueue)
{
return isEqual(otherQueue);
}
template<class Type>
bool queueType<Type>::operator!=(const queueType<Type>& otherQueue)
{
return !isEqual(otherQueue);
}
template<class Type>
void queueType<Type>::deleteBackOfQueue()
{
if (!isEmptyQueue())
{
count--;
queueRear = (queueRear - 1) % maxQueueSize;
}
else
cout <<"Cannot remove from an empty queue"<<endl;
}
template<class Type>
void queueType<Type>::deleteFrontOfQueue()
{
if (!isEmptyQueue())
{
count--;
queueFront = (queueFront - 1) % maxQueueSize;
}
else
cout <<"Cannot remove from an empty queue"<<endl;
}
template<class Type>
void queueType<Type>::printQueue()
{
for (int i=queueFront; i<queueRear; ++i)
{
cout<<" "<<list[i];
}
}
#endif