c++ - I am trying to reverse the stack using recursion. What is being passed in function fun()? s is an object of class stack -
//reverse stack fun() , fun1() /*i unable understand being passed in function fun(). not sure arguement of type stack <int> &s. whole stack being passed or 1 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 trying reverse stack using functions fun() , fun1(). question arguement stack &s takes address of what?
in c++ reference alias existing variable. represented using ampersand (&); stack<int>& indicates reference stack<int>. arguments named s in functions fun , fun1 indicate references entire stacks, not individual nodes thereof.
Comments
Post a Comment