Recursion with Queue's

Sep 18, 2008 at 12:48am
Hi guys. This is my first post here at cplusplus. I've been a big time lurker and have decided to join up. I assume this is where my question belongs, because I don't think advance technical advice is what I need, plus I haven't been learning C++ for very long, so I still consider myself a beginner. I aplogise if this is in the wrong section. This is more of a theoretical question rather than a straight up application of code.

I'm currently writing a client function which when passed the parameter int x, places X at the front of a queue. Normally, it would be easier to use a stack, I know, but the other functions I need to perform on the queue are actually easier when it's a queue, not a stack.

The main issue, is that I'm doing this in a recursive function, but everytime I input a new number it reverses the order of the original numbers. Is there anyway I can get the function to recursively place the original int's in the queue in the same order. I've searched everywhere and can't seem to find a way to do it. ring is the name of the queue used and is a member of the class Circle, which this is a function for.

1
2
3
4
5
6
7
8
9
10
11
if (!ring.empty())
{
	int top = ring.front();
	ring.pop();
	insert(x);
	ring.push(top);
}
else
{
	ring.push(x);
}


I tried putting the .push() before the insert(x), but then it never reaches the stopping case ( if(!ring.empty()) ) and just loops forever. Is there anyway I can get the first instance of the function to .push() first, and still call the other itself without looping forever?
Sep 18, 2008 at 1:00am
Oh, God, what is this aberration? A queue being used as a stack? Now I've seen it all.

Stacks are LIFO structures. If you push items x, y, and z, you'll pop items z, y, and x. That's a basic concept of data structures.

If you need to do push_front, push_back, and pop_front (I'm pretty sure you're doing it wrong if your design requires you to do it [based solely on the fact that you're using a queue as a stack], but what the hell) use a deque.
http://www.cplusplus.com/reference/stl/deque/
Last edited on Sep 18, 2008 at 1:01am
Sep 18, 2008 at 1:25am
Unfortunately I can't use anything other than a queue. It's really really annoying. When i got told about this I chucked up a huge stink about it, but there isn't really anything else I can do other than whinge and moan until they change, and I doubt they're gonna change it.

basically, this is an assignment for Uni, and they want use to adhere strictly to the design specifications they've given us.

Is there anyway you can do this using recursion? Or do I just copy it to a temp queue, clear the original, push X, and then push the temp queue, int by int, into the original (that's my original plan)?
Sep 18, 2008 at 1:30am
Oh, it appears I misunderstood you.
So, what you're saying that you have a queue (head) 1 2 3 4 5 6 (tail), and that when you push 0 to the head the result is (head) 0 6 5 4 3 2 1 (tail)?
Is that what you meant?
Sep 18, 2008 at 2:15am
Not quite. That is currently what its outputting when I push 0 (from your example). What I need it to do is when you push 0, for the queue to look like 0 1 2 3 4 5 6. But because of the recursive function, it's reversing the order of the original ints

So, I input 1 2 3 4 5. The way I need to input it is, 5 first, then 4 then 3 then 2 then 1. However,say I already have the queue 4 3 2 1, when I go to input 5 using the insert function, the queue is changed to 5 1 2 3 4. And as I go to input 6 after 5, it prints as 6 4 3 2 1 5. Its doing it because I'm having to .push(top) before I recall the function, otherwise it won't reach the stopping point. Is there any other way to do that recursively?
Sep 18, 2008 at 2:41am
I already have the queue 4 3 2 1, when I go to input 5 using the insert function, the queue is changed to 5 1 2 3 4.
How is that different from what I said?
In any case, I'll need to see the code.
Topic archived. No new replies allowed.