moving data from queue to a stack?

Jan 18, 2012 at 11:59am
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.

I would greatly appreciate any help
Jan 18, 2012 at 12:16pm
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?
Jan 18, 2012 at 5:00pm
heres what i have so far:
1
2
3
4
5
6
7
int temp;
StackType<int> stack1;

while (!q.IsEmpty())
{
      q.Dequeue(temp)
}
not sure what to put after this.
how to push temp onto the stack?
Jan 18, 2012 at 5:06pm
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.
Jan 18, 2012 at 5:25pm
ok so now i have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void reverseQ()
{
	int temp;
	StackType<int> stack1;

while (!q.IsEmpty())
{
	
	q.Dequeue(temp);
}
stack1.Pop(temp);

while (!stack1.IsEmpty())
{
	q.Enqueue(temp);
}
stack1.Push(temp);

q.printQueue
}

but there is no output to this code
Last edited on Jan 18, 2012 at 6:01pm
Jan 18, 2012 at 6:02pm
Line 19 should be q.printQueue();
Jan 18, 2012 at 8:33pm
that still wasnt working so I moved q.printQueue(); to where i am calling the reverseQ function in main:
1
2
3
void reverseQ();
cout << "queue has been reversed: " << endl;
q.printQueue();

it is now printing the queue but the numbers are not reversed

is the code for reverseQ right?
Jan 19, 2012 at 10:40am
anyone?
Jan 19, 2012 at 11:59am
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.
Jan 19, 2012 at 12:08pm
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
Jan 19, 2012 at 2:00pm
ok so i mixed up push and pop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
Jan 19, 2012 at 2:56pm
the lines in the loops must be the reversed
Jan 19, 2012 at 7:47pm
no that still didnt fix it
heres what it looks like now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void reverseQ()
{
	int temp;
	StackType<int> stack1;

while (!q.IsEmpty())
{	
	q.Dequeue(temp);
	stack1.Push(temp);
}

while (!stack1.IsEmpty())
{
	stack1.Pop(temp);
	q.Enqueue(temp);
}

q.printQueue();

}
Last edited on Jan 19, 2012 at 8:10pm
Jan 19, 2012 at 8:43pm
Gaminic wrote:
as queue.pop() removes the last item (...)
That would be a LIFO (stack)
(...) and queue.push() adds it to the first
Head hurts. ¿what do you mean with 'last' and 'first' item?
http://cplusplus.com/forum/beginner/59638/#msg322031 should have worked.

@OP: you need to be more especific about the problem. And you should at least know the interface of your classes.
Edit: ¿is 'q' global?
Last edited on Jan 19, 2012 at 8:46pm
Jan 20, 2012 at 8:59am
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?
Jan 20, 2012 at 9:30am
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?)
Jan 20, 2012 at 2:38pm
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.

and heres the push and pop functions for stack:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
emplate<class ItemType>
void StackType<ItemType>::Pop(ItemType& item)
{
    NodeType<ItemType>* tempPtr;
    item = topPtr ->info;
    tempPtr = topPtr;
    topPtr = topPtr->next;
    delete tempPtr;
}

template <class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
    NodeType<ItemType>* location;
    location = new NodeType<ItemType>;
    location->info = newItem;
    location->next = topPtr;
    topPtr = location;
}
Last edited on Jan 20, 2012 at 3:04pm
Jan 20, 2012 at 5:59pm
i tried printing the stack to see if the numbers were going into it but it was blank. can anyone correct my code?
Jan 22, 2012 at 1:46pm
problem solved
Topic archived. No new replies allowed.