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?