0% found this document useful (0 votes)
6 views

DS-Lecture 9 (Stack)

The document provides an overview of stacks, including their definition, operations (push and pop), and representation using arrays. It also discusses the conversion of infix expressions to postfix and prefix notations, along with the evaluation of postfix expressions using stacks. Additionally, it highlights the importance of Polish notation and its applications in various computational problems.

Uploaded by

mka.datacenter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DS-Lecture 9 (Stack)

The document provides an overview of stacks, including their definition, operations (push and pop), and representation using arrays. It also discusses the conversion of infix expressions to postfix and prefix notations, along with the evaluation of postfix expressions using stacks. Additionally, it highlights the importance of Polish notation and its applications in various computational problems.

Uploaded by

mka.datacenter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Data Structures

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:

(a) “Push” is the term used to insert an


element into a stack

(b) “Pop” is the term used to delete an


element from a stack

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

• One-way list or a liner array is one way


to represent Stack

• Stack will be maintained by a linear


array STACK

• A pointer variable TOP, which contains


the location of the top element of the
stack
6
Array Representation of Stack
contd…

• A variable MAXSTK which gives the


maximum number of elements that can
be held by the stack

• The condition TOP = 0 or TOP = NULL


will indicate that the stack is empty.

AL 7
Example

AL 8
Push and Pop Algorithm
• The operation of adding (pushing) an
item onto a stack

• The operation of removing (popping) an


item from a stack.

• The following procedures, called PUSH


and POP

AL 9
PUSH Algorithm
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a
stack

1. [Stack already filled?]


If TOP = MAXSTK, then: Print
OVERFLOW and Return

AL 10
PUSH Algorithm contd…
2. Set TOP := TOP + 1
[Increases TOP by 1]

3. Set STACK[TOP] := ITEM


[Insert ITEM IN NEW TOP
position]

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

1. [Stack has an item to be removed?]


If TOP = 0, then: Print:
UNDERFLOW, and return

AL 12
POP Algorithm
2. Set ITEM := STACK[TOP]
[Assigns TOP element to ITEM]

3. Set TOP := TOP – 1


[Decreases TOP by 1]

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)

• There are several way to write the


expression are as follows:
– Infix A+B
– Prefix +AB
– Postfix AB+

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)

• Multiplication has precedence over


addition.
Polish Notation contd…
• Conversion to postfix

A+(B*C) infix form


Polish Notation contd…
• Conversion to postfix

A+(B*C) infix form


A+(BC*) convert multiplication
Polish Notation contd…
• Conversion to postfix

A+(B*C) infix form


A+(BC*) convert multiplication
A(BC*)+ convert addition
Polish Notation contd…
• Conversion to postfix

A+(B*C) infix form


A+(BC*) convert multiplication
A(BC*)+ convert addition
ABC*+ postfix form
Polish Notation contd…
• Conversion to postfix

(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

• The order of precedence is (highest to


lowest)

• 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.

• For exponentiation, the right-to-left 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

• It is not necessary to make repeated


scans through the expression

• No parentheses

• Evaluation can be achieved with great


efficiency

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

Symbol Scanned STACK Expression P


(1) A ( A
(2) + (+ A
(3) ( (+( A
(4) B (+( AB
(5) * (+(* AB
(6) C (+(* ABC
(7) - (+(- ABC*
(8) ( (+(-( ABC*
(9) D (+(-( ABC*D
(10) / (+(-(/ ABC*D
(11) E (+(-(/ ABC*DE
(12) ^ (+(-(/^ ABC*DE
(13) F (+(-(/^ ABC*DEF
(14) ) (+(- ABC*DEF^/
Q: 5 * ( 6 + 2 ) – 12 / 4 )

Symbol Scanned STACK Expression P


(1) 5 ( 5
(2) * (* 5
(3) ( (*( 5
(4) 6 (*( 56
(5) + (*(+ 56
(6) 2 (*(+ 562
(7) ) (* 562+
(8) - (- 562+*
(9) 12 (- 5 6 2 + * 12
(10) / (-/ 5 6 2 + * 12
(11) 4 (-/ 5 6 2 + * 12 4
(12) ) 5 6 2 + * 12 4 / -
Algorithm for evaluation of Postfix expression using
Stack
Symbol scanned STACK
(1) 5 5
(2) 6 5 6
(3) 2 5 6 2
(4) + 5 8
(5) * 40
(6) 12 40 12
(7) 4 40 12 4
(8) / 40 3
(9) - 37
(10) )
Stack Output
B
B (
(( A
((+ A
((+ AB
((+* AB
((+* ABC
((+* ABC
( ABC*+
(/ ABC*+
(/( ABC*+
(/( ABC*+C
(/(/ ABC*+C
(/(/ ABC*+C D
(/(/^ ABC*+C D
(/(/^ ABC*+C DA

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.

1. Add a right parenthesis “)” at the end of P


[This act as a sentinel]
2. Scan P from left to right and repeat Steps
3 and 4 for each element of P until the
input P elements have been completed
44
Evaluation of Postfix Expression
3. If an operand is encountered, put it on
STACK
4. If an operator ʘ is encountered, then:

(a) Remove the two top elements of


STACK, where A is the top element
and B is the next-to-top element

(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]

5. Set VALUE equal to the top element on


STACK

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:

(a) Repeatedly pop from STACK and


add to P each operator (on the top
STACK) which has the same
precedence as or higher
precedence than ʘ

50
Transforming Infix Expression into
Postfix Expression contd…

(b) Add ʘ to STACK


[End of If structure]
6. If a right parenthesis is encountered,
then:
(a) Repeatedly pop from STACK and
add to P each operator (on the top
of STACK) until left parenthesis is
encountered
51
Transforming Infix Expression into
Postfix Expression contd…
(b) Remove left parenthesis.[Do not
add the left parenthesis to P]
[End If Structure]
[End of Step 2 loop]

7. Exit

52
STACK
Recursion
AL 54

You might also like