2. Introduction
A stack is a non-primitive linear data structure.
It is an ordered list in which addition of new data
item and deletion of already existing data items done
from only one end, known as top tacks (TOP).
As all the deletion and insertion in a stack is done
from top of the stack, the last added element will be
the first to be removed from the stack.
That is the reason why stack is also called LAST-IN-
FIRST-OUT (LIFO) type of list.
3. Introduction
Example 1: A common model of a stack is plates
in a marriage party. Fresh plates are “pushed” onto
to the top and “popped” off the top.
Example 2: Some of you may eat biscuits. If you
assume only one side of the cover is open and
biscuits are taken off one by one from one side.
4. Introduction
Whenever a stack is created the stack base remains
fixed, as a new element is added to the stack from
the top, the top goes on increasing.
Conversely as the top most elements of the stack
is removed the stack top is decrementing.
Show the next slide figure to various stages of
stack top during insertion and during deletion.
5. Introduction
Stack top increases during insertion
Stack empty
top=-1
4
3
2
1
0 10
Insert first element
Top=0
4
3
2
1
0
20
10
Top=1
Insert second element
0
1
2
3
4
6. Introduction
Stack top decreases during deletion
Element 10 deleted
4
3
2
1
0 10
Element 20 deleted
Top=0
4
3
2
1
0
20
10
Stack Initially
0
1
2
3
4
Top=1
Top=-1
7. Stack Implementation
Stack can be implemented in two ways:
Static implementation
Dynamic implementation
Static implementation uses arrays to create stack.
Static implementation though a very simple
technique but is not a flexible way of creation, as
the size of stack has to be declared during program
design, after that the size cannot be varied.
Moreover static implementation is not too efficient
with respect to memory utilization.
8. Stack Implementation
As the declaration of array is done before the start
of the operation, now if there are too few elements
to be stored in the stack the statically allocated
memory will be wasted.
On the other hand if there are more number of
elements to be stored in the stack then we can’t be
able to change the size of array to increase its
capacity.
9. Stack Implementation
Dynamic implementation is also called linked
list representation and uses pointers to implement
the stack type of data structure.
10. Operations on Stack
The basic operation that can be performed on
stack are as follows:
PUSH :The process of adding a new element to
the top of stack is called PUSH operation. when
new element will be inserted at the top after every
push operation that top is incremented by one. In
case the array is full and no new element can be
accommodated, it is called STACK-FULL
condition. This condition is called STACK
OVERFLOW.
11. Operations on Stack
POP: The process of deleting an element from the
top of stack is called POP operation. After every pop
operation the stack is decremented by one. If there is
no element on the stack and the pop is performed
then this will result into STACK UNDERFLOW
condition.
PEEP: If one is interested only about an information
stored at some location in a stack then peep
operation is required. In short we can say extract any
position information from the stack.
UPDATE: Update operation is required when the
content of some location in a stack is to be changed.
12. Stack Terminology
MAXSIZE: This term is not standard one, we use
this term to refer the maximum size of stack.
TOP: This term refers to the top stack (TOS). The
stack top is used to check stack overflow or
underflow conditions. Initially TOP stores -1. this
assumption is taken so that whenever an element
is added to the stack the TOP is first incremented
and then the item is inserted into the location
currently indicated by the TOP.
13. Stack Terminology
STACK UNDERFLOW: This is the situation
when the stack contains no element. At this point
the top of stack is present at the bottom of the
stack.
STACK OVERFLOW: This is the situation when
the stack becomes full, and no more elements can
be pushed onto the stack. At this point the stack
top is present at the highest location of the stack.
14. Applications of Stack
There are various applications of Stack:
Recursion: Recursion is an important facility
in many programming language, such as
PASCAL and C ect.
Some machine are also known which use built-
in stack hardware called ‘stack machine’.
Representation of Polish Notation
15. Recursion:
Recursion is the name gives to a technique for
defining a function in terms of itself.
If is define as “a function call itself, thus chain of
process occurs.”
There are two important conditions that must be
satisfied by any recursive procedure:
Each time a procedure calls itself, it must be
“nearer” in some sense solution.
There must be a decision criterion for stopping
the process or computation.
16. Recursion:
There are three popular implementation of
recursive computations:
Calculation of factorial value
Quick sort
Tower of Hanoi problem
Very simple example is to find Factorial value for
an integer n:
n!=n*(n-1)*(n-2)*……..*3*2*1
n!=n*(n-1)!
17. Recursion:
What is the simple implementation of
factorial calculation:
1. fact=1
2. For(i=1 to N) do
3. Fact=i*fact
4. End for
5. Return(fact)
6. Stop
18. Recursion:
Now let us see the recursive definition of the same.
1. if (N==0)
2. fact=1
3. else
4. fact=N*FACTORIAL(N-1)
5. end if
6. return (fact)
7. Stop
This recursive definition to implementation using
stack show in word file.
19. Tower of Hanoi Problem
Another complex recursive problem is the tower of
Hanoi problem.
This problem has a historical basis in the ritual of
ancient Vietnam.
The problem can be described as below:
Suppose, there are three pillars A,B,C. there are N
discs of decreasing size so that no two discs are of
the same size.
Initially all the discs are stacked on one pillar in
their decreasing order of size.
20. Tower of Hanoi Problem
Let this pillar be A. other two pillars are empty.
The problem is to move all the discs from one
pillar to other using as auxiliary so that
Only one disc may be moved at a time.
A disc may be moved from any pillar to
another.
At no time can a larger disc be placed on a
smaller disc.
21. Tower of Hanoi Problem
The solution of this problem is: Move N discs
from Pillar A to C via the pillar B means:
Move first (n-1) discs from pillar A to B
Move the disc from pillar A to C
Move all (n-1) discs from pillar B to C
This problem implementation to show in
word file.
22. Polish Notation
The process of writing the operators of an
expression either before their operands or after
them is called the polish notation.
The honors of its discoverer, the polish
mathematician JAN LUKSIEWICZ.
The fundamental property of polish notation is that
the order in which the operations are to be
performed is completely determined by the
positions of the operators and operands in the
expression.
23. Polish Notation
The polish notation are classified into three
categories.
These are:
Infix notation
Prefix notation
Postfix notation
24. Introduction
Operator Precedence Table:
Operator Name Operator Highest Precedence Association
All types of
Brackets
(),[],{} 1 Left-Right
Exponential ^ or $ or
↑
2 Right-Left
Mulitplication/
Devision
*,/ 3 Left-Right
Addition/
Subtraction
+,- 4 Left-Right
29. Examples
To solves these examples:
(A+B)*C/D+E^F/G
Ans: AB+C*D/EF^G/+
A-B/(C*D^E)
Ans: ABCDE^*/-
(a + b c d) * (e + f / d)
↑ ↑
Ans:abcd +efd/+*
↑↑
33. Examples
Postfix expression to Infix form: to evaluate
expression from left to right
AB*C+
=(A*B)C+
=TC+ :A*B=T
=T+C
=A*B+C :put the value of T