SlideShare a Scribd company logo
Application of Stacks
By
S. Christalin Nelson
Formula Translation
 The Problem: Writing formula or arithmetic
expressions in something close to their usual
mathematical/scientific form
 One of the most important accomplishments of early
design computer languages in designing a compiler that
understood expressions & to produce a machine-
language output.
 In fact, the name FORTRAN stands for FORMULA
TRANSLATOR.
2 of 27
3/17/2022
The Quadratic formula
x = (-b + (b*b – (4*a)*c)^0.5) / (2*a)
Question
 Which operation must be done before others?
 What are the effects of the parentheses? When
can they omitted?
 How many times you will look back and forth
through the expression until you evaluate it?
a
ac
b
b
x
2
4
2




3 of 27
3/17/2022
Compiler Conventions
 Parenthesis has highest priority
 Few important Priorities
1. unary
2. ^
3. * /
4. + –
4 of 27
3/17/2022
Operator priority & associativity
Binary operators
1. * / %
2. + –
3. < <= > >=
4. == !=
5. &&
6. ||
7. =
Unary operators
 -5
 +5
 + –5 = –5
 – –5 = 5
5 of 27
3/17/2022
Polish Notation
 Discovered by the polish mathematician
Jan Lukasiewicz.
 Expressions can be classified w.r.t. the
position of its Operators among operands
 Before operand(s)  prefix expression
 After operand(s)  postfix expression
 In-between operands  infix expression
6 of 27
3/17/2022
Polish & Reverse Polish Notations
Infix: a  b
prefix   a b
postfix  a b 
Infix: a + b  c
prefix  + a  b c
postfix  a b c  +
Prefix and Postfix are not
mirror to each other
Prefix (Polish)
Postfix (Reverse Polish)
7 of 27
3/17/2022
Polish Notation - Importance
 Infix Expressions are harder for Computers to
evaluate because of the additional work needed
to decide on precedence. Hence conversion to
Polish or Reverse Polish Notation is required.
 It is not necessary to make repeated scans
through the expression
 Use of parentheses (if required)
 Evaluation can be achieved with great efficiency
8 of 27
3/17/2022
Activity -1 (Self-Test)
 Change the following expression to
a) Reverse Polish notation
b) Polish notations
3 + (4 + 6  2)  ((8 – 3)  (2 - 5) + 4) – 2  6
a) Reverse Polish Notation:
3 4 6 2  + 8 3 – 2 5 –  4 +  + 2 6  –
b) Polish Notation:
– + 3  + 4  6 2 +  – 8 3 – 2 5 4  2 6
9 of 27
3/17/2022
 Evaluate the following expressions
5 4  Error
6   Error
+  Error
5 –   Unary operator
Reverse Polish Notation
Note:
• Use different symbol to represent unary operator i.e. (~)
• Example: Convert to Reverse Polish
(-5) – (-4)  (~5) – (~4)  5 ~ 4 ~ –
10 of 27
3/17/2022
Converting Infix to Postfix with Stack
 Read expression from Left-to-Right and
 if an operand is read copy it to the output,
 if a left parenthesis is read push it into the stack,
 when a right parenthesis is encountered, the operator at the top of
the stack is popped off the stack and copied to the output until the
symbol at the top of the stack is a left parenthesis. When that occurs,
both parentheses are discarded,
 if an operator is scanned and has a higher precedence than the
operator at the top of the stack, the operator being scanned is pushed
onto the stack,
 while the precedence of the operator being scanned is lower than or
equal to the precedence of the operator at the top of the stack, the
operator at the top of the stack is popped and copied to the output,
 when the end of the expression is reached on the input scan, the
