Stack (data structure)

Stack (data structure)

In computer science, a stack is an 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).

History

The stack method of expression evaluation was first proposed in 1955 and then patented in 1957 by early German computer scientist Friedrich L. Bauer, who received the IEEE Computer Society Pioneer Award in 1988 for his work on "Computer Stacks."

Abstract data type

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 the 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. This can also be written as FILO-First In Last Out, i.e. the record inserted first will be popped out at last.

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" [Horowitz, Ellis: "Fundamentals of Data Structures in Pascal", page 67. Computer Science Press, 1984] (also known as "peek" or "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." node := stack.stackPointer stack.stackPointer := node.next element := node.data return element }

function top("Stack" stack) { "// return 'top' node" return stack.stackPointer.data }

function length("Stack" stack) { "// return the amount of nodes in the stack" length := 0 node := stack.stackPointer while node not null { length := length + 1 node := node.next } return length }

As you can see, these functions pass the stack and the data elements as parameters and return values, not the data nodes that, in this implementation, include pointers. A stack may also be implemented as a linear section of memory (i.e. an array), in which case the function headers would not change, just the internals of the functions.

Implementation

(this is wrtten by mirwais abdullah)A typical storage requirement for a stack of "n" elements is "O(n)". The typical time requirement of O(1) operations is also easy to satisfy with a dynamic array or (singly) linked list implementation.

C++'s Standard Template Library provides a "stack" templated class which is restricted to only push/pop operations. Java's library contains a Javadoc:SE|java/util|Stack class that is a specialization of Javadoc:SE|java/util|Vector. This could be considered a design flaw because the inherited get() method from Javadoc:SE|java/util|Vector ignores the LIFO constraint of the Javadoc:SE|java/util|Stack.

Here is a simple example of a stack with the operations described above (but no error checking) in Python.

class Stack(object): def __init__(self): self.stack_pointer = None def push(self, element): self.stack_pointer = Node(element, self.stack_pointer) def pop(self): e = self.stack_pointer.element self.stack_pointer = self.stack_pointer.next return e

def peek(self): return self.stack_pointer.element

def __len__(self): i = 0 sp = self.stack_pointer while sp: i += 1 sp = sp.next return i

class Node(object): def __init__(self, element=None, next=None): self.element = element self.next = next

if __name__ = '__main__': # small use example s = Stack() [s.push(i) for i in xrange(10)] print [s.pop() for i in xrange(len(s))]

The above is admittedly redundant as Python supports the 'pop' and 'append' functions to lists.

Related data structures

The abstract data type and data structure of the "First In First Out (FIFO)" principle is the queue, and the combination of stack and queue operations is provided by the deque. For example, changing a stack into a queue in a search algorithm can change the algorithm from depth-first search (DFS) into a breadth-first search (BFS). A bounded stack is a stack limited to a fixed size.

Hardware stacks

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 (either up or down, depending on the specific implementation).

For example, a stack might start at a memory location of one thousand, and expand towards lower addresses, in which case new data items are stored at locations ranging below 1000, and the stack pointer is decremented each time a new item is added. When an item is removed from the stack, the stack pointer is incremented.

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:

