I need to use an stack in a recursive function. Between function recursive calls the stack has to keep the contents and only modified by push or pop operations inside the function.
A way to do this it to define a global stack variable like this:
StackPtr stack = createStack();
Another way is to pass an stack to the function:
int recursiveFunction(int n, StackPtr stack);
Is there a way to do this but without global stack or passing an stack to the function? The idea is to encapsulate completely the function so the user just has to call it independently of the program specifications. It is like to define an static stack that conserve the stacks contents between recursive calls.
I tried:
int recursiveFunction(int n){
static StackPtr stack = NULL;
stack = createStack();
...
}
But the function reset the stack each call. I had to create the stack in the way shown because if I put:
static StackPtr stack = createStrack();
An "not initialized constant" error is thrown.
Thanks.
initializedwith initial value offalseto your try with the static stack. If it isfalse(will be so on the first call), create the stack and mark ittrue.