RECURSION IN JAVA
In a "random access data structure" each element can be accessed directly and in constant time.
Example: An array. Visual example: a book - each page of the book can be open independently of others. Random access is critical to many algorithms, for example binary search. a "sequential access data structure" is where each element can be accessed only in particular order. Visual example: a roll of paper or tape: here all prior material must be unrolled in order to get to data you want. A sub case of "sequential data structure" Also known as: limited access data structures. "A stack is a "recursive" data structure". To understand what a "stack" really is lets first look at the definition of a stack: " Stack is a collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle ". To break it down..
There are 2 basic operations performed in a stack:
1. Push 2. Pop Push() The Push function is used to add a new elements into stack Pop() Pop is used to delete or remove an element from the stack When a stack is completely full is said to be "Overflow state" If a stack is completely empty - it is said to be "Underflow state". In Java or C# :: value types (primitives) are stored on the stack. Reference types on the heap. Memory allocation in terms of stack and heap is not specified in the C++ standard. Instead, the standard distinguishes automatic and dynamic storage duration. Local variables have automatic storage duration and compilers store them on the stack. Objects with dynamic memory allocation (created with new) are stored on the free store, conventionally referred to as the heap. Please note: In languages that are not garbage-collected, objects on the heap lead to memory leaks if they are not freed. |
Stack can be visualized as behaving like a "stack of dinner plates" When we need a plate, we take the one at the top of the stack. When we add a plate to the stack, we place it on top of the stack.
We remove the plate from the top to load to wash that is a LIFO structure: The element which is inserted last is accessed first. Last In First Out |