Sep 17, 2012 at 5:04pm UTC
It does nothing because the queue is passed by value.
Sep 17, 2012 at 5:08pm UTC
I think this is pseudo code and you are right that it reverses the queue.
Sep 17, 2012 at 5:09pm UTC
It does nothing because the queue is passed by value.
:)
well the examiner declared a function named delete so i guess that's the least of his issues..
i just want to ask what it would've done if we neglected the baby mistakes
if it is doing what i think it is doing then it is a cool way to do this
please assume that queue is passed by referrence and/or gloablly declared or whatever
i want the
conceptual answer for this
thnx...
Last edited on Sep 17, 2012 at 5:11pm UTC
Sep 17, 2012 at 5:14pm UTC
Well, if the queue is passed by reference then it is enough to consider one iteration of the function call
i=delete(q);
f(q);
insert(q,i);
A value is deleted from the beginning of the queue and inserted in the end of the queue.
So let the queue has
1, 2, 3, 4, 5, 6
At first all elements of the queue are deleted
then they are inserted in the order
6, 5, 4, 3, 2, 1
Last edited on Sep 17, 2012 at 5:15pm UTC
Sep 17, 2012 at 5:20pm UTC
No, the control will be passed back to the calling function. As I said it shall be defined as
void f( queue &q )
{
int i;
if(!isempty(q))
{
i=delete(q);
f(q);
insert(q,i);
}
}
Last edited on Sep 17, 2012 at 5:22pm UTC
Sep 17, 2012 at 5:53pm UTC
For example you can test the function by using standard container std::queue.
1 2 3 4 5 6 7 8 9 10
void reverse_queue( std::queue<int > &q )
{
if ( !q.empty() )
{
int x = q.front();
q.pop();
reverse_queue( q );
q.push( x );
}
}
If you have MS VC++ compiler then the queue contains public method _Get_container. You can use it to be sure that the queue was reversed. For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <queue>
void reverse_queue( std::queue<int > &q )
{
if ( !q.empty() )
{
int x = q.front();
q.pop();
reverse_queue( q );
q.push( x );
}
}
int main()
{
std::deque<int > d( 10 );
std::iota( d.begin(), d.end(), 0 );
std::copy( d.begin(), d.end(),
std::ostream_iterator<int >( std::cout, " " ) );
std::cout << std::endl;
std::queue<int > q( d );
reverse_queue( q );
d = q._Get_container();
std::copy( d.begin(), d.end(),
std::ostream_iterator<int >( std::cout, " " ) );
std::cout << std::endl;
}
The output is
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
Last edited on Sep 17, 2012 at 7:23pm UTC
Sep 17, 2012 at 8:05pm UTC
If to return to the original code it can work without passing q by reference. It depends on the type of queue. For example it could be declared as
typedef base_queue *queue;
where base_queue is the actual definition of the container.
Sep 18, 2012 at 2:55am UTC
yes it is reversing..
thnx for the help guys :).......