SlideShare a Scribd company logo
CSA 2100 - Data Structures
and Algorithms
Contents
1. Introduction to Data Structures
2. Types and Concepts of Arrays
3. Stack
3.1 Concepts and Representation
3.2 Stack operations
3.3 Stack implementation using array
3.4 Applications of Stack.
2
3
Stack
Definitions
1. A stack is a collection of objects that are inserted and removed
according to the last-in, first-out (LIFO) principle. [T1]
2. A stack is an ordered list in which insertion and deletion are done
at one end, called top. The last element inserted is the first one to be
deleted. Hence, it is called the Last in First out(LIFO) or First in Last
out (FILO) list. [T2]
4
Applications of Stack.
• Balancing of symbols
• Infix-to-postfix Conversion- Lab
• Evaluation of postfix expression - Lab
• Implementing function calls (including recursion)
• Page-visited history in a Web browser [Back Buttons]
• Undo sequence in a text editor
• Matching Tags in HTML and XML
Algebraic Expressions
• Infix expressions
• An operator appears between its operands
• Example: a + b
• Prefix expressions
• An operator appears before its operands
• Example: + a b
• Postfix expressions
• An operator appears after its operands
• Example: a b +
A complicated example
• Infix expressions:
a + b * c + ( d * e + f ) * g
• Postfix expressions:
a b c * + d e * f + g * +
• Prefix expressions:
+ + a * b c * + * d e f g
7
Table : the operator precedence rules for common
mathematical operators
8
Infix-to-postfix Conversion
Infix Expression
Definition: An infix expression is a mathematical notation where the
operators are placed between the operands.
This is the most common and familiar notation, especially in
arithmetic expressions.
Example:
A + B
(A + B) * C
9
Infix-to-postfix Conversion
Postfix Expression (Reverse Polish Notation)
Definition: A postfix expression is a mathematical notation where the
operators are placed after their operands.
There are no parentheses needed, and the order of operations is
explicitly defined by the operators’ positions.
Example:
A B +
A B C * +
10
Infix-to-postfix Conversion
Prefix Expression (Polish Notation)
Definition: A prefix expression is a mathematical notation where the
operators are placed before their operands.
Like postfix expressions, there are no parentheses needed, and the
position of the operators dictates the order of operations.
Example:
+ A B
* + A B C
11
Algorithm: Infix to Postfix Conversion
1. Initialize an empty stack for operators and an empty list for the output (postfix expression).
2. Iterate over each character in the infix expression:
•Operand (e.g., numbers or variables): Directly append it to the postfix list.
•Left parenthesis ( Push it onto the stack. Right parenthesis ): Pop from the stack to the postfix
list until a left parenthesis ( is encountered on the stack. Discard the left parenthesis.
•Operator (e.g., +, -, *, /):
 While the stack is not empty and the operator at the top of the stack has greater or equal
precedence than the current operator, pop the operator from the stack to the postfix list.
 Push the current operator onto the stack.
• After iterating over the expression, pop all remaining operators from the stack to the postfix
list.
• The postfix list now contains the postfix expression.
12
Infix to Postfix Conversion:
13
14
15
16
17
18
Example 1: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
A + B * C + D A Operand Print Empty A
A + B * C + D + Operator Push(+) + A
A + B * C + D B Operand Print + AB
A + B * C + D * Operator P(*)>P(stack[TOP]) so Push(*) +* AB
A + B * C + D C Operand Print +* ABC
A + B * C + D + Operator P(+)<=P(stack[TOP]) so Pop() and Print
P(+)<=P(stack[TOP]) so Pop() and Print
+
+
ABC*
ABC*+
A + B * C + D D Operand Print + ABC*+D
Completed 0 - POP() all the symbols from stack and
print
Empty ABC*+D+
19
Example 2: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
((A + B) – C * (D / E)) + F ( LP Push( ‘(‘ ) (
((A + B) – C * (D / E)) + F ( LP Push( ‘(‘ ) ((
((A + B) – C * (D / E)) + F A Operand Print A (( A
((A + B) – C * (D / E)) + F + Operator Push (+) ((+ A
((A + B) – C * (D / E)) + F B Operand Print B ((+ AB
((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘
and discard ‘(‘
Pop(+)
( AB+
((A + B) – C * (D / E)) + F - Operator P(-)>P(stack[TOP]) so push(-) (- AB+
((A + B) – C * (D / E)) + F C Operand Print C (- AB+C
20
Example 2: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
((A + B) – C * (D / E)) + F C Operand Print C (- AB+C
((A + B) – C * (D / E)) + F * Operator P(*)>P(stack[TOP]) so push(*) (-* AB+C
((A + B) – C * (D / E)) + F ( LP Push (‘(‘) (-*( AB+C
((A + B) – C * (D / E)) + F D Operand Print D (-*( AB+CD
((A + B) – C * (D / E)) + F / Operator P(/)>P(stack[TOP]) so push(/) (-*(/ AB+CD
((A + B) – C * (D / E)) + F E Operand Print E (-*(/ AB+CDE
((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘
and discard ‘(‘ - Pop(/)
(-* AB+CDE/
21
Example 2: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘
and discard ‘(‘
Pop(*) and Print
Pop(-) and Print
Discard ‘(‘
(-*
(-
(
Empty
AB+CDE/
AB+CDE/*
AB+CDE/*-
AB+CDE/*-
((A + B) – C * (D / E)) + F + Operator Push(+) + AB+CDE/*-
((A + B) – C * (D / E)) + F F Operand Print F + AB+CDE/*-F
Completed /0 - Pop(+) and Print Empty AB+CDE/*-F+
22
Infix to Prefix Conversion:
23
Example 2: Infix to Prefix Conversion
24
Evaluation of Postfix Expression
• Postfix expression: The expression of the form “a b operator” (ab+)
i.e., when a pair of operands is followed by an operator.
• Input: str = “2 3 1 * + 9 -“
• To evaluate a postfix expression we can use a stack.
• Iterate the expression from left to right and keep on storing the
operands into a stack. Once an operator is received, pop the two
topmost elements and evaluate them and push the result in the stack
again.
25
Follow the steps mentioned below to evaluate
postfix expression using stack:
1. Create a stack to store operands (or values).
2. Scan the given expression from left to right and do the following for
every scanned element.
3. If the element is a number, push it into the stack.
4. If the element is an operator, pop operands for the operator from
the stack. Evaluate the operator and push the result back to the
stack.
5. When the expression is ended, the number in the stack is the final
answer.
26
27
28
29
30
31
32
33
Evaluation of Postfix Expression
34
35

More Related Content

PDF
Data Structures And Algorithms(stacks queues)
PPT
MO 2020 DS Stacks 3 AB.ppt
PPTX
DSA_chapter_04_Stack and data structure and Queue.pptx
PPTX
Data strutcure and annalysis topic stack
PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PPT
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
PPT
Application of Stacks
Data Structures And Algorithms(stacks queues)
MO 2020 DS Stacks 3 AB.ppt
DSA_chapter_04_Stack and data structure and Queue.pptx
Data strutcure and annalysis topic stack
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
Application of Stacks

Similar to STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXPRESSION (20)

PPTX
STACK Applications in DS
PPTX
Lec5-Stack-bukc-28022024-112316am (1) .pptx
PPSX
Data structure_Stack Introduction & app.
PPTX
week9-prefixinfixandpostfixnotations-191013065821.pptx
PPTX
Prefix, Infix and Post-fix Notations
PPTX
Unit 2 application of stack
PPTX
Lecture_04.2.pptx
PPT
data structure and algorithm by bomboat_3
PPTX
DS MOD2 (1) (1).pptx
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
PPTX
stack
PPTX
Application of Stack - Yadraj Meena
PPTX
Applicationofstack by Ali F.RAshid
PPTX
COMP1603 Stacks and RPN 2023 Recording (2).pptx
PDF
Infix to postfix expression in ds
PPTX
Stack Data Structure Intro and Explanation
PDF
stack-111104232459-phpapp02.pdf
PPTX
conversion of Infix to Postfix conversion using stack
PPTX
DS UNIT1_STACKS.pptx
STACK Applications in DS
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Data structure_Stack Introduction & app.
week9-prefixinfixandpostfixnotations-191013065821.pptx
Prefix, Infix and Post-fix Notations
Unit 2 application of stack
Lecture_04.2.pptx
data structure and algorithm by bomboat_3
DS MOD2 (1) (1).pptx
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
stack
Application of Stack - Yadraj Meena
Applicationofstack by Ali F.RAshid
COMP1603 Stacks and RPN 2023 Recording (2).pptx
Infix to postfix expression in ds
Stack Data Structure Intro and Explanation
stack-111104232459-phpapp02.pdf
conversion of Infix to Postfix conversion using stack
DS UNIT1_STACKS.pptx
Ad

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
composite construction of structures.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Sustainable Sites - Green Building Construction
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Strings in CPP - Strings in C++ are sequences of characters used to store and...
UNIT 4 Total Quality Management .pptx
composite construction of structures.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
bas. eng. economics group 4 presentation 1.pptx
Lesson 3_Tessellation.pptx finite Mathematics
web development for engineering and engineering
573137875-Attendance-Management-System-original
OOP with Java - Java Introduction (Basics)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lecture Notes Electrical Wiring System Components
Sustainable Sites - Green Building Construction
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Ad

STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXPRESSION

  • 1. CSA 2100 - Data Structures and Algorithms
  • 2. Contents 1. Introduction to Data Structures 2. Types and Concepts of Arrays 3. Stack 3.1 Concepts and Representation 3.2 Stack operations 3.3 Stack implementation using array 3.4 Applications of Stack. 2
  • 3. 3 Stack Definitions 1. A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO) principle. [T1] 2. A stack is an ordered list in which insertion and deletion are done at one end, called top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out(LIFO) or First in Last out (FILO) list. [T2]
  • 4. 4 Applications of Stack. • Balancing of symbols • Infix-to-postfix Conversion- Lab • Evaluation of postfix expression - Lab • Implementing function calls (including recursion) • Page-visited history in a Web browser [Back Buttons] • Undo sequence in a text editor • Matching Tags in HTML and XML
  • 5. Algebraic Expressions • Infix expressions • An operator appears between its operands • Example: a + b • Prefix expressions • An operator appears before its operands • Example: + a b • Postfix expressions • An operator appears after its operands • Example: a b +
  • 6. A complicated example • Infix expressions: a + b * c + ( d * e + f ) * g • Postfix expressions: a b c * + d e * f + g * + • Prefix expressions: + + a * b c * + * d e f g
  • 7. 7 Table : the operator precedence rules for common mathematical operators
  • 8. 8 Infix-to-postfix Conversion Infix Expression Definition: An infix expression is a mathematical notation where the operators are placed between the operands. This is the most common and familiar notation, especially in arithmetic expressions. Example: A + B (A + B) * C
  • 9. 9 Infix-to-postfix Conversion Postfix Expression (Reverse Polish Notation) Definition: A postfix expression is a mathematical notation where the operators are placed after their operands. There are no parentheses needed, and the order of operations is explicitly defined by the operators’ positions. Example: A B + A B C * +
  • 10. 10 Infix-to-postfix Conversion Prefix Expression (Polish Notation) Definition: A prefix expression is a mathematical notation where the operators are placed before their operands. Like postfix expressions, there are no parentheses needed, and the position of the operators dictates the order of operations. Example: + A B * + A B C
  • 11. 11 Algorithm: Infix to Postfix Conversion 1. Initialize an empty stack for operators and an empty list for the output (postfix expression). 2. Iterate over each character in the infix expression: •Operand (e.g., numbers or variables): Directly append it to the postfix list. •Left parenthesis ( Push it onto the stack. Right parenthesis ): Pop from the stack to the postfix list until a left parenthesis ( is encountered on the stack. Discard the left parenthesis. •Operator (e.g., +, -, *, /):  While the stack is not empty and the operator at the top of the stack has greater or equal precedence than the current operator, pop the operator from the stack to the postfix list.  Push the current operator onto the stack. • After iterating over the expression, pop all remaining operators from the stack to the postfix list. • The postfix list now contains the postfix expression.
  • 12. 12 Infix to Postfix Conversion:
  • 13. 13
  • 14. 14
  • 15. 15
  • 16. 16
  • 17. 17
  • 18. 18 Example 1: Infix to Postfix Conversion Input Current Symbol Type Operation Stack Output A + B * C + D A Operand Print Empty A A + B * C + D + Operator Push(+) + A A + B * C + D B Operand Print + AB A + B * C + D * Operator P(*)>P(stack[TOP]) so Push(*) +* AB A + B * C + D C Operand Print +* ABC A + B * C + D + Operator P(+)<=P(stack[TOP]) so Pop() and Print P(+)<=P(stack[TOP]) so Pop() and Print + + ABC* ABC*+ A + B * C + D D Operand Print + ABC*+D Completed 0 - POP() all the symbols from stack and print Empty ABC*+D+
  • 19. 19 Example 2: Infix to Postfix Conversion Input Current Symbol Type Operation Stack Output ((A + B) – C * (D / E)) + F ( LP Push( ‘(‘ ) ( ((A + B) – C * (D / E)) + F ( LP Push( ‘(‘ ) (( ((A + B) – C * (D / E)) + F A Operand Print A (( A ((A + B) – C * (D / E)) + F + Operator Push (+) ((+ A ((A + B) – C * (D / E)) + F B Operand Print B ((+ AB ((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘ and discard ‘(‘ Pop(+) ( AB+ ((A + B) – C * (D / E)) + F - Operator P(-)>P(stack[TOP]) so push(-) (- AB+ ((A + B) – C * (D / E)) + F C Operand Print C (- AB+C
  • 20. 20 Example 2: Infix to Postfix Conversion Input Current Symbol Type Operation Stack Output ((A + B) – C * (D / E)) + F C Operand Print C (- AB+C ((A + B) – C * (D / E)) + F * Operator P(*)>P(stack[TOP]) so push(*) (-* AB+C ((A + B) – C * (D / E)) + F ( LP Push (‘(‘) (-*( AB+C ((A + B) – C * (D / E)) + F D Operand Print D (-*( AB+CD ((A + B) – C * (D / E)) + F / Operator P(/)>P(stack[TOP]) so push(/) (-*(/ AB+CD ((A + B) – C * (D / E)) + F E Operand Print E (-*(/ AB+CDE ((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘ and discard ‘(‘ - Pop(/) (-* AB+CDE/
  • 21. 21 Example 2: Infix to Postfix Conversion Input Current Symbol Type Operation Stack Output ((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘ and discard ‘(‘ Pop(*) and Print Pop(-) and Print Discard ‘(‘ (-* (- ( Empty AB+CDE/ AB+CDE/* AB+CDE/*- AB+CDE/*- ((A + B) – C * (D / E)) + F + Operator Push(+) + AB+CDE/*- ((A + B) – C * (D / E)) + F F Operand Print F + AB+CDE/*-F Completed /0 - Pop(+) and Print Empty AB+CDE/*-F+
  • 22. 22 Infix to Prefix Conversion:
  • 23. 23 Example 2: Infix to Prefix Conversion
  • 24. 24 Evaluation of Postfix Expression • Postfix expression: The expression of the form “a b operator” (ab+) i.e., when a pair of operands is followed by an operator. • Input: str = “2 3 1 * + 9 -“ • To evaluate a postfix expression we can use a stack. • Iterate the expression from left to right and keep on storing the operands into a stack. Once an operator is received, pop the two topmost elements and evaluate them and push the result in the stack again.
  • 25. 25 Follow the steps mentioned below to evaluate postfix expression using stack: 1. Create a stack to store operands (or values). 2. Scan the given expression from left to right and do the following for every scanned element. 3. If the element is a number, push it into the stack. 4. If the element is an operator, pop operands for the operator from the stack. Evaluate the operator and push the result back to the stack. 5. When the expression is ended, the number in the stack is the final answer.
  • 26. 26
  • 27. 27
  • 28. 28
  • 29. 29
  • 30. 30
  • 31. 31
  • 32. 32
  • 34. 34
  • 35. 35