remaining operators in the stack are popped and copied to the output.
11 of 27
3/17/2022
Example
*
(
–
(
*
Input: 4 * (2 – (6 * 3 + 4) * 2) + 1
Output:
*
(
–
(
+
*
(
–
*
*
4 2 6 3 * 4 + 2 * – *
+
1 +
12 of 27
3/17/2022
Converting Infix to Prefix with Stack
1st method
 Read expression from Right-to-Left and
 if an operand is read copy it to the RIGHT of the output,
 if a right parenthesis is read push it into the stack,
 when a left parenthesis is encountered, the operator at the top of the
stack is popped off the stack and copied to the output until the
symbol at the top of the stack is a right parenthesis. When that
occurs, both parentheses are discarded,
 if an operator is scanned and has a higher or equal precedence than
the operator at the top of the stack, the operator being scanned is
pushed onto the stack,
 while the precedence of the operator being scanned is lower than to
the precedence of the operator at the top of the stack, the operator at
the top of the stack is popped and copied to the output,
 when the end of the expression is reached on the input scan, the
remaining operators in the stack are popped and copied to the LEFT
of the output.
13 of 27
3/17/2022
Example
+
)
*
)
+
Input: 4 * (2 – (6 * 3 + 4) * 2) + 1
Output:
–
*
+ * 4 – 2 * + * 6 4 2 1
3
+
)
*
+
)
+
*
14 of 27
3/17/2022
Converting Infix to Prefix with Stack
2nd method
 Reverse the expression & Read expression from Left-to-Right
 if an operand is read copy it to the output (left-to-right)
 if a right parenthesis is read push it into the stack
 when a left parenthesis is encountered, the operator at the top of the
stack is popped off the stack and copied to the output until the
symbol at the top of the stack is a right parenthesis. When that
occurs, both parentheses are discarded.
 if an operator is scanned and has a higher or equal precedence than
the operator at the top of the stack, the operator being scanned is
pushed onto the stack
 while the precedence of the operator being scanned is lower than to
the precedence of the operator at the top of the stack, the operator at
the top of the stack is popped and copied to the output
 when the end of the expression is reached on the input scan, the
remaining operators in the stack are popped and copied to the output.
 Reverse the output
15 of 27
3/17/2022
Example
+
)
*
)
+
Input: 4 * (2 – (6 * 3 + 4) * 2) + 1
Output:
–
*
+
*
4
–
2
*
+
*
6
4
2
1 3
+
)
*
+
)
+
*
1 + ) 2 * ) 4 + 3 * 6 ( – 2 ( * 4
Reverse:
+ * 4 – 2 * + * 6 3 4 2 1
Reverse Output:
16 of 27
3/17/2022
Activity-2
 Using stack diagrams convert the following
expressions into postfix and prefix forms of polish
notation:
a) 8 – 3  4 + 2
b) 8 – 3  (4 + 2)
c) (8 – 3)  (4 + 2)
d) (8 – 3)  4 + 2
e) (-a + b)  (c + a) – 5
f) 2 + ((-3 + 1)  (4 – 2) + 3)  6 – (1 + 2  3)
g) (5 > 4) and not (3 = 2 – 1)
17 of 27
3/17/2022
Activity-2 (Solution)
Infix Rev. Polish Polish
8 – 3  4 + 2 8 3 4 x - 2 + - 8 + x 3 4 2
8 – 3  (4 + 2) 8 3 4 2 + x - - 8 x 3 + 4 2
(8 – 3)  (4 + 2) 8 3 - 4 2 + x x - 8 3 + 4 2
(8 – 3)  4 + 2 8 3 - 4 x 2 + + x - 8 3 4 2
(-a + b)  (c + a) – 5 a ~ b + c a + x 5 - ?
2 + ((-3 + 1)  (4 – 2) + 3)  6 – (1 + 2  3) ? ?
(5 > 4) && !(3 = 2 – 1) ? ?
18 of 27
3/17/2022
Evaluation of Reverse Polish
Expressions
 Most compilers use the polish form to translate
expressions into machine language.
 Evaluation is done using a stack data-structure
 Read expression from left to right and build the stack of
numbers (operands).
 When an operator is read two operands are popped out
of the stack they are evaluated with the operator and the
result is pushed into the stack.
 At the end of the expression there must be only one
