Stack (abstract data type)
View on Wikipedia


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.
Additionally, a peek operation can, without modifying the stack, return the value of the last element added (the item at the top of the stack). The name stack is an 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 removing multiple other items first.[1]
Considered a sequential collection, a stack has one end which is the only position at which the push and pop operations may occur, the top of the stack, and is fixed at the other end, the bottom. A stack may be implemented as, for example, a singly linked list with 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.
History
[edit]Stacks entered the computer science literature in 1946, when Alan Turing used the terms "bury" and "unbury" as a means of calling and returning from subroutines.[2][3] Subroutines and a two-level stack had already been implemented in Konrad Zuse's Z4 in 1945.[4][5]
Klaus Samelson and Friedrich L. Bauer of Technical University Munich proposed the idea of a stack called Operationskeller ("operational cellar") in 1955[6][7] and filed a patent in 1957.[8][9][10][11] In March 1988, by which time Samelson was deceased, Bauer received the IEEE Computer Pioneer Award for the invention of the stack principle.[12][7] Similar concepts were independently developed by Charles Leonard Hamblin in the first half of 1954[13][7] and by Wilhelm Kämmerer with his automatisches Gedächtnis ("automatic memory") in 1958.[14][15][7]
Stacks are often described using the analogy of a spring-loaded stack of plates in a cafeteria.[16][1][17] Clean plates are placed on top of the stack, pushing down any plates already there. When the top plate is removed from the stack, the one below it is elevated to become the new top plate.
Non-essential operations
[edit]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.[18] Since this can be broken down into a "pop" followed by a "push" to return the same data to the stack, 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 include convenience operations that handle common tasks, such as checking if the stack is empty or returning its size.
Software stacks
[edit]Implementation
[edit]A stack can be easily implemented either through an array or a linked list, as it is merely a special case of a list.[19] In either case, what identifies the data structure as a stack 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 (">" 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 std::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.
package org.wikipedia.example;
import java.util.Stack;
public class Example {
public static void main(String[] args) {
Stack<Character> stack = new Stack<>();
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')
}
}
Some processors, such as the PDP-11, VAX, and Motorola 68000 series have addressing modes useful for stack manipulation. The following trivial PDP-11 assembly source code pushes two numbers on a stack and adds them, leaving the result on the stack.
; R0 is assumed to point to a stack area
; -(R0) pre-decrements stack pointer allocating item on stack
; (R0)+ Post-increments stack pointer removing item on stack
;
MOV #12,-(R0) ; Push 12 on stack
MOV #34,-(R0) ; push 34 on stack
ADD (R0)+,(R0) ; Pop 34 off stack and add to 12
; leaving the result on the stack
Hardware stack
[edit]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 processor 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: the address in the stack pointer is adjusted by the size of the data item and a data item is written at the location to which the stack pointer points.
- A pop or pull operation: a data item at the current location to which the stack pointer points is read, and the stack pointer is moved by a distance corresponding to the size of that 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 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 or beyond). 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 twice, such that two copies of the former top item now lie at the top.
- 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 can also be called the top operation.
- 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, where the top is on the far right, or even growing from top to bottom. The important feature is for the bottom of the stack to be 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" nomenclature is used irrespective of whether the stack actually grows 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]
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 that use register windows within a register-stack as another strategy to avoid the use of slow main memory for function arguments and return values.
There is also a number of small microprocessors that implement 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. At least one microcontroller family, the COP400, implements a stack either directly in hardware or in RAM via a stack pointer, depending on the device. Many stack-based microprocessors were used to implement the programming language Forth at the microcode level.
Applications of stacks
[edit]Expression evaluation and syntax parsing
[edit]Calculators that employ 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 to parse syntax before translation into low-level code. Most programming languages are context-free languages, allowing them to be parsed with stack-based machines.
Backtracking
[edit]Another important application of stacks is backtracking. An illustration of this is the simple example of finding the correct path in a maze that contains a series of points, a starting point, several paths and a destination. If random paths must be chosen, then after following an incorrect path, there must be a method by which to return to the beginning of that path. This can be achieved through the use of stacks, as a last correct point can be pushed onto the stack, and popped from the stack in case of an incorrect path.
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]
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.[20]
- Part of the SMAWK algorithm for finding the row minima of a monotone matrix uses stacks in a similar way to Graham scan.[21]
- 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.[22]
- 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.[23]
Security
[edit]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 such pitfalls in these implementations.
As an 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 doing so, 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]- List of data structures
- Queue
- Double-ended queue
- FIFO (computing and electronics)
- Operational memory stack (aka Automatic memory stack)
Notes
[edit]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)
- ^ Blaauw, Gerrit Anne; Brooks, Jr., Frederick Phillips (1997). Computer architecture: Concepts and evolution. Boston, Massachusetts, USA: Addison-Wesley Longman Publishing Co., Inc.
- ^ LaForest, Charles Eric (April 2007). "2.1 Lukasiewicz and the First Generation: 2.1.2 Germany: Konrad Zuse (1910–1995); 2.2 The First Generation of Stack Computers: 2.2.1 Zuse Z4". Second-Generation Stack Computer Architecture (PDF) (thesis). Waterloo, Canada: University of Waterloo. pp. 8, 11. Archived (PDF) from the original on 2022-01-20. Retrieved 2022-07-02. (178 pages)
- ^ 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).)
- ^ a b c d 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. 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 Ausübung des Verfahrens" (in German). Munich, Germany: Deutsches Patentamt. DE-PS 1094019. Retrieved 2010-10-01.
- ^ Bauer, Friedrich Ludwig; Goos, Gerhard [in German] (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.
- ^ Samelson, Klaus; Bauer, Friedrich Ludwig (1959). "Sequentielle Formelübersetzung" [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)
- ^ Kämmerer, Wilhelm [in German] (1958-12-11). Ziffern-Rechenautomat mit Programmierung nach mathematischem Formelbild (Habilitation thesis) (in German). Jena, Germany: Mathematisch-naturwissenschaftliche Fakultät, Friedrich-Schiller-Universität. urn:nbn:de:gbv:27-20130731-133019-7. PPN:756275237. Archived from the original on 2023-10-14. Retrieved 2023-10-14. [2] (2+69 pages)
- ^ Kämmerer, Wilhelm [in German] (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 & Sons, Inc. ISBN 978-0-471-03070-6. LCCN 77-14977.
- ^ 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). "Data Structures in a Nutshell". Dev Genius. 2020. SSRN 4145204.
- ^ 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
[edit]- 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 [in German] (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. Archived (PDF) from the original on 2022-11-14. Retrieved 2022-11-14. (11 pages) (NB. Published in Fothe & Wilke.)
- Goos, Gerhard [in German] (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). Archived (PDF) from the original on 2022-05-19. Retrieved 2022-11-14. (11 pages)
External links
[edit]Stack (abstract data type)
View on GrokipediaFundamentals
Definition and Characteristics
A stack is an abstract data type representing a linear collection of elements that operates according to the Last In, First Out (LIFO) principle, meaning the most recently added element is the first to be removed.[11][8] Elements are added to and removed from a single designated end referred to as the top of the stack.[12] This structure enforces sequential access exclusively through the top, prohibiting direct or random access to other elements.[13] Stacks may be bounded, with a predefined maximum capacity, or unbounded, permitting indefinite growth depending on available resources.[14] Typically, a stack holds elements of a uniform type, ensuring consistency in data handling.[15] In contrast to queues, which adhere to the First In, First Out (FIFO) principle by allowing insertions at one end and removals at the other, stacks restrict both operations to the same endpoint, enabling distinct behavioral patterns for ordered processing.[16] As an abstract data type, the stack is defined solely by its observable behavior and supported operations—such as adding an element to the top or removing the top element—independent of any underlying physical representation or implementation details.[17][18] This abstraction facilitates its use across various programming contexts while maintaining consistent semantics.Essential Operations
The essential operations of the stack abstract data type enable the basic insertion, removal, inspection, and status checking of elements while enforcing the last-in, first-out (LIFO) principle. These core functions—push, pop, top (also called peek), and isEmpty—form the minimal interface for stack usage and are specified abstractly without regard to underlying representation. In the ideal case, each operation achieves constant time complexity, supporting efficient LIFO management.[19][2] The push operation inserts a new element onto the top of the stack, making it the new top element. Abstractly, it appends the element at the current top position and adjusts the top reference to point to this new element. This operation runs in O(1) amortized time in the ideal case, accounting for any potential resizing in bounded realizations, though the abstract specification assumes unbounded capacity.[20]push(S, x)
// S: stack; x: element to insert
place x at the top of S
update top of S to reference x
The pop operation removes the top element from the stack and returns it to the caller. If the stack is empty, pop signals a stack underflow error, typically by throwing an exception or indicating failure, as removing from an empty structure is undefined. Otherwise, it retrieves the top element, eliminates it from the stack, and updates the top reference. Pop executes in O(1) time.[21]
pop(S)
if isEmpty(S)
signal underflow [error](/page/Error)
else
x ← top element of S
remove top element from S
return x
The top operation (or peek) examines and returns the current top element without altering the stack. Like pop, applying top to an empty stack results in an underflow error, as there is no element to inspect. This non-destructive query operates in O(1) time.[21][2]
top(S)
if isEmpty(S)
signal underflow [error](/page/Error)
else
return top element of S
The isEmpty operation determines whether the stack holds any elements, yielding true if vacant and false otherwise. It provides a constant-time O(1) check essential for safely invoking pop or top, preventing underflow conditions. In bounded stack variants, a complementary isFull operation may detect overflow before push, raising an exception if the stack cannot accommodate more elements, though the unbounded abstract model omits this.[19]
isEmpty(S)
return true if S contains no elements, else false
Historical Development
Early Concepts
The concept of the stack as an abstract data type traces its mathematical precursors to early 20th-century developments in logic and algebra, particularly through structures that embody last-in, first-out (LIFO) processing. In 1924, Polish logician Jan Łukasiewicz introduced Polish notation, a prefix notation system for logical expressions that eliminates the need for parentheses by strictly ordering operators and operands, which inherently requires a LIFO evaluation mechanism akin to a stack.[22] This notation laid foundational groundwork for handling nested operations without ambiguity, influencing later computational models of recursion and expression parsing.[22] Earlier concepts of stack-like structures appeared in computing theory, such as Alan Turing's 1946 design for the Automatic Computing Engine (ACE), where he described "bury" and "unbury" operations to manage subroutine calls in a LIFO manner. The stack's formal introduction in computing occurred around 1957, with concurrent contributions from multiple pioneers. Australian philosopher and computer scientist Charles Leonard Hamblin proposed it as a tool for implementing subroutines in early computers, using "push-down lists" to manage nested calls without explicit addresses. In his paper "An Addressless Coding Scheme Based on Mathematical Notation," presented at the First Australian Conference on Computing and Data Processing, Hamblin described a stack-based system inspired by Polish notation to simplify program execution and storage allocation in addressless machines. Independently, German computer scientists Friedrich L. Bauer and Klaus Samelson developed the stack principle for efficient subroutine handling and expression evaluation in the context of ALGOL, patenting their stack machine concept in 1958. These works marked the stack's transition from pure mathematical abstraction to a practical computing primitive, emphasizing its role in recursion and subroutine handling.[23][24] By the early 1960s, stacks gained prominence in theoretical computer science through their integration into formal language theory and automata models. In 1963, Marcel-Paul Schützenberger formalized the equivalence between context-free languages and pushdown automata, where the stack serves as the auxiliary memory to recognize nested structures beyond finite automata capabilities.[25] Building on Noam Chomsky's earlier explorations of pushdown storage in context-free grammars, this work established stacks as essential for modeling recursive linguistic phenomena and computational hierarchies.[25]Evolution in Computing
The integration of the stack abstract data type into programming languages began prominently in the early 1960s, with ALGOL 60 (1960) leveraging stacks to support recursion through dynamic storage allocation for procedure calls, enabling efficient handling of nested activations without explicit memory management.[26] This software-based approach was complemented by hardware innovations, such as the Burroughs B5000 computer (1961), which incorporated dedicated hardware stacks to optimize ALGOL 60 compilation and execution, allowing seamless push and pop operations for operands and control information directly in the processor.[27] In operating systems, stacks played a crucial role in managing system-level interruptions, as exemplified by Multics (operational from 1969), where processor stacks served as per-processor databases to safely store and restore the processor state during external interrupts, ensuring reliable context switching in a time-sharing environment.[28] This use of stacks for interrupt handling influenced subsequent OS designs by providing a disciplined mechanism for preserving execution context, which became foundational for multitasking and resource management. Standardization efforts further solidified the stack's place in computing, with its usage implied in language specifications such as ISO/IEC 13211-1:1995 for Prolog, which describes execution stacks for backtracking and control flow within the language's semantics. Concurrently, the stack's influence extended to modern languages like C (developed in 1972), where call stacks underpin function invocation and recursion, automating activation record management in the runtime environment. Key contributions from figures like Edsger W. Dijkstra, whose 1968 critique of unstructured control flow in "Go To Statement Considered Harmful" advocated for disciplined programming practices aligned with stack-based recursion, propelled the stack's adoption in structured programming paradigms.[29] This emphasis on stack discipline also drove growth in compiler design, where stacks became essential for parsing and code generation, enabling more robust and verifiable software construction.[30]Abstract Operations
Core Operations
The core operations of the stack abstract data type are formally specified using algebraic methods, which define the type through a set of sorts (carrier sets), operation signatures, and axioms that ensure consistent behavior across implementations. The primary sort is Stack, over a universe of elements of sort Element, with the empty stack denoted as a constant empty : → Stack. The constructor operation is push : Stack × Element → Stack, while the observer operations are top : Stack → Element (accessing the top element without removal) and pop : Stack → Element (removing and returning the top element, with the stack updated accordingly). These operations capture the last-in, first-out (LIFO) discipline inherent to the stack.[31] The semantics are encapsulated in algebraic axioms that relate the operations. For any stack S and element x, the axioms state:- top(push(S, x)) = x
- pop(push(S, x)) = x (with the resulting stack equal to S)
- push(S, x) → S': No precondition. Postcondition: top(S') = x and the stack resulting from popping S' equals S, meaning the original stack is recovered by popping the newly added element, and all prior elements remain unchanged below it.
- pop(S) → x: Precondition: S ≠ empty. Postcondition: x is the top element of S, and the resulting stack has size one less than S.
- top(S) → x: Precondition: S ≠ empty. Postcondition: x is the top element of S, without altering S.
Auxiliary Operations
Auxiliary operations on a stack abstract data type provide additional functionality beyond the core last-in, first-out (LIFO) insertions and deletions, enhancing usability while preserving the fundamental model. These operations are typically optional and not required for the minimal definition of a stack, allowing implementations to include them as needed without compromising the LIFO principle.[35] The size operation returns the number of elements currently stored in the stack. In the abstract model, this can be achieved in constant time, O(1), by maintaining a counter that increments on pushes and decrements on pops.[35][36] For bounded stacks with a fixed capacity, the isFull operation checks whether the stack has reached its maximum size, returning true if no further pushes are possible. This operation is also O(1) in the abstract model, often by comparing the current size against the predefined limit, though it is irrelevant for unbounded stacks using dynamic allocation.[37][19] Some stack definitions include a search or contains operation to determine if a specified element is present in the stack. This requires a linear scan starting from the top and examining each element sequentially until a match is found or the bottom is reached, resulting in O(n) time complexity in the worst case, where n is the number of elements.[38][39] The clear operation, sometimes termed makeEmpty, removes all elements from the stack, restoring it to an empty state. Abstractly, this is O(1) by resetting the top pointer or counter, though actual implementations may require O(n) time to deallocate memory or reinitialize structures.[37][40] These auxiliary operations are considered non-essential because they extend beyond the pure LIFO discipline; for instance, search disrupts the stack's restricted access model by necessitating traversal of internal elements, potentially encouraging designs better suited to other data types like lists. Including them introduces trade-offs in efficiency and conceptual purity, as stacks are optimized for top-only access rather than arbitrary queries.[35][37]Software Implementations
Array-Based Implementation
The array-based implementation of a stack employs a dynamic array to store elements contiguously in memory, maintaining a pointer to the top element for efficient access. This structure typically includes an arrayA of initial capacity n and a variable size (or top + 1) tracking the current number of elements, with the top element at A[size - 1]. For an empty stack, size is 0. This approach realizes the abstract push, pop, and top operations through simple index manipulations.[41]
The fundamental operations are implemented with the following pseudocode, incorporating dynamic resizing to handle variable sizes:
Push operation:
push(obj):
if size == capacity:
new_capacity = 2 * capacity
create new array B of size new_capacity
copy A[0..size-1] to B[0..size-1]
set A = B
capacity = new_capacity
A[size] = obj
size = size + 1
This adds the new element at the next available index and doubles the array capacity if full, followed by copying elements to the larger array.[41]
Pop operation:
pop():
if size == 0:
error "empty stack"
size = size - 1
item = A[size]
if size < capacity / 4 and capacity > 1:
new_capacity = capacity / 2
create new array B of size new_capacity
copy A[0..size-1] to B[0..size-1]
set A = B
capacity = new_capacity
return item
This removes and returns the top element, halving the capacity if the stack is less than one-quarter full to reclaim space.[41]
Top operation:
top():
if size == 0:
error "empty stack"
return A[size - 1]
This simply returns the top element without modifying the stack.[41]
In terms of space efficiency, the contiguous allocation enables O(1) time for push, pop, and top operations under fixed capacity, as indexing is direct. However, fixed-size arrays risk overflow when full, necessitating a predefined maximum size that may lead to underutilization or runtime errors if exceeded. Dynamic resizing addresses this by allowing unbounded growth, achieving amortized O(1) time per operation over a sequence, as the total cost of n pushes is O(n) despite occasional O(n) resizes.[42][41]
The primary advantages include simplicity in indexing and constant worst-case time for non-resizing operations, making it suitable for scenarios with predictable sizes. Limitations arise from resizing overhead, which incurs O(n) time and space for copying during growth or shrinkage, and potential wasted space when the array is underutilized (e.g., load factor below 25%).[42][41]
For example, consider an initial capacity of 4: pushing elements 1 through 4 fills the array (size=4). A fifth push (element 5) triggers resizing to capacity 8, copying the four elements and adding 5, resulting in O(4) copy time but enabling continued operations. Subsequent pops reducing size below 2 would halve capacity back to 4, copying the remaining elements. This strategy balances space usage while maintaining average efficiency.[41]
Linked-List-Based Implementation
The linked-list-based implementation of a stack utilizes a singly linked list, where each node consists of a data field to store the element and a next pointer referencing the subsequent node in the list. The top of the stack is represented by a reference to the head node of this list, enabling direct access for insertion and removal at the front. This structure supports the LIFO principle by treating the head as the most recently added element.[43] The core operations leverage pointer manipulation for constant-time execution. For the push operation, a new node is allocated with the given data, its next pointer is set to the current head, and the head reference is updated to this new node. Pseudocode for push is as follows:function push([data](/page/Data)):
new_node ← allocate Node([data](/page/Data))
new_node.next ← head
head ← new_node
This prepends the element in amortized O(1) time. The pop operation checks if the head is null (indicating an empty stack); if not, it retrieves the data from the head node, updates the head to the next node, deallocates the old head node, and returns the data. Pseudocode for pop is:
function pop():
if head = null:
throw EmptyStackException
data ← head.data
temp ← head
head ← head.next
deallocate temp
return data
This removes the top element in O(1) time. Other operations like peek (returning head.data without removal) and isEmpty (checking if head is null) also run in O(1) time. All primary stack operations achieve worst-case O(1) time complexity due to localized pointer updates at the head.[44][45]
In terms of space, the implementation uses exactly O(n) memory for n elements, with each node requiring storage for the data plus the next pointer, resulting in additional overhead per element compared to contiguous structures. Unlike fixed-size arrays, no preallocation or resizing is needed, allowing the stack to grow unbounded as long as memory is available, naturally preventing capacity overflow.[43][44]
This approach offers advantages in flexibility, supporting dynamic growth without size limits or relocation costs. However, it incurs higher memory overhead from the per-element pointers and exhibits poorer cache performance due to non-contiguous node allocation, which reduces spatial locality during traversal or access.[45][46]
To illustrate, consider an example trace starting with an empty stack (head = null):
- Push(10): Allocate node with data=10, next=null; head points to this node.
- Push(20): Allocate node with data=20, next=pointer to 10's node; head points to 20's node (stack: 20 → 10).
- Push(30): Allocate node with data=30, next=pointer to 20's node; head points to 30's node (stack: 30 → 20 → 10).
- Pop(): Retrieve 30, deallocate 30's node, head now points to 20's node (stack: 20 → 10).
- Pop(): Retrieve 20, deallocate 20's node, head points to 10's node (stack: 10).
Integration with Programming Languages
Built-in Stack Support
Many programming languages provide built-in support for stacks through standard libraries or native data structures, allowing developers to leverage LIFO semantics without implementing the abstract data type from scratch. These facilities often build on core operations like push, pop, and peek, adapting general-purpose containers to enforce stack behavior.[47] In Java, thejava.util.Stack class, introduced in JDK 1.0, implements a stack by extending the Vector class and adding methods such as push, pop, peek, empty, and search.[47] However, this class has been considered legacy since Java 1.2, with the recommendation to use the Deque interface (implemented by classes like ArrayDeque) for more efficient and flexible LIFO operations, as Stack inherits the synchronization overhead and dynamic array resizing from Vector.[48] Python, in contrast, lacks a dedicated stack class but endorses using built-in lists for this purpose, where append() serves as push to add elements to the end and pop() without arguments retrieves and removes the last element, providing efficient O(1) amortized operations for stack usage.[49]
C++ offers std::stack in the <stack> header as a container adaptor from the Standard Template Library (STL), which wraps an underlying sequence container—defaulting to std::deque for efficient push and pop from either end—and exposes only stack-specific methods like push, pop, top, empty, and size. This design promotes reusability by allowing substitution of other containers like std::vector or std::list via templates. In functional languages such as Haskell, built-in lists function as stacks through operations like cons (:) for push and pattern matching with head and tail for peek and pop, leveraging the language's immutable, singly-linked list structure for recursive processing.[50]
Built-in stack support often incorporates thread-safety considerations, particularly in concurrent environments. Java's Stack class is thread-safe because it inherits synchronized methods from Vector, ensuring atomic operations like push and pop across multiple threads, though this comes at the cost of performance due to blocking. Modern alternatives like ArrayDeque are not synchronized by default, requiring manual synchronization (e.g., using synchronized blocks) or the use of concurrent collections such as ConcurrentLinkedDeque for thread-safe LIFO operations if needed in multi-threaded environments.[51]
Over time, stack support in languages has evolved from dedicated, non-generic classes to generic or adaptable containers, enhancing type safety and flexibility. For instance, C#'s non-generic System.Collections.Stack (introduced in .NET Framework 1.0) has been superseded by the generic System.Collections.Generic.Stack<T> since .NET 2.0, which uses an internal array for storage and supports type parameterization to avoid boxing/unboxing overhead for value types.[52] This shift mirrors broader trends in languages like Java and C++, where generics and adaptors replace rigid classes, reducing duplication and improving performance in diverse applications.[53]
Custom Stack Usage
Developers often implement custom stacks to address specific performance requirements or integration needs that exceed the capabilities of built-in data structures, such as optimizing memory allocation in resource-constrained environments. For instance, in game development, a custom array-based stack can be used to manage undo operations for player moves, where rapid push and pop actions are critical for real-time responsiveness; this approach avoids the overhead of dynamic resizing in standard libraries by pre-allocating a fixed-size array tailored to the game's maximum move history. Custom stacks are frequently integrated into graph algorithms like depth-first search (DFS), where they enable iterative traversal without recursion to mitigate stack overflow risks in deep graphs. In DFS, the stack stores vertices to explore, pushing neighbors of the current vertex and popping to backtrack once a path is exhausted. The following pseudocode illustrates a basic iterative DFS implementation using a custom stack:procedure DFS(graph, start_vertex):
visited = set()
stack = Stack() // Custom stack initialized empty
stack.push(start_vertex)
while stack is not empty:
vertex = stack.pop()
if vertex not in visited:
visit(vertex)
visited.add(vertex)
for neighbor in graph.neighbors(vertex):
if neighbor not in visited:
stack.push(neighbor)
This method ensures O(V + E) time complexity, where V is vertices and E is edges, by explicitly controlling stack operations for predictability in custom scenarios.[54]
When choosing between array-based and linked-list-based custom stacks, developers should consider access patterns and memory constraints: array-based stacks offer O(1) constant-time operations with better cache locality for frequent pushes and pops in bounded scenarios, but require pre-known maximum sizes to avoid resizing overhead; linked-list-based stacks provide dynamic growth without size limits at the cost of higher memory usage due to node pointers, making them suitable for unpredictable depths.[44] Best practices include using generics or templates—for example, Java's generics or C++ templates—to ensure type safety in custom implementations, allowing the stack to handle diverse data types while maintaining compile-time checks.[44]
In text editors, custom stacks facilitate advanced undo/redo functionality by storing not just states but versioned deltas, such as character insertions or deletions, to enable efficient reversal without full document snapshots. For example, an undo stack might push operation records (e.g., position and text change) on each edit, popping and applying the inverse for undo, while a separate redo stack captures forward actions; this design supports multi-step versioning, limiting stack size to prevent excessive memory use by capping history depth at 100 operations.[55] While built-in stacks in languages like Python or Java provide alternatives, custom versions allow integration of domain-specific optimizations, such as compression for large documents.[55]
Hardware Implementations
Main Memory Stacks
In computer architectures like x86, main memory stacks are implemented within the process's virtual address space in RAM, serving as a dedicated segment for dynamic allocation during program execution. The stack segment is typically positioned at the higher end of the address space, starting from high memory addresses and growing downward toward lower addresses as elements are added. This downward growth convention facilitates efficient management of the stack pointer register (e.g., ESP or RSP), which tracks the top of the stack.[56][57] Hardware support for stack operations in main memory is provided through dedicated instructions in the processor's instruction set. In x86 assembly, the PUSH instruction decrements the stack pointer by the size of the operand (typically 4 or 8 bytes) and stores the value at the new top address, effectively adding an element to the stack. Conversely, the POP instruction loads the value from the current top address into a register or memory and then increments the stack pointer to remove it. These operations ensure atomicity and efficiency, directly mapping to the abstract stack's push and pop primitives in a single sentence of reference.[58][59] Operating systems handle the allocation and oversight of main memory stacks to prevent resource exhaustion. In Unix-like systems, the kernel enforces a per-process stack size limit configurable via the RLIMIT_STACK resource, often set using the ulimit shell command (e.g., ulimit -s 8192 for 8 MB). The default limit is commonly 8 MB, beyond which attempts to grow the stack trigger a page fault, leading the kernel to deliver a SIGSEGV (segmentation violation) signal to the process, effectively detecting and handling stack overflow exceptions. This mechanism protects system stability by terminating or signaling the offending process.[60][61] A key example of main memory stacks in action is the call stack employed during function calls in procedural programming. Each invocation creates a new stack frame pushed onto the call stack, containing the return address (for resuming the caller), local variables, and saved register values (such as the base pointer for frame access). As functions return, frames are popped in last-in-first-out order, restoring the execution context and deallocating the frame's memory. This structure enables recursive and nested calls while maintaining isolation between activation records.[62][59]Register or Cache Stacks
Register stacks refer to hardware implementations where a fixed-size array of CPU registers functions as a stack, enabling rapid push and pop operations without accessing slower main memory. These are common in stack machine architectures, where the top elements of the stack reside in dedicated registers for efficiency. For instance, the Burroughs B5500 employed a stack mechanism with registers A, B, and S to hold the top-of-stack values, allowing microcode to perform operations directly on these registers. This design supported a deep effective stack depth by combining register residency with spillover to memory when needed.[63] In the VAX architecture, the stack pointer (register 14, SP) and frame pointer (register 13, FP) facilitate stack-based operations, treating a subset of the 16 general-purpose registers as part of the call stack for procedure management. Push and pop instructions in such systems are typically implemented at the microcode level, adjusting the stack pointer and transferring data between registers in a single clock cycle, which minimizes latency compared to memory-bound stacks. The primary advantage is enhanced speed, as register access times are orders of magnitude faster than main memory, reducing overhead in compute-intensive tasks.[64] In modern pipelined processors, the register file provides high-speed storage for temporary values and operands during instruction execution, acting in a stack-like manner for certain operations. When register pressure exceeds available resources, values spill over to main memory via compiler-managed stores, maintaining pipeline throughput. This spilling mechanism leverages the memory stack within cache hierarchies, with overflows handled transparently to avoid stalling the pipeline. In the Forth programming language, the return stack—used for control flow and return addresses—is often optimized by mapping its top elements to CPU registers, bypassing memory for frequent accesses and improving execution speed. Similarly, modern GPUs utilize per-thread register files to manage local variables and temporaries across thousands of threads, with spilling to local memory when registers are insufficient. Research has proposed concurrency-aware register stacks to handle function calls more efficiently, but as of 2025, such mechanisms are not yet implemented in commercial GPU architectures.[65][66]Applications
Expression Evaluation and Parsing
Stacks play a crucial role in evaluating expressions in postfix notation, also known as Reverse Polish Notation (RPN), where operators follow their operands, avoiding the need for parentheses to denote precedence. The evaluation algorithm scans the expression from left to right, using a stack to store intermediate results. Operands are pushed onto the stack, while encountering an operator triggers popping the required number of operands (typically two for binary operators), applying the operation, and pushing the result back onto the stack. This process continues until the expression is fully processed, with the final result remaining on the top of the stack.[67] Consider the postfix expression "2 3 + 4 *", which corresponds to (2 + 3) * 4. The steps are as follows: push 2; push 3; encounter +, pop 3 and 2, compute 5, push 5; push 4; encounter *, pop 4 and 5, compute 20, push 20. The stack now holds 20, the result. The LIFO nature of the stack ensures that the most recently encountered operands are used first, aligning with operator precedence requirements.[67] This algorithm achieves O(n time complexity, where n is the number of tokens, as each token is processed exactly once with constant-time stack operations.[67] To convert infix expressions to postfix notation, the shunting-yard algorithm employs a stack to manage operators based on their precedence and associativity. Invented by Edsger W. Dijkstra in 1961, the algorithm processes input tokens sequentially: operands are output directly to a queue; for an operator, operators of equal or higher precedence are popped from the stack to the output until the condition no longer holds, then the current operator is pushed; parentheses are handled by pushing the opening parenthesis and popping operators until the matching closing one is found. At the end, any remaining operators on the stack are output. This produces a postfix expression ready for stack-based evaluation.[68] In syntactic parsing, particularly for compiler design, stacks facilitate the construction of syntax trees using recursive descent methods, where the call stack implicitly manages recursion to match grammar rules top-down. Each non-terminal in the grammar corresponds to a recursive procedure that consumes input tokens and builds subtree nodes, pushing and popping states as needed to handle nested structures. Recursive descent parsers, suitable for LL(k) grammars, leverage this stack mechanism to predict and verify syntactic validity efficiently. The foundational work on LL parsers by Lewis and Stearns in 1968 formalized top-down parsing strategies that integrate stack operations for lookahead and error recovery in compilers.[69]Backtracking and Search
Stacks play a crucial role in depth-first search (DFS) algorithms for traversing graphs or trees, where they maintain the current path and enable efficient backtracking upon reaching dead-ends. In DFS, exploration proceeds deeply along one branch before retreating to try alternatives, aligning with the stack's last-in, first-out (LIFO) structure. The stack typically stores nodes representing the path from the starting vertex; unvisited neighbors are pushed onto the stack to extend the path, while popping occurs when no further progress is possible, allowing the algorithm to backtrack and explore other branches. A visited set prevents revisiting nodes, ensuring termination and avoiding cycles. This stack-based approach is particularly effective for finding paths, detecting cycles, or computing connected components in graphs.[70] The following pseudocode illustrates an iterative implementation of DFS using a stack, adapted for path exploration in an undirected graph:procedure IterativeDFS(graph, startVertex):
stack = new Stack() // Stores vertices in current path
visited = [empty set](/page/Empty_set)
stack.push(startVertex)
visited.add(startVertex)
while stack is not empty:
current = stack.peek() // Examine top without popping yet
if has unvisited neighbors(current):
nextVertex = select unvisited neighbor of current
stack.push(nextVertex)
visited.add(nextVertex)
else:
// Dead-end: backtrack by popping
pathNode = stack.pop()
// Process or record pathNode as needed (e.g., for path reconstruction)
// The stack contents at any point represent the current path from start
In this algorithm, neighbors are pushed to deepen the search, and popping handles backtracking at dead-ends, with the stack implicitly tracking the path for reconstruction upon finding a goal. For explicit path storage, each stack entry can include parent pointers or coordinates. This method contrasts with recursive DFS by explicitly using the stack to simulate the call stack, making it suitable for deep graphs where recursion depth might overflow.[71]
Backtracking algorithms leverage stacks to build and dismantle partial solutions incrementally, ideal for combinatorial problems requiring exhaustive yet efficient search. The stack holds the current partial solution, such as a sequence of choices, allowing the addition of new elements (push) if they satisfy constraints and removal (pop) upon violation to restore the previous state. This enables a depth-first exploration of the solution space, pruning invalid branches early to avoid unnecessary computation. Backtracking is widely used in constraint satisfaction problems, where the stack ensures reversible trial-and-error without losing prior progress.[72]
A classic example is the N-Queens puzzle, which places N queens on an N×N chessboard such that no two share the same row, column, or diagonal. Here, the stack stores the column index for each row in the current placement, starting empty for row 0. For the current row, the algorithm tries columns 0 to N-1: it pushes the chosen column if no conflicts with prior queens (checked via row, column, and diagonal attacks), then advances to the next row. If a conflict arises or the row is fully tried without success, it pops the last choice and increments the trial for that row. Success occurs when the stack size reaches N, yielding a valid board configuration; otherwise, continued popping backtracks until an alternative is viable or the search exhausts. This stack-driven process can find all solutions or the first one, with time complexity exponential in N but practical for small N due to pruning. Pseudocode for the core loop might resemble:
procedure SolveNQueens(currentRow, stack, boardSize):
if currentRow == boardSize:
// Solution found: stack holds column positions for rows 0 to N-1
output solution
return
for col = 0 to boardSize-1:
if no conflict with stack (check attacks):
stack.push(col)
SolveNQueens(currentRow + 1, stack, boardSize)
stack.pop() // Backtrack regardless of success
Although often implemented recursively, the stack explicitly manages partial solutions in iterative variants, simulating recursion.[72]
Stacks facilitate undo mechanisms in reversible computations, particularly within backtracking searches, by storing snapshots of states or actions for restoration after failed trials. Each push records a decision or modification (e.g., variable assignments or board placements), while popping reverts to the preceding state, undoing changes in reverse order of application. This LIFO reversal ensures precise recovery, preventing contamination from discarded paths and supporting iterative refinement in algorithms like puzzle solvers or optimization under constraints. In backtracking contexts, the undo stack often pairs with a trail of modifications, allowing efficient restoration without recomputing prior valid states. Such mechanisms are essential for maintaining computational integrity in exploratory processes where multiple alternatives must be tested sequentially.[73]
Maze solving exemplifies stack usage in pathfinding, employing DFS to navigate from start to goal while avoiding walls and loops. The stack stores positions (e.g., (x, y) coordinates) forming the current path, initialized by pushing the starting cell. In each iteration, the top position is examined: if it's the goal, the stack traces the solution path; otherwise, valid unvisited adjacent cells (typically up, down, left, right, if passable and unmarked) are pushed in a chosen order to explore depth-first. Upon exhausting moves from the current cell (dead-end), it is popped, backtracking to the previous position for alternative routes. Visited cells are marked to prevent cycles, and the process continues until the goal is reached or the stack empties (no path exists). This yields a valid path if one exists, often the first discovered due to search order, with the stack's contents providing the route upon success. For a 10×10 maze, this might explore hundreds of positions before resolving, highlighting the stack's role in scalable navigation.[74]
