Reverse elements in a Queue Problem
Nov 12, 2009 at 9:01pm UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#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
Nov 12, 2009 at 10:02pm UTC
I'm sorry to say that almost nothing in your main file is anywhere close to right...
Nov 13, 2009 at 5:08am UTC
I am currently having a problem. I feel that I did everything right to try to get the numbers in reverse, however, I am only getting the last element in reverse so far and none of the other elements are showing! Can you please help me fix this problem?
Recent output:
The original elements in the queue are... 5 10 15 20
The reverse elements in the queue are... 20
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <fstream>
#include <sstream>
#include "queue.h"
void reverseQueue(queueType<int > & Queue);
using namespace std;
int main()
{
ifstream inData("input.txt" );
ofstream outData("output.txt" );
stringstream ss;
string line;
queueType<int > Queue;
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... " ;
reverseQueue(Queue);
while (!Queue.isEmptyQueue())
{
cout << Queue.front() << endl;
Queue.deleteQueue();
}
outData<<" " <<numbers;
ss.clear();
}
inData.close();
outData.close();
system ("PAUSE" );
return 0;
}
void reverseQueue(queueType<int > & Queue)
{
while (!Queue.isEmptyQueue())
{
int val = Queue.front();
Queue.deleteQueue();
reverseQueue(Queue);
Queue.addQueue(val);
}
}
queue.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#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
Nov 13, 2009 at 2:20pm UTC
If all you did was add a few print statements into reverseQueue you would see the
problem. Like for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void reverseQueue(queueType<int > & Queue)
{
std::cout << "reverseQueue( " ;
Queue.printQueue();
std::cout << " ) called" << std::endl;
while (!Queue.isEmptyQueue())
{
int val = Queue.front();
Queue.deleteQueue();
std::cout << "Popped front of queue (" << val << ")" << std::endl;
reverseQueue(Queue);
std::cout << "Returned from reverseQueue()" << std::endl;
Queue.addQueue(val);
std::cout << "Added " << val << " to end of queue" << std::endl;
}
}
Topic archived. No new replies allowed.