Vous êtes sur la page 1sur 18

STACK

DATA STRUCTURES
HISTORY
The stack was first proposed in 1946, in the computer
design of Alan M. Turing (who used the terms "bury"
and "unbury") as a means of calling and returning
from subroutines. The Germans Klaus
Samelson and Friedrich L. Bauer of Technical
University Munich proposed the idea in 1955 and
filed a patent in 1957. The same concept was
developed, independently, by the Australian Charles
Leonard Hamblin in the first half of 1957.
In computer science, a stack is a particular
kind of abstract data type or collection in
which the principal operations on the
collection are the addition of an entity to the
collection, known as PUSH and removal of an
entity, known as POP.
PRINCIPAL OPERATIONS
PUSH
Adds an item to a stack
POP
Extracts the most recently pushed item from the
stack
OTHER OPERATIONS
PEEK
Returns the item at the top without removing it
isEmpty
Determines whether the stack has anything in it
STACK OVERFLOW
A stack may be implemented to have a bounded
capacity. If the stack is full and does not contain
enough space to accept an entity to be pushed, the
stack is then considered to be in an overflow state.
STACK UNDERFLOW
The pop operation removes an item from the top of
the stack. A pop either reveals previously concealed
items or results in an empty stack, but, if the stack is
empty, it goes into underflow state, which means no
items are present in stack to be removed.
APPLICATION
The simplest application of a stack is to reverse a
word. You push a given word to stack - letter by letter
- and then pop letters from the stack.

APPLICATION
Another application is an "undo" mechanism in text
editors; this operation is accomplished by keeping all
text changes in a stack.
APPLICATION
Backtracking. This is a process when you need to
access the most recent data element in a series of
elements. Think of a labyrinth or maze - how do you
find a way from an entrance to an exit?
Once you reach a dead end, you must backtrack. But
backtrack to where? to the previous choice point.
Therefore, at each choice point you store on a stack
all possible choices. Then backtracking simply means
popping a next choice from the stack.
STACK IMPLEMENTATION
An array is a random access data structure, where each
element can be accessed directly and in constant time.
A typical illustration of random access is a book - each
page of the book can be open independently of others.
Random access is critical to many algorithms, for
example binary search.
A linked list is a sequential access data structure, where
each element can be accesed only in particular order. A
typical illustration of sequential access is a roll of paper
or tape - all prior material must be unrolled in order to
get to data you want.

STACK CAN BE IMPLEMENTED USING AN ARRAY
OR USING A LINKED LIST
In an array-based implementation we maintain the
following fields: an array A of a default size ( 1), the
variable top that refers to the top element in the stack
and the capacity that refers to the array size. The
variable top changes from -1 to capacity - 1. We say
that a stack is empty when top = -1, and the stack is full
when top = capacity-1.
In a fixed-size stack abstraction, the capacity stays
unchanged, therefore when top reaches capacity, the
stack object throws an exception.
ARRAY BASED IMPLEMENTATION
ARRAY BASED
IMPLEMENTATION
Implementation of array-based stack is very simple. It
uses TOP variable to point to the topmost stack's element
in the array.
1. Initially TOP= -1;
2. PUSH operation increases TOP by one and writes
pushed element to STORAGE[TOP];
3. POP operation checks that top is not equal to -1 and
decreases top variable by 1;
4. PEEK operation checks that top is not equal to -1 and
returns storage[top];

IMPLEMENTATION USING C++
Global Variables
Int Top
Int capacity
Int *storage;
IMPLEMENTATION USING C++
void stack(int cap)
{
storage=new int[cap];
top=0;
capacity=cap;
}
IMPLEMENTATION USING C++
void push(int value)
{
if(top==capacity)
{
gotoxy(25,14);cout<<"Stack overflow!";
}
else
{
top++;
storage[top]=value;
gotoxy(25,14);cout<<"Item successfully
pushed!";
}
}
IMPLEMENTATION USING C++
void pop()
{
if(top==0)
{
gotoxy(25,13);cout<<"Stack underflow!";
}
else
{
top--;
gotoxy(25,13);cout<<"Item successfully
popped!";
}
}
IMPLEMENTATION USING C++
int peek()
{
if(top==0)
{
cout<<"Stack is Empty!";
}
return storage[top];
}
IMPLEMENTATION USING C++
int peek()
{
if(top==0)
{
cout<<"Stack is Empty!";
}
return storage[top];
}

Vous aimerez peut-être aussi