Changes

Jump to: navigation, search

Stack

12,218 bytes added, 16:47, 3 June 2018
Created page with "'''Home * Programming * Data * Stack''' FILE:Data stack.svg|border|right|thumb|Stack <ref>[https://de.wikipedia.org/wiki/Stapelspeicher Stapelspeicher..."
'''[[Main Page|Home]] * [[Programming]] * [[Data]] * Stack'''

[[FILE:Data stack.svg|border|right|thumb|Stack <ref>[https://de.wikipedia.org/wiki/Stapelspeicher Stapelspeicher - Wikipedia.de] (German)</ref> ]]

A '''Stack''' is a [https://en.wikipedia.org/wiki/List_of_data_structures#Linear_data_structures linear data structure] to [https://en.wikipedia.org/wiki/Collection_%28computing%29 collect] data elements in [https://en.wikipedia.org/wiki/LIFO_%28computing%29 last in, first out order] (LIFO). The only elementary operations are '''push''' and '''pop'''. Push adds one element on the top of stack, while pop removes an entry from the top of the stack and returns that element to the caller. Entries are removed from the stack in the reverse order of their additions. In practice, there is often a third operation. '''Peek''' retrieves the top element without removing the entry from the stack.

The stack is a natural structure to remember where to continue after some work was interrupted to do some more important tasks, which itself might be interrupted again, even [[Recursion|recursively]] and multiple times. This applies to [[Depth-First|depth-first]] tree traversals like [[Minimax|minimax search]] or evaluating nested arithmetical [https://en.wikipedia.org/wiki/Expression_%28programming%29 expressions].

=Software Stack=
A stack can be implemented through an [[Array|array]] or a [[Linked List|linked list]], for instance in [[C]]-like pseudo code with a fixed sized array in conjunction with typical pre-increment and post-decrement:
<pre>
int tos = -1; // index top of stack
int stack[STACKSIZE]; // fixed sized array

void push (int element) {
if (tos == STACKSIZE - 1) throw stack overflow exception;
stack[++tos] = element;
}

int pop () {
if (tos == -1 ) throw stack underflow exception;
return stack[tos--];
}
</pre>
<span id="Hardware"></span>
=Hardware Stack=
Most [https://en.wikipedia.org/wiki/Central_processing_unit central processing units] support a hardware stack (processor stack, call stack) as part of the [[Memory|main memory]]. Processors have a special register, pointing to the top of stack (stack pointer), and push and pop instructions respectively. [[Intel]] processors from [[8080]] until [[x86-64]] let the stack grow from higher to lower addresses via pre-decrement push and post-increment pop.
<pre>
push(register) ::= stack[--sp] := register
pop (register) ::= register := stack[sp++]
</pre>

To implement such hardware stacks was motivated by control flow considerations of calling and returning from [https://en.wikipedia.org/wiki/Subroutine subroutines], possibly recursively as coined by [[Algol|Algol-like]] [[Languages|programming languages]]. A call instruction pushes the program address of the next instruction on the stack, just before it sets its [https://en.wikipedia.org/wiki/Program_counter instruction pointer register] to the subroutine-address. After executing the subroutine, a trailing return instruction pops the return address from the stack into the instruction pointer, to continue the execution flow as expected. Additionally, high level procedural programming languages demanded [https://en.wikipedia.org/wiki/Local_variable local variables]. While [https://en.wikipedia.org/wiki/Parameter_%28computer_science%29 actual parameters] were pushed before the call applying certain [https://en.wikipedia.org/wiki/Calling_convention calling conventions] of various platforms and compiler <ref>[https://en.wikipedia.org/wiki/X86_calling_conventions x86 calling conventions from Wikipedia]</ref><ref>[http://www.agner.org/optimize/calling_conventions.pdf Calling conventions for different C++ compilers and operating systems] (pdf) by [http://www.agner.org/ Agner Fog]</ref>, the callee did accordant stack pointer manipulations, to reserve or [https://en.wikipedia.org/wiki/Stack-based_memory_allocation allocate] a stack frame for local, automatic variables. Intel's 16-bit [[8086]] introduced an additional register, intended as base pointer into a possible variable stack frame, and the option of dynamic frame re-allocation.

A typical 8086 stack frame with [https://en.wikipedia.org/wiki/X86_calling_conventions#pascal Pascal calling conventions], where the callee is responsible for balancing the stack. Similar stack layout appears in [[x86]] with 32-bit extended registers (esp, ebp) and stack entries <ref>[http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames x86 Disassembly/Functions and Stack Frames from Wikibooks]</ref>.
<pre>
push word para1
push word para2
call foo
...

foo proc near
push bp
mov bp, sp
sub sp, 3*2 ; allocate space for three local 16-bit variables
...
mov sp, bp
pop bp
ret 4 ; return and "pop" additional four bytes of the parameters
</pre>
with following memory layout inside the scope of ''foo'':
<pre>
stack growth
^ <---- 16 bit ---->
| +------------------+ +------+
| local variable 3 | [bp-6] <---| SP |
+------------------+ +------+
| local variable 2 | [bp-4]
+------------------+
| local variable 1 | [bp-2]
+------------------+ +------+
| saved bp | <----------| BP |
+------------------+ +------+
| return address |
+------------------+
| parameter 2 | [bp+4]
+------------------+
| parameter 1 | [bp+6]
| +------------------+
v
higher addresses
</pre>

=Applications=
==Stack Calculator==
The [https://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish] or Postfix notation scheme as proposed in 1954 by [[Mathematician#Burks|Burks]], Warren, and Wright <ref>[[Mathematician#Burks|Arthur W. Burks]], Don W. Warren, Jesse B. Wright ('''1954''') ''[http://www.jstor.org/pss/2001990 An Analysis of a Logical Machine Using Parenthesis-Free Notation]''.</ref> , was independently reinvented by [[Mathematician#Bauer|Friedrich L. Bauer]], [[Mathematician#Samelson|Klaus Samelson]] <ref>[[Mathematician#Samelson|Klaus Samelson]], [[Mathematician#Bauer|Friedrich L. Bauer]] ('''1960'''). ''[http://portal.acm.org/citation.cfm?id=366968 Sequential Formula Translation]''. [[ACM#Communications|Communications of the ACM]], Vol. 3 No. 2, pp. 76-83</ref> and [[Mathematician#EWDijkstra|Edsger W. Dijkstra]] in the early 1960s to coin and utilize the [https://en.wikipedia.org/wiki/Stack_%28data_structure%29 stack data structure] to efficiently evaluate [https://en.wikipedia.org/wiki/Expression_%28programming%29 expressions], likely after [https://en.wikipedia.org/wiki/Parsing parsing] [[Recursion#InfixExpression|infix notation]] [[Recursion|recursively]], which is actually a [[Depth-First|depth-first]] traversal of a expression tree.

'''Infix''':
<pre>
(1 + 2)*(3 + 4)
</pre>

'''Infix Tree''':
<pre>
*
+ +
1 2 3 4
</pre>

'''Postfix Stack''':
<pre>
push 1
push 2
add
push 3
push 4
add
mul
</pre>
Many todays compiler and interpreter work that way, so does the [https://en.wikipedia.org/wiki/Java_Virtual_Machine Java Virtual Machine]. [https://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish notation] is further utilized in the [[Forth]] [https://en.wikipedia.org/wiki/Stack-oriented_programming_language Stack-oriented programming language].
<span id="SearchStack"></span>
==Depth-First Search==
In computer game programming, a stack of [[Node|nodes]] is also the appropriate data structure in [[Depth-First|depth-first tree search]], like [[Minimax]] or [[Alpha-Beta|Alpha-beta]]. Immediately before a [[Make Move|move is made]] to search further, the node is pushed on the stack - and popped back while [[Unmake Move|unmaking]]. Despite global state variables like a [[Board Representation|board representation]], which are [[Incremental Updates|updated incrementally]], programs implicitly use the processor stack by passing parameters to a [[Recursion|recursive]] search routine with local variables. A software stack is obligatory for an [[Iterative Search|iterative search]], and likely implemented as [[Array|array]] indexed by [[Ply|ply]].

=See also=
* [[Array]]
* [[Forth]]
* [[Linked List]]
* [[Triangular PV-Table#LinkedListontheStack|Linked List on the Stack]] from [[Triangular PV-Table]]
* [[Principal Variation#PVListontheStack|PV-List on the Stack]] from [[Principal variation]]
* [[Queue]]

=Publications=
* [[Mathematician#Burks|Arthur W. Burks]], Don W. Warren, [http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/w/Wright:Jesse_B=.html Jesse B. Wright] ('''1954''') ''[http://www.jstor.org/pss/2001990 An Analysis of a Logical Machine Using Parenthesis-Free Notation]''.
* [[Mathematician#Samelson|Klaus Samelson]], [[Mathematician#Bauer|Friedrich L. Bauer]] ('''1960'''). ''[http://portal.acm.org/citation.cfm?id=366968 Sequential Formula Translation]''. [[ACM#Communications|Communications of the ACM]], Vol. 3 No. 2
* [http://www.finseth.com/parts/index.php Craig A. Finseth] ('''1980'''). ''[http://www.atariarchives.org/bcc3/showpage.php?page=45 Something is Missing (Implementing recursion and stacks in BASIC)]''. [[Creative Computing#Best3|The Best of Creative Computing Volume 3]] ยป [[Basic]]
* [[Donald Knuth]] ('''1997'''). ''[http://www-cs-faculty.stanford.edu/%7Eknuth/taocp.html The Art of Computer Programming]'', Volume 1: Fundamental Algorithms (Third Edition), Addison-Wesley, ISBN 0-201-89683-4, 2.2.1: Stacks, Queues, and Deques
* [[Mathematician#THCormen|Thomas H. Cormen]], [[Charles Leiserson|Charles E. Leiserson]], [[Ronald L. Rivest]], [[Mathematician#CliffordStein|Clifford Stein]] ('''2009''').''[https://en.wikipedia.org/wiki/Introduction_to_Algorithms Introduction to Algorithms, 3rd Edition]''

=Forum Posts=
* [http://www.talkchess.com/forum/viewtopic.php?t=40493 LIFO stack based parallel processing?] by [[Srdja Matovic]], [[CCC]], September 22, 2011

=External Links=
* [https://en.wikipedia.org/wiki/Stack Stack from Wikipedia]
* [https://en.wikipedia.org/wiki/Stack_(abstract_data_type) Stack (abstract data type) from Wikipedia]
* [https://en.wikipedia.org/wiki/Call_stack Call stack from Wikipedia]
* [http://xlinux.nist.gov/dads//HTML/stack.html stack] from [http://xlinux.nist.gov/dads/ Dictionary of Algorithms and Data Structures] by [http://hissa.nist.gov/~black/ Paul E. Black], [https://en.wikipedia.org/wiki/National_Institute_of_Standards_and_Technology National Institute of Standards and Technology]
* [https://en.wikipedia.org/wiki/LIFO_%28computing%29 LIFO (computing) from Wikipedia]
* [https://en.wikipedia.org/wiki/Call_stack Call stack from Wikipedia]
* [https://en.wikipedia.org/wiki/Stack_overflow Stack overflow from Wikipedia]
* [https://en.wikipedia.org/wiki/Stack-based_memory_allocation Stack-based memory allocation from Wikipedia]
* [https://en.wikipedia.org/wiki/Stack_machine Stack machine from Wikipedia]
* [https://en.wikipedia.org/wiki/Stack-based_language Stack-oriented programming language from Wikipedia]
==[[x86]]==
* [http://en.wikibooks.org/wiki/X86_Disassembly/The_Stack x86 Disassembly/The Stack from Wikibooks]
* [http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames x86 Disassembly/Functions and Stack Frames from Wikibooks]
* [http://eli.thegreenplace.net/2011/02/04/where-the-top-of-the-stack-is-on-x86/ Where the top of the stack is on x86] from [http://eli.thegreenplace.net/ Eli Bendersky's website], February 04, 2011
==[[x86-64]]==
* [http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64/ Stack frame layout on x86-64] from [http://eli.thegreenplace.net/ Eli Bendersky's website], September 06, 2011
==[[Cpp|C++]]==
* [https://en.wikipedia.org/wiki/Stack_(C%2B%2B) Stack (C++) from Wikipedia]
* [http://www.sgi.com/tech/stl/stack.html stack<T, Sequence>], [https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library C++ Standard Library]
* [http://www.cplusplus.com/reference/stl/stack/ stack - C++ Reference]
==[[Java]]==
* [http://download.oracle.com/javase/1.4.2/docs/api/java/util/Stack.html Stack (Java 2 Platform SE v1.4.2)]
==.NET==
* [http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx Stack Class (System.Collections)]

=References=
<references />

'''[[Data|Up one Level]]'''

Navigation menu