in a nutshell..
what does the stack function do?
thanks
raz
in a nutshell..
what does the stack function do?
thanks
raz
push and pop
think of it as a stack of plates. each time you add a plate it goes on top (push onto the stack). then when you want to use a plate you take the top most one (pop from stack). You always have access to the last thing you put onto the stack. (you can get to everything else too but that defeats the purpose)
Also, a stack is an abstract data type (ADT), not a function. This means that it is an class which contains a place to hold the data, either an array or a linked list, and the two member functions explained above, namely push and pop. It is also considered LIFO or Last In First Out, which goes right along with the stack of plates example.
An example of a use for a stack is when a computer translates an expression in infix notation (3+2) to postfix notation (32+), it uses a stack as part of that conversion. Actually, it uses the stack to evaluate the newly created postfix expression as well.
Well, the word "stack" (like heap) has two different meanings, although they are similar.
*A* stack is an abstract data type which is essentially a first in, last out data structure, which behaves as mentioned in previous posts.
*The* stack operates similarly, and is used for storage of local variables, as well as parameters and return values to functions.
Any variable or object you create is created on the stack. Any memory allocated by malloc() or new is allocated on the heap (which actually is nothing like *a* heap which is another ADT).
When using the term "stack" or "heap" be sure to make the context tell the reader which meaning you're talking about, as, especially for a stack vs. the stack, they are easily confused.