fbpx
Wikipedia

Stack (abstract data type)

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations:

  • Push, which adds an element to the collection, and
  • Pop, which removes the most recently added element that was not yet removed.
Similar to a stack of plates, adding or removing is only possible at the top.
Simple representation of a stack runtime with push and pop operations.

Additionally, a peek operation can, without modifying the stack, return the value of the last element added. Calling this structure a stack is by analogy to a set of physical items stacked one atop another, such as a stack of plates.

The order in which an element added to or removed from a stack is described as last in, first out, referred to by the acronym LIFO.[nb 1] As with a stack of physical objects, this structure makes it easy to take an item off the top of the stack, but accessing a datum deeper in the stack may require taking off multiple other items first.[1]

Considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack. This data structure makes it possible to implement a stack as a singly linked list and as a pointer to the top element. A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space to accept another element, the stack is in a state of stack overflow.

A stack is needed to implement depth-first search.

History

Stacks entered the computer science literature in 1946, when Alan M. Turing used the terms "bury" and "unbury" as a means of calling and returning from subroutines.[2][3] Subroutines had already been implemented in Konrad Zuse's Z4 in 1945.

Klaus Samelson and Friedrich L. Bauer of Technical University Munich proposed the idea of a stack in 1955[4][5] and filed a patent in 1957.[6][7][8][9] In March 1988, by which time Samelson was deceased, Bauer received the IEEE Computer Pioneer Award for the invention of the stack principle.[10][5] Similar concepts were developed, independently, by Charles Leonard Hamblin in the first half of 1954[11] and by Wilhelm Kämmerer [de] in 1958.[12][13]

Stacks are often described using the analogy of a spring-loaded stack of plates in a cafeteria.[14][1][15] Clean plates are placed on top of the stack, pushing down any already there. When a plate is removed from the stack, the one below it pops up to become the new top plate.

Non-essential operations

In many implementations, a stack has more operations than the essential "push" and "pop" operations. An example of a non-essential operation is "top of stack", or "peek", which observes the top element without removing it from the stack.[16] This could be done with a "pop" followed by a "push" to return the same data to the stack, so it is not considered an essential operation. If the stack is empty, an underflow condition will occur upon execution of either the "stack top" or "pop" operations. Additionally, many implementations provide a check if the stack is empty and one that returns its size.

Software stacks

Implementation

A stack can be easily implemented either through an array or a linked list, as stacks are just special cases of lists.[17] What identifies the data structure as a stack, in either case, is not the implementation but the interface: the user is only allowed to pop or push items onto the array or linked list, with few other helper operations. The following will demonstrate both implementations, using pseudocode.

Array

An array can be used to implement a (bounded) stack, as follows. The first element, usually at the zero offset, is the bottom, resulting in array[0] being the first element pushed onto the stack and the last element popped off. The program must keep track of the size (length) of the stack, using a variable top that records the number of items pushed so far, therefore pointing to the place in the array where the next element is to be inserted (assuming a zero-based index convention). Thus, the stack itself can be effectively implemented as a three-element structure:

structure stack: maxsize : integer top : integer items : array of item 
procedure initialize(stk : stack, size : integer): stk.items ← new array of size items, initially empty stk.maxsize ← size stk.top ← 0 

The push operation adds an element and increments the top index, after checking for overflow:

procedure push(stk : stack, x : item): if stk.top = stk.maxsize: report overflow error else: stk.items[stk.top] ← x stk.top ← stk.top + 1 

Similarly, pop decrements the top index after checking for underflow, and returns the item that was previously the top one:

procedure pop(stk : stack): if stk.top = 0: report underflow error else: stk.top ← stk.top − 1 r ← stk.items[stk.top] return r 

Using a dynamic array, it is possible to implement a stack that can grow or shrink as much as needed. The size of the stack is simply the size of the dynamic array, which is a very efficient implementation of a stack since adding items to or removing items from the end of a dynamic array requires amortized O(1) time.

Linked list

Another option for implementing stacks is to use a singly linked list. A stack is then a pointer to the "head" of the list, with perhaps a counter to keep track of the size of the list:

structure frame: data : item next : frame or nil 
structure stack: head : frame or nil size : integer 
procedure initialize(stk : stack): stk.head ← nil stk.size ← 0 

Pushing and popping items happens at the head of the list; overflow is not possible in this implementation (unless memory is exhausted):

procedure push(stk : stack, x : item): newhead ← new frame newhead.data ← x newhead.next ← stk.head stk.head ← newhead stk.size ← stk.size + 1 
procedure pop(stk : stack): if stk.head = nil: report underflow error r ← stk.head.data stk.head ← stk.head.next stk.size ← stk.size - 1 return r 

Stacks and programming languages

Some languages, such as Perl, LISP, JavaScript and Python, make the stack operations push and pop available on their standard list/array types. Some languages, notably those in the Forth family (including PostScript), are designed around language-defined stacks that are directly visible to and manipulated by the programmer.

The following is an example of manipulating a stack in Common Lisp (">" is the Lisp interpreter's prompt; lines not starting with ">" are the interpreter's responses to expressions):