* "Dup(licate)": the top item is popped and pushed again so that an additional copy of the former top item is now on top, with the original below it.
* "Peek": the topmost item is popped, but the stack pointer is not changed, and the stack size does not change (meaning that 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": 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 either visualized growing from the bottom up (like real-world stacks), or, with the top of the stack in a fixed position (see image), a coin holder ( [http://www.containerstore.com/browse/Product.jhtml?CATID=77006&PRODID=64041] ) or growing from left to right, so that "topmost" becomes "rightmost". This visualization may be independent of the actual structure of the stack in memory. This means that 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 visualisations 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.

Hardware support

Many CPUs have registers that can be used as stack pointers. Some, like the Intel x86, have special instructions that implicitly use a register dedicated to the job of being a stack pointer. Others, like the DEC PDP-11 and the Motorola 68000 family have addressing modes that make it possible to use any of a set of registers as a stack pointer. The Intel 80x87 series of numeric coprocessors has a set of registers that can be accessed either as a stack or as a series of numbered registers. Some microcontrollers, for example some PICs, have a fixed-depth stack that is not directly accessible. Sun's SPARC has a number of register windows organized as a stack which significantly reduces the need to use memory for passing function's arguments and return values.

There are also a number of microprocessors which implement a stack directly in hardware:

* Computer Cowboys MuP21
* Harris RTX line
* Novix NC4016

Many stack-based microprocessors were used to implement the programming language Forth at the microcode level. Stacks were also used as a basis of a number of mainframes and mini computers. Such machines were called stack machines, the most famous being the Burroughs B5000.

Software support

In application programs written in a high level language, a stack can be implemented efficiently using either arrays or linked lists. In LISP there is no need to implement the stack, as the functions push and pop are available for any list. Adobe PostScript is also designed around a stack that is directly visible to and manipulated by the programmer.

Applications

Stacks are ubiquitous in the computing world.

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. Conversion from one form of the expression to another form needs a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code. Most of the programming languages are context-free languages allowing them to be parsed with stack based machines.

For example, The calculation: ((1 + 2) * 4) + 3 can be written down like this in postfix notation with the advantage of no precedence rules and parentheses needed: 1 2 + 4 * 3 +The expression is evaluated from the left to right using a stack:
* push when encountering an operand and
* pop two operands and evaluate the value when encountering an operation.
* push the resultLike the following way (the "Stack" is displayed after "Operation" has taken place):

The final result, 15, lies on the top of the stack at the end of the calculation.

example : implementation in pascal. using marked sequential file as data archives.

{programmer : clx321file : stack.pasunit : Pstack.tpu}program TestStack;{this program use ADT of Stack, i will assume that the unit of ADT of Stack has already existed}

uses PStack; {ADT of STACK}

{dictionary}const mark = '.';

var data : stack; f : text; cc : char; ccInt, cc1, cc2 : integer; {functions} IsOperand (cc : char) : boolean; {JUST Prototype} {return TRUE if cc is operand} ChrToInt (cc : char) : integer; {JUST Prototype} {change char to integer} Operator (cc1, cc2 : integer) : integer; {JUST Prototype} {operate two operands}

{algorithms}begin assign (f, cc); reset (f); read (f, cc); {first elmt} if (cc = mark) then begin writeln ('empty archives !'); end else begin repeat if (IsOperand (cc)) then begin ccInt := ChrToInt (cc); push (ccInt, data); end else begin pop (cc1, data); pop (cc2, data); push (data, Operator (cc2, cc1)); end; read (f, cc); {next elmt} until (cc = mark); end; close (f);end.

Runtime memory management

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.

Forth uses two stacks, one for argument passing and one for subroutine return addresses. The use of a return stack is extremely commonplace, but the somewhat unusual use of an argument stack for a human-readable programming language is the reason Forth is referred to as a "stack-based" language.

Many virtual machines are also stack-oriented, including the p-code machine and the Java virtual machine..

Almost all computer runtime memory environments 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. They 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.

Solving search problems

Solving a search problem, regardless of whether the approach is exhaustive or optimal, needs stack space. Examples of exhaustive search methods are bruteforce and backtracking. Examples of optimal search exploring methods are branch and bound and heuristic solutions. All of these algorithms use stacks to remember the search nodes that have been noticed but not explored yet. The only alternative to using a stack is to use recursion and let the compiler do the remembering for you (but in this case the compiler is still using a stack internally). The use of stacks is prevalent in many problems, ranging from simple in-order traversals of trees or depth-first traversals of graphs to a crossword puzzle solver or computer chess game. Some of these problems can be solved by alternative data structures like a queue, when a different order of traversal is required.

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 to take 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 programming languages (such as C) 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

* Compare with queue, deque
* Call stack
* Stack-based memory allocation
* The computer networking term protocol stack
* Stack machine
*

References

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.
* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. "Introduction to Algorithms", Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 10.1: Stacks and queues, pp.200–204.

External links

* [http://24bytes.com/stack.html Stack program in c++ ]
* [http://www.ece.cmu.edu/~koopman/stack_computers/index.html Stack Machines - the new wave]
* [http://www.cs.utah.edu/~regehr/stacktool Bounding stack depth]
* [http://research.avayalabs.com/project/libsafe/ Libsafe - Protecting Critical Elements of Stacks]
* [http://www.cs.ucla.edu/~palsberg/paper/sas03.pdf Stack Size Analysis for Interrupt-driven Programs] (322 KB)
* [http://www.mycplus.com/utilitiesdetail.asp?iPro=1 Stack Implementation ( Graphical & Text Mode)] C Language implementation of Stack
* Pointers to [http://web-cat.cs.vt.edu/AlgovizWiki/Stacks stack visualizations]
*


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Data structure alignment — is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized …   Wikipedia

  • Container (data structure) — For the abstract notion of containers in Type theory, see Container (Type theory). In computer science, a container is a class, a data structure[1][2], or an abstract data type (ADT) whose instances are collections of other objects. In other… …   Wikipedia

  • Heap (data structure) — This article is about the programming data structure. For the dynamic memory area, see Dynamic memory allocation. Example of a complete binary max heap In computer science, a heap is a specialized tree based data structure that satisfies the heap …   Wikipedia

  • Data structure — In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.[1][2] Different kinds of data structures are suited to different kinds of applications, and some are highly …   Wikipedia

  • Persistent data structure — In computing, a persistent data structure is a data structure which always preserves the previous version of itself when it is modified; such data structures are effectively immutable, as their operations do not (visibly) update the structure in… …   Wikipedia

  • Queue (data structure) — A queue (pronounced /kjuː/) is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and… …   Wikipedia

  • Tree (data structure) — A simple unordered tree; in this diagram, the node labeled 7 has two children, labeled 2 and 6, and one parent, labeled 2. The root node, at the top, has no parent. In computer science, a tree is a widely used data structure that emulates a… …   Wikipedia

  • data structure — noun An organization in software of data that allows more optimal searching, categorizing, or storage of information. Examples: matrix, stack, queue, dequeue, list …   Wiktionary

  • Disjoint-set data structure — In computing, a disjoint set data structure is a data structure that keeps track of a set of elements partitioned into a number of disjoint (nonoverlapping) subsets. A union find algorithm is an algorithm that performs two useful operations on… …   Wikipedia

  • Stack-oriented programming language — A stack oriented programming language is one that relies on a stack machine model for passing parameters. Several programming languages fit this description, notably Forth and PostScript, and also many Assembly languages (but on a much lower… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”