operand into the stack (the solution) otherwise ERROR.
19 of 27
3/17/2022
5
3
3 4 6 2  + 8 3 – 2 5 –  4 +  + 2 6  –
3
4
6
2
6  2
3
4
12
4 + 12
3
16
8
8 – 3
2
3
16
5
2 – 5
-3
3
16
5
5  (-3)
4
3
16
-15
-15+4
3
16
-11
16(-11)
3
-176
3+(-176)
-173
2
6
26
-173
12
-173 – 12
-185
Result =
20 of 27
3/17/2022
Evaluation of Polish Expressions
 Evaluation is done using a stack data-structure
 Read expression from right to left and build the stack of
numbers (operands).
 When an operator is read two operands are popped
out of the stack they are evaluated with the operator
and the result is pushed into the stack.
 At the end of the expression there must be only one
operand into the stack (the solution) otherwise ERROR.
21 of 27
3/17/2022
3
–  3 – 8  3 2 – ~ 4 – 6 2
2
6
6 – 2
4
4
-4
4
-4
-4 – 4
2
-8
3  2
6
-8
8
8 – 6
3
-8
2
3  2
-8
6
6 – (-8)
14
Result =
22 of 27
3/17/2022
Running-sum condition
 For a sequence E of operands, unary operators and
binary operators, form a running-sum by starting at
the left-end of E and counting
 +1 for each operand,
 0 for unary operator, and
 –1 for each binary operator.
 E satisfies the running-sum condition provided that
it never falls below 1, and is exactly 1 at the right-
hand-end of E.
3 4 6 2  + 8 3 – 2 5 –  4 +  + 2 6  –
23 of 27
3/17/2022
Exercises
1. Which of the following are syntactically correct
postfix expressions (use the running-sum
condition)? Show the error in each incorrect
expression. Translate each correct expression into
infix form.
a) a b c +  a / c b + d / –
b) a b + c a  b c / d –
c) a b + c a  – c  + b c –
d) a ~ b 
e) a  b ~
f) a b  –
g) a b ~ 
24 of 27
3/17/2022
2. Convert the following postfix form expressions to
infix form:
a) a b + a b – 
b) a b + c 
c) a b c + 
d) a b c + a c d –   b + –
3. Evaluate the following postfix form expressions
and then convert them to infix form:
a) 4 5 2 + 3  –
b) 2 4 6 – 8 3 –  2 + –
25 of 27
3/17/2022
4. Evaluate the following prefix form expressions and
then convert them to infix form:
a) – + 4 – 5 ~ 2 +  3 4 6
b) – + 2  – 3 8 – 6 4 2
5. Convert the quadratic formula shown below to
postfix form.
6. Translate each of the following expressions from
postfix form to prefix form:
a) a b + c 
b) a b c + 
c) a ! b ! / c d – a ! – 
d) a b < not c d  e < or
a
ac
b
b
x
2
4
2




26 of 27
3/17/2022
7. Evaluate the following prefix expressions. Hence,
translate them into postfix form:
a) / + 2 4 ! 4
b) / + ! 3 9 3
c) and < 3 4 or not = + 2 5 7 > 3 0
8. Translate each of the following expressions from
infix form into postfix and then by using stack
diagrams evaluate the postfix expressions:
a) 5 * ((-3 – 2) * (4 – 6) + 3 * 2)
b) -3 + (5 + 2) * 8 + 6 – ((4 – 2 * 3) * (-2 – 3) – 9)
c) 9 + 5 * ((3 + 2) – 8 * (1 – 3) * (-3 – 6)) + 2 * 3
d) 1 – (3 – (-1 + 2 * (6 + 7 * 2)))
27 of 27
3/17/2022

More Related Content

What's hot

1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
Christalin Nelson
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
Vivek chan
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Mergesort
MergesortMergesort
Mergesort
luzenith_g
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Cd lab manual
Cd lab manualCd lab manual
Cd lab manual
Haftu Hagos
 
