2013-04-11 01:07:29 -07:00
{{data structure}}[[Category:Classic CS problems and programs]]
2016-12-05 22:15:40 +01:00
A '''stack''' is a container of elements with <big><u>l</u>ast <u>i</u>n, <u>f</u>irst <u>o</u>ut</big> access policy. Sometimes it also called '''LIFO'''.
The stack is accessed through its '''top'''.
2015-02-20 00:35:01 -05:00
The basic stack operations are:
2013-04-11 01:07:29 -07:00
2016-12-05 22:15:40 +01:00
* ''push'' stores a new element onto the stack top;
* ''pop'' returns the last pushed stack element, while removing it from the stack;
* ''empty'' tests if the stack contains no elements.
2013-04-11 01:07:29 -07:00
2016-12-05 22:15:40 +01:00
<br>
2013-04-11 01:07:29 -07:00
Sometimes the last pushed stack element is made accessible for immutable access (for read) or mutable access (for write):
2016-12-05 22:15:40 +01:00
* ''top'' (sometimes called ''peek'' to keep with the ''p'' theme) returns the topmost element without modifying the stack.
2013-04-11 01:07:29 -07:00
2016-12-05 22:15:40 +01:00
<br>
2015-02-20 00:35:01 -05:00
Stacks allow a very simple hardware implementation.
2016-12-05 22:15:40 +01:00
They are common in almost all processors.
In programming, stacks are also very popular for their way ('''LIFO''') of resource management, usually memory.
2015-02-20 00:35:01 -05:00
Nested scopes of language objects are naturally implemented by a stack (sometimes by multiple stacks).
2016-12-05 22:15:40 +01:00
This is a classical way to implement local variables of a re-entrant or recursive subprogram. Stacks are also used to describe a formal computational framework.
2015-02-20 00:35:01 -05:00
See [[wp:Stack_automaton|stack machine]].
2016-12-05 22:15:40 +01:00
2015-02-20 00:35:01 -05:00
Many algorithms in pattern matching, compiler construction (e.g. [[wp:Recursive_descent|recursive descent parsers]]), and machine learning (e.g. based on [[wp:Tree_traversal|tree traversal]]) have a natural representation in terms of stacks.
2013-04-11 01:07:29 -07:00
2016-12-05 22:15:40 +01:00
;Task:
2013-04-11 01:07:29 -07:00
Create a stack supporting the basic operations: push, pop, empty.
2013-10-27 22:24:23 +00:00
2016-12-05 22:15:40 +01:00
2013-10-27 22:24:23 +00:00
{{Template:See also lists}}
2016-12-05 22:15:40 +01:00
<br><br>