NinjaLink (18) Mar 21, 2010 at 10:51am UTC
I need help with my print function, so I can print the elements in a queue. The print function, that I have now is the last function in my header file. I tried to do a small simple program to see if it works, but it prints nothing to the screen. Please help!
Small Simple Program
#include <iostream>
#include "queue.h"
using namespace std;
int main()
{
queueType<int> Queue;
Queue.addQueue(5);
Queue.addQueue(10);
Queue.addQueue(15);
Queue.addQueue(20);
Queue.printQueue();
system ("PAUSE");
return 0;
}
header file:
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
#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 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>::printQueue()
{
while (!isEmptyQueue())
{
front();
deleteQueue();
}
}
#endif
Last edited on Mar 21, 2010 at 10:51am UTC
Bazzy (4118) Mar 21, 2010 at 10:51am UTC
180 181 182 183 184
while (!isEmptyQueue())
{
front();
deleteQueue();
}
You aren't printing anything, How do you expect the output to be visible?
NinjaLink (18) Mar 21, 2010 at 10:51am UTC
I also tried doing this
1 2 3 4
for (int i = front; i < back; i++)
{
list[i]
}
jsmith (3802) Mar 21, 2010 at 10:51am UTC
front and back are both member functions, so the line above as typed wouldn't even compile.
But even so, list[i] by itself does nothing.
So to repeat Bazzy... you aren't printing anything...
NinjaLink (18) Mar 21, 2010 at 10:51am UTC
what if I cout<<i; from above?
NinjaLink (18) Mar 21, 2010 at 10:51am UTC
The code below, I printed out the indices. I am running out of ideas!
1 2 3 4 5 6 7 8 9
template <class Type>
void queueType<Type>::printQueue()
{
for (int i=queueFront; i<queueRear; i++)
{
list[i];
cout<<i;
}
}
Bazzy (4118) Mar 21, 2010 at 10:51am UTC
what if I cout<<i; from above?
int i
You will be printing integers...
Your previous loop ( the while one ) was close to what you need
NinjaLink (18) Mar 21, 2010 at 10:51am UTC
ahh, I finally figured it out! Thank you guys for leading me in the right direction
This topic is archived - New replies not allowed.