toreib.blogg.se

Recursive linked list stack
Recursive linked list stack










recursive linked list stack

Method arr.reduce explained in the chapter Array methods to get the sum of the array.

Recursive linked list stack code#

Note that the code uses smart features that we’ve covered before:

  • Context: subcalls are made, while arrays are the “leaves” of the recursion tree, they give immediate result.
  • In the beginning of the call pow(2, 3) the execution context will store variables: x = 2, n = 3, the execution flow is at line 1 of the function. Let’s see what happens during the pow(2, 3) call.
  • After it ends, the old execution context is retrieved from the stack, and the outer function is resumed from where it stopped.
  • The execution context associated with it is remembered in a special data structure called execution context stack.
  • When a function makes a nested call, the following happens: One function call has exactly one execution context associated with it. The execution context is an internal data structure that contains details about the execution of a function: where the control flow is now, the current variables, the value of this (we don’t use it here) and few other internal details.

    recursive linked list stack

    The information about the process of execution of a running function is stored in its execution context. For that we’ll look under the hood of functions. Now let’s examine how recursive calls work. There are many tasks where recursive way of thinking gives simpler code, easier to maintain. That limits the application of recursion, but it still remains very wide. There are automatic optimizations that help alleviate this (“tail calls optimizations”), but they are not yet supported everywhere and work only in simple cases. We can rely on it being 10000, some engines allow more, but 100000 is probably out of limit for the majority of them. The maximal recursion depth is limited by JavaScript engine. The maximal number of nested calls (including the first one) is called recursion depth.












    Recursive linked list stack