> (setf stack (list 'a 'b 'c)) ;; set the variable "stack" (A B C) > (pop stack) ;; get top (leftmost) element, should modify the stack A > stack ;; check the value of stack (B C) > (push 'new stack) ;; push a new top onto the stack (NEW B C) 

Several of the C++ Standard Library container types have push_back and pop_back operations with LIFO semantics; additionally, the stack template class adapts existing containers to provide a restricted API with only push/pop operations. PHP has an SplStack class. Java's library contains a Stack class that is a specialization of Vector. Following is an example program in Java language, using that class.

import java.util.Stack; class StackDemo { public static void main(String[]args) { Stack<String> stack = new Stack<String>(); stack.push("A"); // Insert "A" in the stack stack.push("B"); // Insert "B" in the stack stack.push("C"); // Insert "C" in the stack stack.push("D"); // Insert "D" in the stack System.out.println(stack.peek()); // Prints the top of the stack ("D") stack.pop(); // removing the top ("D") stack.pop(); // removing the next top ("C") } } 

Hardware stack

A common use of stacks at the architecture level is as a means of allocating and accessing memory.

Basic architecture of a stack

A typical stack is an area of computer memory with a fixed origin and a variable size. Initially the size of the stack is zero. A stack pointer, usually in the form of a hardware register, points to the most recently referenced location on the stack; when the stack has a size of zero, the stack pointer points to the origin of the stack.

The two operations applicable to all stacks are:

  • a push operation, in which a data item is placed at the location pointed to by the stack pointer, and the address in the stack pointer is adjusted by the size of the data item;
  • a pop or pull operation: a data item at the current location pointed to by the stack pointer is removed, and the stack pointer is adjusted by the size of the data item.

There are many variations on the basic principle of stack operations. Every stack has a fixed location, in memory, at which it begins. As data items are added to the stack, the stack pointer is displaced to indicate the current extent of the stack, which expands away from the origin.

Stack pointers may point to the origin of a stack or to a limited range of addresses either above or below the origin (depending on the direction in which the stack grows); however, the stack pointer cannot cross the origin of the stack. In other words, if the origin of the stack is at address 1000 and the stack grows downwards (towards addresses 999, 998, and so on), the stack pointer must never be incremented beyond 1000 (to 1001, 1002, etc.). If a pop operation on the stack causes the stack pointer to move past the origin of the stack, a stack underflow occurs. If a push operation causes the stack pointer to increment or decrement beyond the maximum extent of the stack, a stack overflow occurs.

Some environments that rely heavily on stacks may provide additional operations, for example:

  • Duplicate: the top item is popped, and then pushed again (twice), so that an additional copy of the former top item is now on top, with the original below it.
  • Peek: the topmost item is inspected (or returned), but the stack pointer and stack size does not change (meaning the item remains on the stack). This is also called top operation in many articles.
  • Swap or exchange: the two topmost items on the stack exchange places.
  • Rotate (or Roll): the n topmost items are moved on the stack in a rotating fashion. For example, if n = 3, items 1, 2, and 3 on the stack are moved to positions 2, 3, and 1 on the stack, respectively. Many variants of this operation are possible, with the most common being called left rotate and right rotate.

Stacks are often visualized growing from the bottom up (like real-world stacks). They may also be visualized growing from left to right, so that "topmost" becomes "rightmost", or even growing from top to bottom. The important feature is that the bottom of the stack is in a fixed position. The illustration in this section is an example of a top-to-bottom growth visualization: the top (28) is the stack "bottom", since the stack "top" (9) is where items are pushed or popped from.

A right rotate will move the first element to the third position, the second to the first and the third to the second. Here are two equivalent visualizations of this process:

apple banana banana ===right rotate==> cucumber cucumber apple 
cucumber apple banana ===left rotate==> cucumber apple banana 

A stack is usually represented in computers by a block of memory cells, with the "bottom" at a fixed location, and the stack pointer holding the address of the current "top" cell in the stack. The top and bottom terminology are used irrespective of whether the stack actually grows towards lower memory addresses or towards higher memory addresses.

Pushing an item on to the stack adjusts the stack pointer by the size of the item (either decrementing or incrementing, depending on the direction in which the stack grows in memory), pointing it to the next cell, and copies the new top item to the stack area. Depending again on the exact implementation, at the end of a push operation, the stack pointer may point to the next unused location in the stack, or it may point to the topmost item in the stack. If the stack points to the current topmost item, the stack pointer will be updated before a new item is pushed onto the stack; if it points to the next available location in the stack, it will be updated after the new item is pushed onto the stack.

Popping the stack is simply the inverse of pushing. The topmost item in the stack is removed and the stack pointer is updated, in the opposite order of that used in the push operation.

Stack in main memory

Many CISC-type CPU designs, including the x86, Z80 and 6502, have a dedicated register for use as the call stack stack pointer with dedicated call, return, push, and pop instructions that implicitly update the dedicated register, thus increasing code density. Some CISC processors, like the PDP-11 and the 68000, also have special addressing modes for implementation of stacks, typically with a semi-dedicated stack pointer as well (such as A7 in the 68000). In contrast, most RISC CPU designs do not have dedicated stack instructions and therefore most, if not all, registers may be used as stack pointers as needed.

Stack in registers or dedicated memory

Some machines use a stack for arithmetic and logical operations; operands are pushed onto the stack, and arithmetic and logical operations act on the top one or more items on the stack, popping them off the stack and pushing the result onto the stack. Machines that function in this fashion are called stack machines.

A number of mainframes and minicomputers were stack machines, the most famous being the Burroughs large systems. Other examples include the CISC HP 3000 machines and the CISC machines from Tandem Computers.

The x87 floating point architecture is an example of a set of registers organised as a stack where direct access to individual registers (relative to the current top) is also possible.

Having the top-of-stack as an implicit argument allows for a small machine code footprint with a good usage of bus bandwidth and code caches, but it also prevents some types of optimizations possible on processors permitting random access to the register file for all (two or three) operands. A stack structure also makes superscalar implementations with register renaming (for speculative execution) somewhat more complex to implement, although it is still feasible, as exemplified by modern x87 implementations.

Sun SPARC, AMD Am29000, and Intel i960 are all examples of architectures using register windows within a register-stack as another strategy to avoid the use of slow main memory for function arguments and return values.

There are also a number of small microprocessors that implements a stack directly in hardware and some microcontrollers have a fixed-depth stack that is not directly accessible. Examples are the PIC microcontrollers, the Computer Cowboys MuP21, the Harris RTX line, and the Novix NC4016. Many stack-based microprocessors were used to implement the programming language Forth at the microcode level.

Applications of stacks

Expression evaluation and syntax parsing

Calculators employing reverse Polish notation use a stack structure to hold values. Expressions can be represented in prefix, postfix or infix notations and conversion from one form to another may be accomplished using a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low-level code. Most programming languages are context-free languages, allowing them to be parsed with stack-based machines.

Backtracking

Another important application of stacks is backtracking. Consider a simple example of finding the correct path in a maze. There are a series of points, from the starting point to the destination. We start from one point. To reach the final destination, there are several paths. Suppose we choose a random path. After following a certain path, we realise that the path we have chosen is wrong. So we need to find a way by which we can return to the beginning of that path. This can be done with the use of stacks. With the help of stacks, we remember the point where we have reached. This is done by pushing that point into the stack. In case we end up on the wrong path, we can pop the last point from the stack and thus return to the last point and continue our quest to find the right path. This is called backtracking.

The prototypical example of a backtracking algorithm is depth-first search, which finds all vertices of a graph that can be reached from a specified starting vertex. Other applications of backtracking involve searching through spaces that represent potential solutions to an optimization problem. Branch and bound is a technique for performing such backtracking searches without exhaustively searching all of the potential solutions in such a space.

Compile-time memory management

 
A typical call stack, storing local data and call information for multiple levels of procedure calls. This stack grows downward from its origin. The stack pointer points to the current topmost datum on the stack. A push operation decrements the pointer and copies the data to the stack; a pop operation copies data from the stack and then increments the pointer. Each procedure called in the program stores procedure return information (in yellow) and local data (in other colors) by pushing them onto the stack. This type of stack implementation is extremely common, but it is vulnerable to buffer overflow attacks (see the text).

A number of programming languages are stack-oriented, meaning they define most basic operations (adding two numbers, printing a character) as taking their arguments from the stack, and placing any return values back on the stack. For example, PostScript has a return stack and an operand stack, and also has a graphics state stack and a dictionary stack. Many virtual machines are also stack-oriented, including the p-code machine and the Java Virtual Machine.

Almost all calling conventions‍—‌the ways in which subroutines receive their parameters and return results‍—‌use a special stack (the "call stack") to hold information about procedure/function calling and nesting in order to switch to the context of the called function and restore to the caller function when the calling finishes. The functions follow a runtime protocol between caller and callee to save arguments and return value on the stack. Stacks are an important way of supporting nested or recursive function calls. This type of stack is used implicitly by the compiler to support CALL and RETURN statements (or their equivalents) and is not manipulated directly by the programmer.

Some programming languages use the stack to store data that is local to a procedure. Space for local data items is allocated from the stack when the procedure is entered, and is deallocated when the procedure exits. The C programming language is typically implemented in this way. Using the same stack for both data and procedure calls has important security implications (see below) of which a programmer must be aware in order to avoid introducing serious security bugs into a program.

Efficient algorithms

Several algorithms use a stack (separate from the usual function call stack of most programming languages) as the principal data structure with which they organize their information. These include:

  • Graham scan, an algorithm for the convex hull of a two-dimensional system of points. A convex hull of a subset of the input is maintained in a stack, which is used to find and remove concavities in the boundary when a new point is added to the hull.[18]
  • Part of the SMAWK algorithm for finding the row minima of a monotone matrix uses stacks in a similar way to Graham scan.[19]
  • All nearest smaller values, the problem of finding, for each number in an array, the closest preceding number that is smaller than it. One algorithm for this problem uses a stack to maintain a collection of candidates for the nearest smaller value. For each position in the array, the stack is popped until a smaller value is found on its top, and then the value in the new position is pushed onto the stack.[20]
  • The nearest-neighbor chain algorithm, a method for agglomerative hierarchical clustering based on maintaining a stack of clusters, each of which is the nearest neighbor of its predecessor on the stack. When this method finds a pair of clusters that are mutual nearest neighbors, they are popped and merged.[21]

Security

Some computing environments use stacks in ways that may make them vulnerable to security breaches and attacks. Programmers working in such environments must take special care to avoid the pitfalls of these implementations.

For example, some programming languages use a common stack to store both data local to a called procedure and the linking information that allows the procedure to return to its caller. This means that the program moves data into and out of the same stack that contains critical return addresses for the procedure calls. If data is moved to the wrong location on the stack, or an oversized data item is moved to a stack location that is not large enough to contain it, return information for procedure calls may be corrupted, causing the program to fail.

Malicious parties may attempt a stack smashing attack that takes advantage of this type of implementation by providing oversized data input to a program that does not check the length of input. Such a program may copy the data in its entirety to a location on the stack, and in so doing it may change the return addresses for procedures that have called it. An attacker can experiment to find a specific type of data that can be provided to such a program such that the return address of the current procedure is reset to point to an area within the stack itself (and within the data provided by the attacker), which in turn contains instructions that carry out unauthorized operations.

This type of attack is a variation on the buffer overflow attack and is an extremely frequent source of security breaches in software, mainly because some of the most popular compilers use a shared stack for both data and procedure calls, and do not verify the length of data items. Frequently, programmers do not write code to verify the size of data items, either, and when an oversized or undersized data item is copied to the stack, a security breach may occur.

See also

Notes

  1. ^ By contrast, a queue operates first in, first out, referred to by the acronym FIFO.

References

  1. ^ a b Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2009) [1990]. Introduction to Algorithms (3rd ed.). MIT Press and McGraw-Hill. pp. 232–233. ISBN 0-262-03384-4.
  2. ^ Turing, Alan Mathison (1946-03-19) [1945]. Proposals for Development in the Mathematics Division of an Automatic Computing Engine (ACE). (NB. Presented on 1946-03-19 before the Executive Committee of the National Physical Laboratory (Great Britain).)
  3. ^ Carpenter, Brian Edward; Doran, Robert William (1977-01-01) [October 1975]. "The other Turing machine". The Computer Journal. 20 (3): 269–279. doi:10.1093/comjnl/20.3.269. (11 pages)
  4. ^ Samelson, Klaus (1957) [1955]. Written at Internationales Kolloquium über Probleme der Rechentechnik, Dresden, Germany. Probleme der Programmierungstechnik (in German). Berlin, Germany: VEB Deutscher Verlag der Wissenschaften. pp. 61–68. (NB. This paper was first presented in 1955. It describes a number stack (Zahlenkeller), but names it linear auxiliary memory (linearer Hilfsspeicher).)
  5. ^ a b Fothe, Michael; Wilke, Thomas, eds. (2015) [2014-11-14]. Written at Jena, Germany. Keller, Stack und automatisches Gedächtnis – eine Struktur mit Potenzial [Cellar, stack and automatic memory - a structure with potential] (PDF) (Tagungsband zum Kolloquium 14. November 2014 in Jena). GI Series: Lecture Notes in Informatics (LNI) – Thematics (in German). Vol. T-7. Bonn, Germany: Gesellschaft für Informatik (GI) / Köllen Druck + Verlag GmbH. ISBN 978-3-88579-426-4. ISSN 1614-3213. (PDF) from the original on 2020-04-12. Retrieved 2020-04-12. (77 pages)
  6. ^ Bauer, Friedrich Ludwig; Samelson, Klaus (1957-03-30). "Verfahren zur automatischen Verarbeitung von kodierten Daten und Rechenmaschine zur Ausübung des Verfahrens" (in German). Munich, Germany: Deutsches Patentamt. DE-PS 1094019. Retrieved 2010-10-01.
  7. ^ Bauer, Friedrich Ludwig; Goos, Gerhard (1982). Informatik – Eine einführende Übersicht (in German). Vol. Part 1 (3 ed.). Berlin: Springer-Verlag. p. 222. ISBN 3-540-11722-9. Die Bezeichnung 'Keller' hierfür wurde von Bauer und Samelson in einer deutschen Patentanmeldung vom 30. März 1957 eingeführt.
  8. ^ Samelson, Klaus; Bauer, Friedrich Ludwig (1959). "Sequentielle Formelübersetzung" [Sequential Formula Translation]. Elektronische Rechenanlagen (in German). 1 (4): 176–182.
  9. ^ Samelson, Klaus; Bauer, Friedrich Ludwig (1960). "Sequential Formula Translation". Communications of the ACM. 3 (2): 76–83. doi:10.1145/366959.366968. S2CID 16646147.
  10. ^ Technical University of Munich, Faculty of Computer Science. 1989-01-01. Archived from the original on 2017-11-07.
  11. ^ Hamblin, Charles Leonard (May 1957). An Addressless Coding Scheme based on Mathematical Notation (PDF) (typescript). N.S.W. University of Technology. pp. 121-1–121-12. (PDF) from the original on 2020-04-12. Retrieved 2020-04-12. (12 pages)
  12. ^ Kämmerer, Wilhelm (1958). Ziffern-Rechenautomat mit Programmierung nach mathematischem Formelbild (Habilitation thesis) (in German). Friedrich-Schiller-Universität, Jena, Germany.
  13. ^ Kämmerer, Wilhelm (1960). Ziffernrechenautomaten. Elektronisches Rechnen und Regeln (in German). Vol. 1. Berlin, Germany: Akademie-Verlag.
  14. ^ Ball, John A. (1978). Algorithms for RPN calculators (1 ed.). Cambridge, Massachusetts, USA: Wiley-Interscience, John Wiley & Sons, Inc. ISBN 978-0-471-03070-6.
  15. ^ Godse, Atul P.; Godse, Deepali A. (2010-01-01). Computer Architecture. Technical Publications. pp. 1–56. ISBN 978-8-18431534-9. Retrieved 2015-01-30.
  16. ^ Horowitz, Ellis (1984). Fundamentals of Data Structures in Pascal. Computer Science Press. p. 67.
  17. ^ Pandey, Shreesham (2020-06-07). "Data Structures in a Nutshell". Rochester, New York, USA. SSRN 4145204. from the original on 2022-11-12. Retrieved 2022-11-27. {{cite journal}}: Cite journal requires |journal= (help)
  18. ^ Graham, Ronald "Ron" Lewis (1972). An Efficient Algorithm for Determining the Convex Hull of a Finite Planar Set (PDF). Information Processing Letters 1. Vol. 1. pp. 132–133. (PDF) from the original on 2022-10-22.
  19. ^ Aggarwal, Alok; Klawe, Maria M.; Moran, Shlomo; Shor, Peter; Wilber, Robert (1987). "Geometric applications of a matrix-searching algorithm". Algorithmica. 2 (1–4): 195–208. doi:10.1007/BF01840359. MR 0895444. S2CID 7932878..
  20. ^ Berkman, Omer; Schieber, Baruch; Vishkin, Uzi (1993). "Optimal doubly logarithmic parallel algorithms based on finding all nearest smaller values". Journal of Algorithms. 14 (3): 344–370. CiteSeerX 10.1.1.55.5669. doi:10.1006/jagm.1993.1018..
  21. ^ Murtagh, Fionn (1983). "A survey of recent advances in hierarchical clustering algorithms" (PDF). The Computer Journal. 26 (4): 354–359. doi:10.1093/comjnl/26.4.354..

Further reading

  • Donald Knuth. The Art of Computer Programming, Volume 1: Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.2.1: Stacks, Queues, and Deques, pp. 238–243.
  • Langmaack, Hans (2015) [2014-11-14]. Written at Kiel, Germany. Friedrich L. Bauers und Klaus Samelsons Arbeiten in den 1950er-Jahren zur Einführung der Begriffe Kellerprinzip und Kellerautomat [Friedrich L. Bauer's and Klaus Samelson's works in the 1950s on the introduction of the terms cellar principle and cellar automaton] (PDF) (in German). Jena, Germany: Institut für Informatik, Christian-Albrechts-Universität zu Kiel. pp. 19–29. (PDF) from the original on 2022-11-14. Retrieved 2022-11-14. (11 pages) (NB. Published in Fothe & Wilke.)
  • Goos, Gerhard (2017-08-07). Geschichte der deutschsprachigen Informatik - Programmiersprachen und Übersetzerbau [History of informatics in German-speaking countries - Programming languages and compiler design] (PDF) (in German). Karlsruhe, Germany: Fakultät für Informatik, Karlsruhe Institute of Technology (KIT). (PDF) from the original on 2022-05-19. Retrieved 2022-11-14. (11 pages)

External links

  • Stack Machines - the new wave
  • Bounding stack depth
  • Stack Size Analysis for Interrupt-driven Programs

stack, abstract, data, type, term, lifo, accounting, lifo, accounting, term, pushdown, strength, training, pushdown, exercise, other, uses, stack, disambiguation, computer, science, stack, abstract, data, type, that, serves, collection, elements, with, main, o. For the use of the term LIFO in accounting see LIFO accounting For the use of the term pushdown in strength training see Pushdown exercise For other uses see Stack disambiguation In computer science a stack is an abstract data type that serves as a collection of elements with two main operations Push which adds an element to the collection and Pop which removes the most recently added element that was not yet removed Similar to a stack of plates adding or removing is only possible at the top Simple representation of a stack runtime with push and pop operations Additionally a peek operation can without modifying the stack return the value of the last element added Calling this structure a stack is by analogy to a set of physical items stacked one atop another such as a stack of plates The order in which an element added to or removed from a stack is described as last in first out referred to by the acronym LIFO nb 1 As with a stack of physical objects this structure makes it easy to take an item off the top of the stack but accessing a datum deeper in the stack may require taking off multiple other items first 1 Considered as a linear data structure or more abstractly a sequential collection the push and pop operations occur only at one end of the structure referred to as the top of the stack This data structure makes it possible to implement a stack as a singly linked list and as a pointer to the top element A stack may be implemented to have a bounded capacity If the stack is full and does not contain enough space to accept another element the stack is in a state of stack overflow A stack is needed to implement depth first search Contents 1 History 2 Non essential operations 3 Software stacks 3 1 Implementation 3 1 1 Array 3 1 2 Linked list 3 2 Stacks and programming languages 4 Hardware stack 4 1 Basic architecture of a stack 4 1 1 Stack in main memory 4 1 2 Stack in registers or dedicated memory 5 Applications of stacks 5 1 Expression evaluation and syntax parsing 5 2 Backtracking 5 3 Compile time memory management 5 4 Efficient algorithms 6 Security 7 See also 8 Notes 9 References 10 Further reading 11 External linksHistory EditSee also Jan Lukasiewicz Work Stacks entered the computer science literature in 1946 when Alan M Turing used the terms bury and unbury as a means of calling and returning from subroutines 2 3 Subroutines had already been implemented in Konrad Zuse s Z4 in 1945 Klaus Samelson and Friedrich L Bauer of Technical University Munich proposed the idea of a stack in 1955 4 5 and filed a patent in 1957 6 7 8 9 In March 1988 by which time Samelson was deceased Bauer received the IEEE Computer Pioneer Award for the invention of the stack principle 10 5 Similar concepts were developed independently by Charles Leonard Hamblin in the first half of 1954 11 and by Wilhelm Kammerer de in 1958 12 13 Stacks are often described using the analogy of a spring loaded stack of plates in a cafeteria 14 1 15 Clean plates are placed on top of the stack pushing down any already there When a plate is removed from the stack the one below it pops up to become the new top plate Non essential operations EditIn many implementations a stack has more operations than the essential push and pop operations An example of a non essential operation is top of stack or peek which observes the top element without removing it from the stack 16 This could be done with a pop followed by a push to return the same data to the stack so it is not considered an essential operation If the stack is empty an underflow condition will occur upon execution of either the stack top or pop operations Additionally many implementations provide a check if the stack is empty and one that returns its size Software stacks EditImplementation Edit A stack can be easily implemented either through an array or a linked list as stacks are just special cases of lists 17 What identifies the data structure as a stack in either case is not the implementation but the interface the user is only allowed to pop or push items onto the array or linked list with few other helper operations The following will demonstrate both implementations using pseudocode Array Edit An array can be used to implement a bounded stack as follows The first element usually at the zero offset is the bottom resulting in array 0 being the first element pushed onto the stack and the last element popped off The program must keep track of the size length of the stack using a variable top that records the number of items pushed so far therefore pointing to the place in the array where the next element is to be inserted assuming a zero based index convention Thus the stack itself can be effectively implemented as a three element structure structure stack maxsize integer top integer items array of item procedure initialize stk stack size integer stk items new array of size items initially empty stk maxsize size stk top 0 The push operation adds an element and increments the top index after checking for overflow procedure push stk stack x item if stk top stk maxsize report overflow error else stk items stk top x stk top stk top 1 Similarly pop decrements the top index after checking for underflow and returns the item that was previously the top one procedure pop stk stack if stk top 0 report underflow error else stk top stk top 1 r stk items stk top return r Using a dynamic array it is possible to implement a stack that can grow or shrink as much as needed The size of the stack is simply the size of the dynamic array which is a very efficient implementation of a stack since adding items to or removing items from the end of a dynamic array requires amortized O 1 time Linked list Edit Another option for implementing stacks is to use a singly linked list A stack is then a pointer to the head of the list with perhaps a counter to keep track of the size of the list structure frame data item next frame or nil structure stack head frame or nil size integer procedure initialize stk stack stk head nil stk size 0 Pushing and popping items happens at the head of the list overflow is not possible in this implementation unless memory is exhausted procedure push stk stack x item newhead new frame newhead data x newhead next stk head stk head newhead stk size stk size 1 procedure pop stk stack if stk head nil report underflow error r stk head data stk head stk head next stk size stk size 1 return r Stacks and programming languages Edit Some languages such as Perl LISP JavaScript and Python make the stack operations push and pop available on their standard list array types Some languages notably those in the Forth family including PostScript are designed around language defined stacks that are directly visible to and manipulated by the programmer The following is an example of manipulating a stack in Common Lisp gt is the Lisp interpreter s prompt lines not starting with gt are the interpreter s responses to expressions gt setf stack list a b c set the variable stack A B C gt pop stack get top leftmost element should modify the stack A gt stack check the value of stack B C gt push new stack push a new top onto the stack NEW B C Several of the C Standard Library container types have push back and pop back operations with LIFO semantics additionally the stack template class adapts existing containers to provide a restricted API with only push pop operations PHP has an SplStack class Java s library contains a Stack class that is a specialization of Vector Following is an example program in Java language using that class import java util Stack class StackDemo public static void main String args Stack lt String gt stack new Stack lt String gt stack push A Insert A in the stack stack push B Insert B in the stack stack push C Insert C in the stack stack push D Insert D in the stack System out println stack peek Prints the top of the stack D stack pop removing the top D stack pop removing the next top C Hardware stack EditMain article Stack based memory allocation A common use of stacks at the architecture level is as a means of allocating and accessing memory Basic architecture of a stack Edit A typical stack is an area of computer memory with a fixed origin and a variable size Initially the size of the stack is zero A stack pointer usually in the form of a hardware register points to the most recently referenced location on the stack when the stack has a size of zero the stack pointer points to the origin of the stack The two operations applicable to all stacks are a push operation in which a data item is placed at the location pointed to by the stack pointer and the address in the stack pointer is adjusted by the size of the data item a pop or pull operation a data item at the current location pointed to by the stack pointer is removed and the stack pointer is adjusted by the size of the data item There are many variations on the basic principle of stack operations Every stack has a fixed location in memory at which it begins As data items are added to the stack the stack pointer is displaced to indicate the current extent of the stack which expands away from the origin Stack pointers may point to the origin of a stack or to a limited range of addresses either above or below the origin depending on the direction in which the stack grows however the stack pointer cannot cross the origin of the stack In other words if the origin of the stack is at address 1000 and the stack grows downwards towards addresses 999 998 and so on the stack pointer must never be incremented beyond 1000 to 1001 1002 etc If a pop operation on the stack causes the stack pointer to move past the origin of the stack a stack underflow occurs If a push operation causes the stack pointer to increment or decrement beyond the maximum extent of the stack a stack overflow occurs Some environments that rely heavily on stacks may provide additional operations for example Duplicate the top item is popped and then pushed again twice so that an additional copy of the former top item is now on top with the original below it Peek the topmost item is inspected or returned but the stack pointer and stack size does not change meaning the item remains on the stack This is also called top operation in many articles Swap or exchange the two topmost items on the stack exchange places Rotate or Roll the n topmost items are moved on the stack in a rotating fashion For example if n 3 items 1 2 and 3 on the stack are moved to positions 2 3 and 1 on the stack respectively Many variants of this operation are possible with the most common being called left rotate and right rotate Stacks are often visualized growing from the bottom up like real world stacks They may also be visualized growing from left to right so that topmost becomes rightmost or even growing from top to bottom The important feature is that the bottom of the stack is in a fixed position The illustration in this section is an example of a top to bottom growth visualization the top 28 is the stack bottom since the stack top 9 is where items are pushed or popped from A right rotate will move the first element to the third position the second to the first and the third to the second Here are two equivalent visualizations of this process apple banana banana right rotate gt cucumber cucumber apple cucumber apple banana left rotate gt cucumber apple banana A stack is usually represented in computers by a block of memory cells with the bottom at a fixed location and the stack pointer holding the address of the current top cell in the stack The top and bottom terminology are used irrespective of whether the stack actually grows towards lower memory addresses or towards higher memory addresses Pushing an item on to the stack adjusts the stack pointer by the size of the item either decrementing or incrementing depending on the direction in which the stack grows in memory pointing it to the next cell and copies the new top item to the stack area Depending again on the exact implementation at the end of a push operation the stack pointer may point to the next unused location in the stack or it may point to the topmost item in the stack If the stack points to the current topmost item the stack pointer will be updated before a new item is pushed onto the stack if it points to the next available location in the stack it will be updated after the new item is pushed onto the stack Popping the stack is simply the inverse of pushing The topmost item in the stack is removed and the stack pointer is updated in the opposite order of that used in the push operation Stack in main memory Edit Many CISC type CPU designs including the x86 Z80 and 6502 have a dedicated register for use as the call stack stack pointer with dedicated call return push and pop instructions that implicitly update the dedicated register thus increasing code density Some CISC processors like the PDP 11 and the 68000 also have special addressing modes for implementation of stacks typically with a semi dedicated stack pointer as well such as A7 in the 68000 In contrast most RISC CPU designs do not have dedicated stack instructions and therefore most if not all registers may be used as stack pointers as needed Stack in registers or dedicated memory Edit Main article Stack machine Some machines use a stack for arithmetic and logical operations operands are pushed onto the stack and arithmetic and logical operations act on the top one or more items on the stack popping them off the stack and pushing the result onto the stack Machines that function in this fashion are called stack machines A number of mainframes and minicomputers were stack machines the most famous being the Burroughs large systems Other examples include the CISC HP 3000 machines and the CISC machines from Tandem Computers The x87 floating point architecture is an example of a set of registers organised as a stack where direct access to individual registers relative to the current top is also possible Having the top of stack as an implicit argument allows for a small machine code footprint with a good usage of bus bandwidth and code caches but it also prevents some types of optimizations possible on processors permitting random access to the register file for all two or three operands A stack structure also makes superscalar implementations with register renaming for speculative execution somewhat more complex to implement although it is still feasible as exemplified by modern x87 implementations Sun SPARC AMD Am29000 and Intel i960 are all examples of architectures using register windows within a register stack as another strategy to avoid the use of slow main memory for function arguments and return values There are also a number of small microprocessors that implements a stack directly in hardware and some microcontrollers have a fixed depth stack that is not directly accessible Examples are the PIC microcontrollers the Computer Cowboys MuP21 the Harris RTX line and the Novix NC4016 Many stack based microprocessors were used to implement the programming language Forth at the microcode level Applications of stacks EditExpression evaluation and syntax parsing Edit Calculators employing reverse Polish notation use a stack structure to hold values Expressions can be represented in prefix postfix or infix notations and conversion from one form to another may be accomplished using a stack Many compilers use a stack for parsing the syntax of expressions program blocks etc before translating into low level code Most programming languages are context free languages allowing them to be parsed with stack based machines Backtracking Edit Main article Backtracking Another important application of stacks is backtracking Consider a simple example of finding the correct path in a maze There are a series of points from the starting point to the destination We start from one point To reach the final destination there are several paths Suppose we choose a random path After following a certain path we realise that the path we have chosen is wrong So we need to find a way by which we can return to the beginning of that path This can be done with the use of stacks With the help of stacks we remember the point where we have reached This is done by pushing that point into the stack In case we end up on the wrong path we can pop the last point from the stack and thus return to the last point and continue our quest to find the right path This is called backtracking The prototypical example of a backtracking algorithm is depth first search which finds all vertices of a graph that can be reached from a specified starting vertex Other applications of backtracking involve searching through spaces that represent potential solutions to an optimization problem Branch and bound is a technique for performing such backtracking searches without exhaustively searching all of the potential solutions in such a space Compile time memory management Edit Main articles Stack based memory allocation and Stack machine A typical call stack storing local data and call information for multiple levels of procedure calls This stack grows downward from its origin The stack pointer points to the current topmost datum on the stack A push operation decrements the pointer and copies the data to the stack a pop operation copies data from the stack and then increments the pointer Each procedure called in the program stores procedure return information in yellow and local data in other colors by pushing them onto the stack This type of stack implementation is extremely common but it is vulnerable to buffer overflow attacks see the text A number of programming languages are stack oriented meaning they define most basic operations adding two numbers printing a character as taking their arguments from the stack and placing any return values back on the stack For example PostScript has a return stack and an operand stack and also has a graphics state stack and a dictionary stack Many virtual machines are also stack oriented including the p code machine and the Java Virtual Machine Almost all calling conventions the ways in which subroutines receive their parameters and return results use a special stack the call stack to hold information about procedure function calling and nesting in order to switch to the context of the called function and restore to the caller function when the calling finishes The functions follow a runtime protocol between caller and callee to save arguments and return value on the stack Stacks are an important way of supporting nested or recursive function calls This type of stack is used implicitly by the compiler to support CALL and RETURN statements or their equivalents and is not manipulated directly by the programmer Some programming languages use the stack to store data that is local to a procedure Space for local data items is allocated from the stack when the procedure is entered and is deallocated when the procedure exits The C programming language is typically implemented in this way Using the same stack for both data and procedure calls has important security implications see below of which a programmer must be aware in order to avoid introducing serious security bugs into a program Efficient algorithms Edit Several algorithms use a stack separate from the usual function call stack of most programming languages as the principal data structure with which they organize their information These include Graham scan an algorithm for the convex hull of a two dimensional system of points A convex hull of a subset of the input is maintained in a stack which is used to find and remove concavities in the boundary when a new point is added to the hull 18 Part of the SMAWK algorithm for finding the row minima of a monotone matrix uses stacks in a similar way to Graham scan 19 All nearest smaller values the problem of finding for each number in an array the closest preceding number that is smaller than it One algorithm for this problem uses a stack to maintain a collection of candidates for the nearest smaller value For each position in the array the stack is popped until a smaller value is found on its top and then the value in the new position is pushed onto the stack 20 The nearest neighbor chain algorithm a method for agglomerative hierarchical clustering based on maintaining a stack of clusters each of which is the nearest neighbor of its predecessor on the stack When this method finds a pair of clusters that are mutual nearest neighbors they are popped and merged 21 Security EditSome computing environments use stacks in ways that may make them vulnerable to security breaches and attacks Programmers working in such environments must take special care to avoid the pitfalls of these implementations For example some programming languages use a common stack to store both data local to a called procedure and the linking information that allows the procedure to return to its caller This means that the program moves data into and out of the same stack that contains critical return addresses for the procedure calls If data is moved to the wrong location on the stack or an oversized data item is moved to a stack location that is not large enough to contain it return information for procedure calls may be corrupted causing the program to fail Malicious parties may attempt a stack smashing attack that takes advantage of this type of implementation by providing oversized data input to a program that does not check the length of input Such a program may copy the data in its entirety to a location on the stack and in so doing it may change the return addresses for procedures that have called it An attacker can experiment to find a specific type of data that can be provided to such a program such that the return address of the current procedure is reset to point to an area within the stack itself and within the data provided by the attacker which in turn contains instructions that carry out unauthorized operations This type of attack is a variation on the buffer overflow attack and is an extremely frequent source of security breaches in software mainly because some of the most popular compilers use a shared stack for both data and procedure calls and do not verify the length of data items Frequently programmers do not write code to verify the size of data items either and when an oversized or undersized data item is copied to the stack a security breach may occur See also Edit Computer programming portalList of data structures Queue Double ended queue FIFO computing and electronics Stack based memory allocation Stack overflow Stack oriented programming languageNotes Edit By contrast a queue operates first in first out referred to by the acronym FIFO References Edit a b Cormen Thomas H Leiserson Charles E Rivest Ronald L Stein Clifford 2009 1990 Introduction to Algorithms 3rd ed MIT Press and McGraw Hill pp 232 233 ISBN 0 262 03384 4 Turing Alan Mathison 1946 03 19 1945 Proposals for Development in the Mathematics Division of an Automatic Computing Engine ACE NB Presented on 1946 03 19 before the Executive Committee of the National Physical Laboratory Great Britain Carpenter Brian Edward Doran Robert William 1977 01 01 October 1975 The other Turing machine The Computer Journal 20 3 269 279 doi 10 1093 comjnl 20 3 269 11 pages Samelson Klaus 1957 1955 Written at Internationales Kolloquium uber Probleme der Rechentechnik Dresden Germany Probleme der Programmierungstechnik in German Berlin Germany VEB Deutscher Verlag der Wissenschaften pp 61 68 NB This paper was first presented in 1955 It describes a number stack Zahlenkeller but names it linear auxiliary memory linearer Hilfsspeicher a b Fothe Michael Wilke Thomas eds 2015 2014 11 14 Written at Jena Germany Keller Stack und automatisches Gedachtnis eine Struktur mit Potenzial Cellar stack and automatic memory a structure with potential PDF Tagungsband zum Kolloquium 14 November 2014 in Jena GI Series Lecture Notes in Informatics LNI Thematics in German Vol T 7 Bonn Germany Gesellschaft fur Informatik GI Kollen Druck Verlag GmbH ISBN 978 3 88579 426 4 ISSN 1614 3213 Archived PDF from the original on 2020 04 12 Retrieved 2020 04 12 1 77 pages Bauer Friedrich Ludwig Samelson Klaus 1957 03 30 Verfahren zur automatischen Verarbeitung von kodierten Daten und Rechenmaschine zur Ausubung des Verfahrens in German Munich Germany Deutsches Patentamt DE PS 1094019 Retrieved 2010 10 01 Bauer Friedrich Ludwig Goos Gerhard 1982 Informatik Eine einfuhrende Ubersicht in German Vol Part 1 3 ed Berlin Springer Verlag p 222 ISBN 3 540 11722 9 Die Bezeichnung Keller hierfur wurde von Bauer und Samelson in einer deutschen Patentanmeldung vom 30 Marz 1957 eingefuhrt Samelson Klaus Bauer Friedrich Ludwig 1959 Sequentielle Formelubersetzung Sequential Formula Translation Elektronische Rechenanlagen in German 1 4 176 182 Samelson Klaus Bauer Friedrich Ludwig 1960 Sequential Formula Translation Communications of the ACM 3 2 76 83 doi 10 1145 366959 366968 S2CID 16646147 IEEE Computer Pioneer Preis Bauer Friedrich L Technical University of Munich Faculty of Computer Science 1989 01 01 Archived from the original on 2017 11 07 Hamblin Charles Leonard May 1957 An Addressless Coding Scheme based on Mathematical Notation PDF typescript N S W University of Technology pp 121 1 121 12 Archived PDF from the original on 2020 04 12 Retrieved 2020 04 12 12 pages Kammerer Wilhelm 1958 Ziffern Rechenautomat mit Programmierung nach mathematischem Formelbild Habilitation thesis in German Friedrich Schiller Universitat Jena Germany Kammerer Wilhelm 1960 Ziffernrechenautomaten Elektronisches Rechnen und Regeln in German Vol 1 Berlin Germany Akademie Verlag Ball John A 1978 Algorithms for RPN calculators 1 ed Cambridge Massachusetts USA Wiley Interscience John Wiley amp Sons Inc ISBN 978 0 471 03070 6 Godse Atul P Godse Deepali A 2010 01 01 Computer Architecture Technical Publications pp 1 56 ISBN 978 8 18431534 9 Retrieved 2015 01 30 Horowitz Ellis 1984 Fundamentals of Data Structures in Pascal Computer Science Press p 67 Pandey Shreesham 2020 06 07 Data Structures in a Nutshell Rochester New York USA SSRN 4145204 Archived from the original on 2022 11 12 Retrieved 2022 11 27 a href Template Cite journal html title Template Cite journal cite journal a Cite journal requires journal help Graham Ronald Ron Lewis 1972 An Efficient Algorithm for Determining the Convex Hull of a Finite Planar Set PDF Information Processing Letters 1 Vol 1 pp 132 133 Archived PDF from the original on 2022 10 22 Aggarwal Alok Klawe Maria M Moran Shlomo Shor Peter Wilber Robert 1987 Geometric applications of a matrix searching algorithm Algorithmica 2 1 4 195 208 doi 10 1007 BF01840359 MR 0895444 S2CID 7932878 Berkman Omer Schieber Baruch Vishkin Uzi 1993 Optimal doubly logarithmic parallel algorithms based on finding all nearest smaller values Journal of Algorithms 14 3 344 370 CiteSeerX 10 1 1 55 5669 doi 10 1006 jagm 1993 1018 Murtagh Fionn 1983 A survey of recent advances in hierarchical clustering algorithms PDF The Computer Journal 26 4 354 359 doi 10 1093 comjnl 26 4 354 This article incorporates public domain material from Paul E Black Bounded stack Dictionary of Algorithms and Data Structures NIST Further reading EditDonald Knuth The Art of Computer Programming Volume 1 Fundamental Algorithms Third Edition Addison Wesley 1997 ISBN 0 201 89683 4 Section 2 2 1 Stacks Queues and Deques pp 238 243 Langmaack Hans 2015 2014 11 14 Written at Kiel Germany Friedrich L Bauers und Klaus Samelsons Arbeiten in den 1950er Jahren zur Einfuhrung der Begriffe Kellerprinzip und Kellerautomat Friedrich L Bauer s and Klaus Samelson s works in the 1950s on the introduction of the terms cellar principle and cellar automaton PDF in German Jena Germany Institut fur Informatik Christian Albrechts Universitat zu Kiel pp 19 29 Archived PDF from the original on 2022 11 14 Retrieved 2022 11 14 11 pages NB Published in Fothe amp Wilke Goos Gerhard 2017 08 07 Geschichte der deutschsprachigen Informatik Programmiersprachen und Ubersetzerbau History of informatics in German speaking countries Programming languages and compiler design PDF in German Karlsruhe Germany Fakultat fur Informatik Karlsruhe Institute of Technology KIT Archived PDF from the original on 2022 05 19 Retrieved 2022 11 14 11 pages External links Edit Wikimedia Commons has media related to Stack data structures Wikibooks has a book on the topic of Data Structures Stacks and Queues Stack Machines the new wave Bounding stack depth Stack Size Analysis for Interrupt driven Programs Retrieved from https en wikipedia org w index php title Stack abstract data type amp oldid 1126628432, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.