DSA # 3 Stack & Queues
DSA # 3 Stack & Queues
Muzammil Khan
Department of
Computer & Software Technology
University of Swat
1
Chapter 3
Stack & Queue
STACKSIZE = 10
ITEM = 55
Algorithm’s Description
Suppose IE is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression PE
19
Infix to Postfix Conversation (Cont...)
Example
Let infix expression
IE is A + ( B * C – ( D / E ^ F ) * G ) * H
PE ?
Step 1.
Add “)” at the end of expression IE i.e.
A + ( B * C – ( D / E ^ F ) * G ) * H ) and
Also Push a “(“ on Stack
Scan rest of the IE from left to right
The table show
Scan symbol
Stack status
PE (required expression)
Data Structure & Algorithms
20
21
Example (Cont...)
Algorithm’s Description
If PE is an arithmetic expression written in postfix notation. This
algorithm uses STACK to hold operands, and evaluate PE
Algorithm’s Statements
Step 1. Add a Dollar Sign “$” at the end of PE
Step 2. Scan PE from left to right and repeat Steps 3 and 4 for each
element of PE until the sentinel “$” is encountered
Step 3. If an operand is encountered, put it on STACK
Step 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.
c) Place the result of (b) back on STACK
Step 5. Set VALUE equal to the top element on STACK
Step 5. Exit
22
Computation of Postfix Expression
Example
An infix arithmetic expression
(5 + 2) * 3 – 9 / 3
Its postfix expression is
52+3*93/–
Now add “$” at the end of expression as a sentinel
52+3*84/–$
Let MAXSIZE = 6
Priority Queues
Representation of priority queues
List representation
Array representation
Pages 193-196
Exercise
Pages 196...