<< Chapter < Page | Chapter >> Page > |
(From Wikipedia, the free encyclopedia)
In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used extensively at every level of a modern computer system. For example, a modern PC uses stacks at the architecture level, which are used in the basic design of an operating system for interrupt handling and operating system function calls. Among other uses, stacks are used to run a Java Virtual Machine, and the Java language itself has a class called "Stack", which can be used by the programmer. The stack is ubiquitous.
A stack-based computer system is one that stores temporary information primarily in stacks, rather than hardware CPU registers (a register-based computer system).
(From Wikipedia, the free encyclopedia)
As an abstract data type, the stack is a container of nodes and has two basic operations: push and pop. Push adds a given node to the top of the stack leaving previous nodes below. Pop removes and returns the current top node of the stack. A frequently used metaphor is the idea of a stack of plates in a spring loaded cafeteria stack. In such a stack, only the top plate is visible and accessible to the user, all other plates remain hidden. As new plates are added, each new plate becomes the top of the stack, hiding each plate below, pushing the stack of plates down. As the top plate is removed from the stack, they can be used, the plates pop back up, and second plate becomes the top of the stack. Two important principles are illustrated by this metaphor, the Last In First Out principle is one. The second is that the contents of the stack are hidden. Only the top plate is visible, so to see what is on the third plate, the first and second plates will have to be removed.
Operations
In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top (also known as peek and peak) can return the current top element of the stack without removing it from the stack.
This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers.
record Node {
data // The data being stored in the node
next // A reference to the next node; null for last node
}
record Stack {
Node stackPointer // points to the 'top' node; null for an empty stack
}
function push(Stack stack, Element element) { // push element onto stack
new(newNode) // Allocate memory to hold new node
newNode.data := element
newNode.next := stack.stackPointer
stack.stackPointer := newNode
}
function pop(Stack stack) {// increase the stack pointer and return 'top' node
// You could check if stack.stackPointer is null here.
// If so, you may wish to error, citing the stack underflow.
Notification Switch
Would you like to follow the 'Data structures and algorithms' conversation and receive update notifications?