Backtracking & branch and bound
Backtracking & branch and boundBacktracking & branch and bound
Backtracking & branch and bound
Vipul Chauhan
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
jyoti_lakhani
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 

What's hot (20)

1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Mergesort
MergesortMergesort
Mergesort
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Stacks
StacksStacks
Stacks
 
Cd lab manual
Cd lab manualCd lab manual
Cd lab manual
 
Backtracking & branch and bound
Backtracking & branch and boundBacktracking & branch and bound
Backtracking & branch and bound
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 

Similar to Applications of Stack

Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
JAYAPRIYAR7
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7ecomputernotes
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
ecomputernotes
 
data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3
jateno3396
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
Vetukurivenkatashiva
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
NARESH GUMMAGUTTA
 
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in ds
Rohini Mahajan
 
2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf
aniarihant
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
Krish_ver2
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
arihantsherwani
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
C compiler(final)
C compiler(final)C compiler(final)
C compiler(final)
Farzan Dehbashi
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
soniasharmafdp
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
LavanyaJ28
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
mailmerk
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionsecomputernotes
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 

Similar to Applications of Stack (20)

Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
 
data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
 
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in ds
 
2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
C compiler(final)
C compiler(final)C compiler(final)
C compiler(final)
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 

More from Christalin Nelson

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
Christalin Nelson
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
Christalin Nelson
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
Christalin Nelson
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
Christalin Nelson
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
Christalin Nelson
 
Relational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfRelational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdf
Christalin Nelson
 
Data Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdfData Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdf
Christalin Nelson
 
Data Modeling - Entity Relationship Diagrams-1.pdf
Data Modeling - Entity Relationship Diagrams-1.pdfData Modeling - Entity Relationship Diagrams-1.pdf
Data Modeling - Entity Relationship Diagrams-1.pdf
Christalin Nelson
 
Overview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdfOverview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdf
Christalin Nelson
 
Overview of Databases and Data Modelling-1.pdf
Overview of Databases and Data Modelling-1.pdfOverview of Databases and Data Modelling-1.pdf
Overview of Databases and Data Modelling-1.pdf
Christalin Nelson
 
Packages and Subpackages in Java
Packages and Subpackages in JavaPackages and Subpackages in Java
Packages and Subpackages in Java
Christalin Nelson
 
Bitwise complement operator
Bitwise complement operatorBitwise complement operator
Bitwise complement operator
Christalin Nelson
 
Advanced Data Structures - Vol.2
Advanced Data Structures - Vol.2Advanced Data Structures - Vol.2
Advanced Data Structures - Vol.2
Christalin Nelson
 
Deadlocks
DeadlocksDeadlocks
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
Christalin Nelson
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
Christalin Nelson
 
Process Management
Process ManagementProcess Management
Process Management
Christalin Nelson
 
Storage system architecture
Storage system architectureStorage system architecture
Storage system architecture
Christalin Nelson
 
Data Storage and Information Management
Data Storage and Information ManagementData Storage and Information Management
Data Storage and Information Management
Christalin Nelson
 
Application Middleware Overview
Application Middleware OverviewApplication Middleware Overview
Application Middleware Overview
Christalin Nelson
 

