DS-Lecture 9 (Stack)
DS-Lecture 9 (Stack)
STACK
Dr. M Naveed
Stack
• A stack is a list of elements in which an
element may be inserted or deleted only
at one end, called the top of the stack
AL 2
Operation on Stacks
• The following two basic operations
associated with stacks:
3
Example
• Suppose the following 6 elements are
pushed, in order onto an empty stack:
AAA, BBB, CCC, DDD, EEE, FFF
4
AL 5
Array Representation of Stack
AL 7
Example
AL 8
Push and Pop Algorithm
• The operation of adding (pushing) an
item onto a stack
AL 9
PUSH Algorithm
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a
stack
AL 10
PUSH Algorithm contd…
2. Set TOP := TOP + 1
[Increases TOP by 1]
4. Return
AL 11
POP Algorithm
POP(STACK, TOP, ITEM)
This procedure deles the top element of
STACK and assigns it to the variable
ITEM
AL 12
POP Algorithm
2. Set ITEM := STACK[TOP]
[Assigns TOP element to ITEM]
4. Return
AL 13
Usage of Stack
• Stack are used in several different
problems
– Recursion
– Quick Sort
– Polish Notation
– etc
AL 14
Polish Notation
• Expression involving constraints and
operations (binary operation)
AL 15
Polish Notation contd…
• The prefixes “pre” and “post” refer to the
position of the operator with respect to the
two operands
AL 16
Polish Notation contd…
• Consider the infix expression
A+B*C
• We “know” that multiplication is done
before addition.
• The expression is interpreted as
A+(B*C)
(A + B ) * C infix form
Polish Notation contd…
• Conversion to postfix
(A + B ) * C infix form
(AB+)*C convert addition
(AB+)C* convert multiplication
Polish Notation contd…
• Conversion to postfix
(A + B ) * C infix form
(AB+)*C convert addition
(AB+)C* convert multiplication
AB+C* postfix form
Infix to Prefix
• Conversion to prefix
( A+B)*C Infix
(+AB)*C convert addition
*+ABC convert multiplication
Infix to Prefix contd…
• Conversion to prefix
A+(B*C) Infix
A+(*BC) convert multiplication
+A*BC convert addition
Infix to Prefix contd…
• Conversion to prefix
(A + B)/(C – D)
Precedence of Operators
• The five binary operators are: addition,
subtraction, multiplication, division and
exponentiation
• Exponentiation
• Multiplication/division *, /
• Addition/subtraction +, -
Precedence of Operators
• For operators of same precedence, the
left-to-right rule applies:
A B C means A ( B C )
Infix to Postfix
Infix Postfix
A+B AB+
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) AB+CD–*
AL 31
Importance of Polish Notation
• No parentheses
AL 32
AL 33
AL 34
Stack for Polish Notation
• Conversion of Infix notation to postfix notation
using STACK
• Evaluation of Postfix expression using STACK
Algorithm for conversion
from infix to postfix
Q: A+(B*C-(D/E^F)*G)*H
AL
(/ ABC*+C DA ^/ 41
ABC*+C DA ^//
AL 42
• Go to ppt titled “Polish Notation
examples.ppt”
AL 43
Evaluation of Postfix Expression
• Algorithm: This algorithm finds the
VALUE of an arithmetic expression P
written in postfix notation.
(b) Evaluate B ʘ A
45
Evaluation of Postfix Expression
(c) Place the result (b) back on
STACK
[End If Structure]
[End of Step 2 Loop]
6. Exit
46
Infix: convert and evaluation into prefix
4 * (2-(6*3+4)*2)+1
i. Convert
ii. evaluation
AL 47
Transforming Infix Expression into
Postfix Expression
Algorithm: Polish(P, Q)
Suppose Q is an arithmetic expression written in infix
notation. This algorithm finds the equivalent postfix
expression P
1. Push “(“ onto STACK, and add “)” to the end of Q
2. Scan Q from left to right and repeat Steps 3 to 6 for
each element of Q until the input elements are
completely scanned.
• 48
Transforming Infix Expression into
Postfix Expression contd…
3. If an operand is encountered, add it
to P
4. If a left parenthesis is encountered,
push it onto STACK
• 49
Transforming Infix Expression into
Postfix Expression contd…
5. If an operator ʘ is encountered,
then:
50
Transforming Infix Expression into
Postfix Expression contd…
7. Exit
52
STACK
Recursion
AL 54