I need some help with setting up a count function for this code that i have. I'm not at all sure how to edit the code in there and my teacher explained that i have to edit a few functions to get it to work, but i don't understand why i need to.
Think about it like this:
If you add an Item onto your queue, and count was keeping track of that, what would count need to do? (in terms of increasing or decreasing).
Also, if you're removing something from your queue, what would count need to do? (same terms).
Let's say I have 8 items in my queue, and then I remove two, and add one:
count - 1; //for the first removal
count - 1; //for the second
cout + 1; //for the third
Now that that's all done, what would you do to show how many items are in your queue? (Hint: If I gave you a function that was supposed to do nothing but return a variable, how would I do that?)
Sometimes -- a lot of times, actually -- teachers confuse me.
In order to keep a queue, you must have some sort of inbuilt count. In this class's design, the count is (the difference from the index of the first and last items, plus the max queue size) mod max queue size.
template<class Type>
void queueType<Type>::addQueue(Type newElement)
{
if (!isFullQueue())
{
queueRear = (queueRear + 1) % maxQueueSize;
list[queueRear] = newElement;
}
else
cout << "Cannot add to a full queue." << endl;
count = count + 1;
}
^^^This function doesn't work as I think you're intending it to. Here, regardless if it actually adds or not, you have it increase.. Shouldn't you instead have it increase only when actually adding?
i think i got it, i looked up different types of solutions to this problem and the simplest one i found was to just get the absolute value of the queue and return that, it seems to work but i wanted to know if this is a practical solution to this problem?