More from Christalin Nelson (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Relational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfRelational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdf
 
Data Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdfData Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdf
 
Data Modeling - Entity Relationship Diagrams-1.pdf
Data Modeling - Entity Relationship Diagrams-1.pdfData Modeling - Entity Relationship Diagrams-1.pdf
Data Modeling - Entity Relationship Diagrams-1.pdf
 
Overview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdfOverview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdf
 
Overview of Databases and Data Modelling-1.pdf
Overview of Databases and Data Modelling-1.pdfOverview of Databases and Data Modelling-1.pdf
Overview of Databases and Data Modelling-1.pdf
 
Packages and Subpackages in Java
Packages and Subpackages in JavaPackages and Subpackages in Java
Packages and Subpackages in Java
 
Bitwise complement operator
Bitwise complement operatorBitwise complement operator
Bitwise complement operator
 
Advanced Data Structures - Vol.2
Advanced Data Structures - Vol.2Advanced Data Structures - Vol.2
Advanced Data Structures - Vol.2
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Process Management
Process ManagementProcess Management
Process Management
 
Storage system architecture
Storage system architectureStorage system architecture
Storage system architecture
 
Data Storage and Information Management
Data Storage and Information ManagementData Storage and Information Management
Data Storage and Information Management
 
Application Middleware Overview
Application Middleware OverviewApplication Middleware Overview
Application Middleware Overview
 

Recently uploaded

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 

Applications of Stack

  • 1. Application of Stacks By S. Christalin Nelson
  • 2. Formula Translation  The Problem: Writing formula or arithmetic expressions in something close to their usual mathematical/scientific form  One of the most important accomplishments of early design computer languages in designing a compiler that understood expressions & to produce a machine- language output.  In fact, the name FORTRAN stands for FORMULA TRANSLATOR. 2 of 27 3/17/2022
  • 3. The Quadratic formula x = (-b + (b*b – (4*a)*c)^0.5) / (2*a) Question  Which operation must be done before others?  What are the effects of the parentheses? When can they omitted?  How many times you will look back and forth through the expression until you evaluate it? a ac b b x 2 4 2     3 of 27 3/17/2022
  • 4. Compiler Conventions  Parenthesis has highest priority  Few important Priorities 1. unary 2. ^ 3. * / 4. + – 4 of 27 3/17/2022
  • 5. Operator priority & associativity Binary operators 1. * / % 2. + – 3. < <= > >= 4. == != 5. && 6. || 7. = Unary operators  -5  +5  + –5 = –5  – –5 = 5 5 of 27 3/17/2022
  • 6. Polish Notation  Discovered by the polish mathematician Jan Lukasiewicz.  Expressions can be classified w.r.t. the position of its Operators among operands  Before operand(s)  prefix expression  After operand(s)  postfix expression  In-between operands  infix expression 6 of 27 3/17/2022
  • 7. Polish & Reverse Polish Notations Infix: a  b prefix   a b postfix  a b  Infix: a + b  c prefix  + a  b c postfix  a b c  + Prefix and Postfix are not mirror to each other Prefix (Polish) Postfix (Reverse Polish) 7 of 27 3/17/2022
  • 8. Polish Notation - Importance  Infix Expressions are harder for Computers to evaluate because of the additional work needed to decide on precedence. Hence conversion to Polish or Reverse Polish Notation is required.  It is not necessary to make repeated scans through the expression  Use of parentheses (if required)  Evaluation can be achieved with great efficiency 8 of 27 3/17/2022
  • 9. Activity -1 (Self-Test)  Change the following expression to a) Reverse Polish notation b) Polish notations 3 + (4 + 6  2)  ((8 – 3)  (2 - 5) + 4) – 2  6 a) Reverse Polish Notation: 3 4 6 2  + 8 3 – 2 5 –  4 +  + 2 6  – b) Polish Notation: – + 3  + 4  6 2 +  – 8 3 – 2 5 4  2 6 9 of 27 3/17/2022
  • 10.  Evaluate the following expressions 5 4  Error 6   Error +  Error 5 –   Unary operator Reverse Polish Notation Note: • Use different symbol to represent unary operator i.e. (~) • Example: Convert to Reverse Polish (-5) – (-4)  (~5) – (~4)  5 ~ 4 ~ – 10 of 27 3/17/2022
  • 11. Converting Infix to Postfix with Stack  Read expression from Left-to-Right and  if an operand is read copy it to the output,  if a left parenthesis is read push it into the stack,  when a right parenthesis is encountered, the operator at the top of the stack is popped off the stack and copied to the output until the symbol at the top of the stack is a left parenthesis. When that occurs, both parentheses are discarded,  if an operator is scanned and has a higher precedence than the operator at the top of the stack, the operator being scanned is pushed onto the stack,  while the precedence of the operator being scanned is lower than or equal to the precedence of the operator at the top of the stack, the operator at the top of the stack is popped and copied to the output,  when the end of the expression is reached on the input scan, the remaining operators in the stack are popped and copied to the output. 11 of 27 3/17/2022
  • 12. Example * ( – ( * Input: 4 * (2 – (6 * 3 + 4) * 2) + 1 Output: * ( – ( + * ( – * * 4 2 6 3 * 4 + 2 * – * + 1 + 12 of 27 3/17/2022
  • 13. Converting Infix to Prefix with Stack 1st method  Read expression from Right-to-Left and  if an operand is read copy it to the RIGHT of the output,  if a right parenthesis is read push it into the stack,  when a left parenthesis is encountered, the operator at the top of the stack is popped off the stack and copied to the output until the symbol at the top of the stack is a right parenthesis. When that occurs, both parentheses are discarded,  if an operator is scanned and has a higher or equal precedence than the operator at the top of the stack, the operator being scanned is pushed onto the stack,  while the precedence of the operator being scanned is lower than to the precedence of the operator at the top of the stack, the operator at the top of the stack is popped and copied to the output,  when the end of the expression is reached on the input scan, the remaining operators in the stack are popped and copied to the LEFT of the output. 13 of 27 3/17/2022
  • 14. Example + ) * ) + Input: 4 * (2 – (6 * 3 + 4) * 2) + 1 Output: – * + * 4 – 2 * + * 6 4 2 1 3 + ) * + ) + * 14 of 27 3/17/2022
  • 15. Converting Infix to Prefix with Stack 2nd method  Reverse the expression & Read expression from Left-to-Right  if an operand is read copy it to the output (left-to-right)  if a right parenthesis is read push it into the stack  when a left parenthesis is encountered, the operator at the top of the stack is popped off the stack and copied to the output until the symbol at the top of the stack is a right parenthesis. When that occurs, both parentheses are discarded.  if an operator is scanned and has a higher or equal precedence than the operator at the top of the stack, the operator being scanned is pushed onto the stack  while the precedence of the operator being scanned is lower than to the precedence of the operator at the top of the stack, the operator at the top of the stack is popped and copied to the output  when the end of the expression is reached on the input scan, the remaining operators in the stack are popped and copied to the output.  Reverse the output 15 of 27 3/17/2022
  • 16. Example + ) * ) + Input: 4 * (2 – (6 * 3 + 4) * 2) + 1 Output: – * + * 4 – 2 * + * 6 4 2 1 3 + ) * + ) + * 1 + ) 2 * ) 4 + 3 * 6 ( – 2 ( * 4 Reverse: + * 4 – 2 * + * 6 3 4 2 1 Reverse Output: 16 of 27 3/17/2022
  • 17. Activity-2  Using stack diagrams convert the following expressions into postfix and prefix forms of polish notation: a) 8 – 3  4 + 2 b) 8 – 3  (4 + 2) c) (8 – 3)  (4 + 2) d) (8 – 3)  4 + 2 e) (-a + b)  (c + a) – 5 f) 2 + ((-3 + 1)  (4 – 2) + 3)  6 – (1 + 2  3) g) (5 > 4) and not (3 = 2 – 1) 17 of 27 3/17/2022
  • 18. Activity-2 (Solution) Infix Rev. Polish Polish 8 – 3  4 + 2 8 3 4 x - 2 + - 8 + x 3 4 2 8 – 3  (4 + 2) 8 3 4 2 + x - - 8 x 3 + 4 2 (8 – 3)  (4 + 2) 8 3 - 4 2 + x x - 8 3 + 4 2 (8 – 3)  4 + 2 8 3 - 4 x 2 + + x - 8 3 4 2 (-a + b)  (c + a) – 5 a ~ b + c a + x 5 - ? 2 + ((-3 + 1)  (4 – 2) + 3)  6 – (1 + 2  3) ? ? (5 > 4) && !(3 = 2 – 1) ? ? 18 of 27 3/17/2022
  • 19. Evaluation of Reverse Polish Expressions  Most compilers use the polish form to translate expressions into machine language.  Evaluation is done using a stack data-structure  Read expression from left to right and build the stack of numbers (operands).  When an operator is read two operands are popped out of the stack they are evaluated with the operator and the result is pushed into the stack.  At the end of the expression there must be only one operand into the stack (the solution) otherwise ERROR. 19 of 27 3/17/2022
  • 20. 5 3 3 4 6 2  + 8 3 – 2 5 –  4 +  + 2 6  – 3 4 6 2 6  2 3 4 12 4 + 12 3 16 8 8 – 3 2 3 16 5 2 – 5 -3 3 16 5 5  (-3) 4 3 16 -15 -15+4 3 16 -11 16(-11) 3 -176 3+(-176) -173 2 6 26 -173 12 -173 – 12 -185 Result = 20 of 27 3/17/2022
  • 21. Evaluation of Polish Expressions  Evaluation is done using a stack data-structure  Read expression from right to left and build the stack of numbers (operands).  When an operator is read two operands are popped out of the stack they are evaluated with the operator and the result is pushed into the stack.  At the end of the expression there must be only one operand into the stack (the solution) otherwise ERROR. 21 of 27 3/17/2022
  • 22. 3 –  3 – 8  3 2 – ~ 4 – 6 2 2 6 6 – 2 4 4 -4 4 -4 -4 – 4 2 -8 3  2 6 -8 8 8 – 6 3 -8 2 3  2 -8 6 6 – (-8) 14 Result = 22 of 27 3/17/2022
  • 23. Running-sum condition  For a sequence E of operands, unary operators and binary operators, form a running-sum by starting at the left-end of E and counting  +1 for each operand,  0 for unary operator, and  –1 for each binary operator.  E satisfies the running-sum condition provided that it never falls below 1, and is exactly 1 at the right- hand-end of E. 3 4 6 2  + 8 3 – 2 5 –  4 +  + 2 6  – 23 of 27 3/17/2022
  • 24. Exercises 1. Which of the following are syntactically correct postfix expressions (use the running-sum condition)? Show the error in each incorrect expression. Translate each correct expression into infix form. a) a b c +  a / c b + d / – b) a b + c a  b c / d – c) a b + c a  – c  + b c – d) a ~ b  e) a  b ~ f) a b  – g) a b ~  24 of 27 3/17/2022
  • 25. 2. Convert the following postfix form expressions to infix form: a) a b + a b –  b) a b + c  c) a b c +  d) a b c + a c d –   b + – 3. Evaluate the following postfix form expressions and then convert them to infix form: a) 4 5 2 + 3  – b) 2 4 6 – 8 3 –  2 + – 25 of 27 3/17/2022
  • 26. 4. Evaluate the following prefix form expressions and then convert them to infix form: a) – + 4 – 5 ~ 2 +  3 4 6 b) – + 2  – 3 8 – 6 4 2 5. Convert the quadratic formula shown below to postfix form. 6. Translate each of the following expressions from postfix form to prefix form: a) a b + c  b) a b c +  c) a ! b ! / c d – a ! –  d) a b < not c d  e < or a ac b b x 2 4 2     26 of 27 3/17/2022
  • 27. 7. Evaluate the following prefix expressions. Hence, translate them into postfix form: a) / + 2 4 ! 4 b) / + ! 3 9 3 c) and < 3 4 or not = + 2 5 7 > 3 0 8. Translate each of the following expressions from infix form into postfix and then by using stack diagrams evaluate the postfix expressions: a) 5 * ((-3 – 2) * (4 – 6) + 3 * 2) b) -3 + (5 + 2) * 8 + 6 – ((4 – 2 * 3) * (-2 – 3) – 9) c) 9 + 5 * ((3 + 2) – 8 * (1 – 3) * (-3 – 6)) + 2 * 3 d) 1 – (3 – (-1 + 2 * (6 + 7 * 2))) 27 of 27 3/17/2022