// Reverse the stack with fun() and fun1()
/* I am unable to understand what is being passed in function fun().
I am not sure about the argument of type stack <int> &s.
Is it a whole stack being passed or just one node? */
void fun1(stack<int> &s, int k)
{
if (s.empty())
{
s.push(k);
return;
}
int t = s.top(); s.pop();
fun1(s, k);
s.push(t);
}
void fun(stack<int> &s)
{
if (s.empty())
{
return;
}
int t = s.top(); s.pop();
fun(s);
fun1(s,t);
}
The program is trying to reverse the stack using the functions fun() and fun1(). My question is only that the argument stack <int>
&s takes the address of what?
In C++ a reference is basically an alias for an existing variable. They are represented using the ampersand (&
); stack<int>&
indicates a reference to a stack<int>
. Thus the arguments named s
in your functions fun
and fun1
indicate references to entire stacks, not individual nodes thereof.