I'm just learning about queues and stacks. I have the queue and stack working but now i need to write a function that reverses the data inside the queue. I need to create a temporary linked stack, remove the data from the queue and place it into the stack, then removed it from the stack and place it back into the queue. I know there are easier ways to reverse the numbers in a queue but I have to do it this way to help me understand how queues and stacks work.
Just pop the queue and push the element on the stack, until the queue is empty, and then do the same except that you pop the stack and push on the queue?
Just realized my previous post was wrong; as queue.pop() removes the last item and queue.push() adds it to the first, emptying a queue into a stack and then doing the opposite would simply place everything back into the same order. You could simply copy the stack into the queue, or depending on the type of queue, you could design a pop_first or push_front
1 2 3 4 5 6 7 8 9
queue myQueue;
stack myStack;
// Code to fill Queue goes here
/* ... */
// Empty queue:
while (!myQueue.empty()) myStack.push(myQueue.pop());
// Empty stack:
while (!myStack.empty()) myQueue.push_front(myQueue.pop());
As the stack works LIFO and push_front will add in reverse order of appearance, the second line will simply copy the stack into the queue.
No, and how could it even be correct?
Lines 6 to 11 iteratively empty the queue. The result is that the queue is empty and temp holds the last item.
Then on line 11, you pop the stack, which makes no sense: 'pop' means removing the last item. Your stack is empty!
On line 13 you check if stack is not empty, but it is empty, because all you've done with it is pop it once (which should throw an error).
Line 17 pushes something on the stack, but I'm not sure what 'temp' will hold at this point.
Anyway, I quite literally gave you the code in my previous post. My assumption was:
-pop takes no argument and returns the removed element.
-push takes the element to be added as argument and has no return value (void).
With those assumptions, you can chain the operations like I did.
That reverseQ() doesn't work at all. Line 11/17 are outside the loop. 'stack1' deals only with one value! Put the lines in the loop and see if that works
void reverseQ()
{
int temp;
StackType<int> stack1;
while (!q.IsEmpty())
{
stack1.Push(temp);
q.Dequeue(temp);
}
while (!stack1.IsEmpty())
{
q.Enqueue(temp);
stack1.Pop(temp);
}
}
@Gaminic i tried your code and it doesnt work. I have to put temp in or i get errors and i dont have a myQueue.push_front function, i only have functions for pushing and popping so not sure what myQueue.push_front does
what i am trying to do is send the items in the queue into the stack then send the items back into the queue. This is my first time using queues and stacks so im a bit confused. could someone please show me the correct way to do it?
Your last code works as expected. If that doesn't you implemented stack and/or queue wrong.
stack: Push -> front, Pop -> front (LIFO)
queue: Enqueue -> back, Dequeue -> front (FIFO)
So if you're implementation follows that pradigm the result of the code above is reverse data ->
printQueue() before reverseQ() != printQueue() after reverseQ()
independently from how (order?) you print the queue (does printQueue() do what you expect?)
so your saying my code is correct? no printQueue doesnt do what i expect. it prints the same number as before. there are not getting reversed. all my other functions for queue work perfectly except this one.