SlideShare a Scribd company logo
1 of 25
Topic : “Application of Stack
For Expression
Evaluation”
P.E.S. COLLEGE OF ENGINEERING
Name : Zodge Prakash Rajesh
Roll No. : B-41
Department : CSE
Semester : IV
Contents
⚫ What is Stack?
⚫ Concept of Stack
⚫ Stack Operations
⚫ PUSH & POP
⚫ ALGORITHMS FOR PUSH & POP
⚫ ALGORITHM FOR PEEP
⚫ ARITHMATIC EXPRESSION
⚫ APPLICATION OF STACK
⚫ EXPRESSION CONVERSION
⚫ INFIX, PREFIX & POSTFIX NOTATIONS
⚫ ALGORITHMS
⚫ REFERENCES
What is Stack?
⚫ A stack is an Abstract Data Type (ADT)
⚫ Stack is an important data structure which stores its elements in an
ordered manner.
Concepts of Stack
⚫ LIFO data structure
Stack Operations
⚫ PUSH – The process of putting a new data
element onto stack is known as a Push
Operation.
⚫ POP – Accessing the content while removing it
from the stack, is known as a Pop Operation.
⚫ PEEK – Get the data of top element of stack, return the
value of the top element
PUSH Operation
⚫ The push operation is used to insert an element in to the stack.
POP Operation
⚫ The pop operation is used to delete the topmost element from the stack.
Algorithm for PUSH & POP
Algorithm to PUSH an element in a stack
⚫ Step 1: IF TOP = MAX-1, then
PRINT “OVERFLOW”
Goto Step 4
[END OF IF]
⚫ Step 2: SET TOP = TOP + 1
⚫ Step 3: SET STACK[TOP] = VALUE
⚫ Step 4: END
Algorithm to POP an element from a stack
⚫ Step 1: IF TOP = NULL, then
PRINT “UNDERFLOW”
Goto Step 4
[END OF IF]
⚫ Step 2: SET VAL = STACK[TOP]
⚫ Step 3: SET TOP = TOP - 1
⚫ Step 4: END
Algorithm for PEEP Operation
Algorithm for Peep Operation
⚫ Step 1: IF TOP = NULL, then
PRINT “STACK IS EMPTY”
Go TO Step 3
[END OF IF]
⚫ Step 2: RETURN STACK[TOP]
⚫ Step 3: END
Arithmetic Expression
Arithmetic expressions have
⚫ Operands &
⚫ Operators (+,-,*,/,%)
Priority conventions are
⚫ Brackets [Highest Priority - I]
⚫ ~ Unary minus [Highest Priority - II]
⚫ *,/,% [Medium Priority]
⚫ +,- [Low Priority]
Applications of Stack
⚫ Express Evaluation
Ex. 5 * ( 6 + 2 )
Where ( 6 +2 ) = 8 will be evaluated first.
⚫ Parenthesis Matching
Ex. ( a + b ) * ( c + d)
Whereas, (a + b * [c + d ) is not a valid expression
⚫ Expression Conversion
Infix, Prefix & Postfix
⚫ Memory Management
Expression Conversion
⚫ Converting one form of expressions to another is one of the important
applications of stacks.
Infix Notation
⚫ Writing an arithmetic expression using infix notation, the operator is
placed between the operands.
Ex. A+B
⚫ Here, plus operator is placed between the two operands A and B.
Postfix Notation
⚫ Postfix notation was given by Jan Łukasiewicz
⚫ The operator is placed after the operands
Ex. Infix : ( A + B ) Postfix : A B +
⚫ A postfix operation does not even follow the rules of operator precedence.
⚫ No parentheses
Ex. Infix : ( A + B ) * C Postfix : A B + C *
Prefix Notation
⚫ The operator is placed before the operands
Ex. Infix : A + B Prefix : + A B
• The expression (A + B) * C
is written as:
* + A B C in the prefix notation
Infix to Postfix Algorithm
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
1 If the precedence of the scanned operator is greater than the precedence of
the operator in the stack(or the stack is empty or the stack contains a
‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater than or equal to
in precedence than that of the scanned operator. After doing that Push the
scanned operator to the stack. (If you encounter parenthesis while popping
then stop there and push the scanned operator in the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is
encountered, and discard both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.
Continue…
Infix to Prefix Algorithm
⚫ Step 1: Reverse the infix expression i.e A+B*C will become C*B+A.
Note while reversing each ‘(‘ will become ‘)’ and each ‘)’
becomes ‘(‘.
⚫ Step 2: Obtain the “nearly” postfix expression of the modified
expression i.e CB*A+.
⚫ Step 3: Reverse the postfix expression. Hence in our example prefix
is +A*BC.
Continue…
Postfix to Infix Algorithm
1. While there are input symbol left
1.1 Read the next symbol from the input.
2. If the symbol is an operand
2.1 Push it onto the stack.
3. Otherwise,
3.1 the symbol is an operator.
3.2 Pop the top 2 values from the stack.
3.3 Put the operator, with the values as arguments and form a string.
3.4 Push the resulted string back to stack.
4. If there is only one value in the stack
4.1 That value in the stack is the desired infix string
Continue…
Postfix to Prefix Algorithm
1. Read the Postfix expression from left to right
2. If the symbol is an operand, then push it onto the Stack
3. If the symbol is an operator, then pop two operands from the Stack
Create a string by concatenating the two operands and the operator
before them.
string = operator + operand2 + operand1
And push the resultant string back to Stack
4. Repeat the above steps until end of Prefix expression.
Continue…
References
⚫ https://prepinsta.com/
⚫ https://www.geeksforgeeks.org/
⚫ https://www.tutorialspoint.com/
⚫ https://www.faceprep.in/
Thank You

More Related Content

What's hot (20)

Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Queues
QueuesQueues
Queues
 
Stack
StackStack
Stack
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
stack & queue
stack & queuestack & queue
stack & queue
 
Linked list
Linked listLinked list
Linked list
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
Stacks
StacksStacks
Stacks
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
stack presentation
stack presentationstack presentation
stack presentation
 
Queue
QueueQueue
Queue
 
Queues
QueuesQueues
Queues
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 

Similar to Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURESUsha Mahalingam
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9sumitbardhan
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Concept of stack ,stack of aaray stack by linked list , application of stac...
Concept of stack ,stack of aaray   stack by linked list , application of stac...Concept of stack ,stack of aaray   stack by linked list , application of stac...
Concept of stack ,stack of aaray stack by linked list , application of stac...muskankumari7360
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptxRockyIslam5
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptxBaby81727
 

Similar to Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx (20)

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Stack
StackStack
Stack
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Concept of stack ,stack of aaray stack by linked list , application of stac...
Concept of stack ,stack of aaray   stack by linked list , application of stac...Concept of stack ,stack of aaray   stack by linked list , application of stac...
Concept of stack ,stack of aaray stack by linked list , application of stac...
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx

  • 1. Topic : “Application of Stack For Expression Evaluation” P.E.S. COLLEGE OF ENGINEERING Name : Zodge Prakash Rajesh Roll No. : B-41 Department : CSE Semester : IV
  • 2. Contents ⚫ What is Stack? ⚫ Concept of Stack ⚫ Stack Operations ⚫ PUSH & POP ⚫ ALGORITHMS FOR PUSH & POP ⚫ ALGORITHM FOR PEEP ⚫ ARITHMATIC EXPRESSION ⚫ APPLICATION OF STACK ⚫ EXPRESSION CONVERSION ⚫ INFIX, PREFIX & POSTFIX NOTATIONS ⚫ ALGORITHMS ⚫ REFERENCES
  • 3. What is Stack? ⚫ A stack is an Abstract Data Type (ADT) ⚫ Stack is an important data structure which stores its elements in an ordered manner.
  • 4. Concepts of Stack ⚫ LIFO data structure
  • 5. Stack Operations ⚫ PUSH – The process of putting a new data element onto stack is known as a Push Operation. ⚫ POP – Accessing the content while removing it from the stack, is known as a Pop Operation. ⚫ PEEK – Get the data of top element of stack, return the value of the top element
  • 6. PUSH Operation ⚫ The push operation is used to insert an element in to the stack.
  • 7. POP Operation ⚫ The pop operation is used to delete the topmost element from the stack.
  • 8. Algorithm for PUSH & POP Algorithm to PUSH an element in a stack ⚫ Step 1: IF TOP = MAX-1, then PRINT “OVERFLOW” Goto Step 4 [END OF IF] ⚫ Step 2: SET TOP = TOP + 1 ⚫ Step 3: SET STACK[TOP] = VALUE ⚫ Step 4: END Algorithm to POP an element from a stack ⚫ Step 1: IF TOP = NULL, then PRINT “UNDERFLOW” Goto Step 4 [END OF IF] ⚫ Step 2: SET VAL = STACK[TOP] ⚫ Step 3: SET TOP = TOP - 1 ⚫ Step 4: END
  • 9. Algorithm for PEEP Operation Algorithm for Peep Operation ⚫ Step 1: IF TOP = NULL, then PRINT “STACK IS EMPTY” Go TO Step 3 [END OF IF] ⚫ Step 2: RETURN STACK[TOP] ⚫ Step 3: END
  • 10. Arithmetic Expression Arithmetic expressions have ⚫ Operands & ⚫ Operators (+,-,*,/,%) Priority conventions are ⚫ Brackets [Highest Priority - I] ⚫ ~ Unary minus [Highest Priority - II] ⚫ *,/,% [Medium Priority] ⚫ +,- [Low Priority]
  • 11. Applications of Stack ⚫ Express Evaluation Ex. 5 * ( 6 + 2 ) Where ( 6 +2 ) = 8 will be evaluated first. ⚫ Parenthesis Matching Ex. ( a + b ) * ( c + d) Whereas, (a + b * [c + d ) is not a valid expression ⚫ Expression Conversion Infix, Prefix & Postfix ⚫ Memory Management
  • 12. Expression Conversion ⚫ Converting one form of expressions to another is one of the important applications of stacks.
  • 13. Infix Notation ⚫ Writing an arithmetic expression using infix notation, the operator is placed between the operands. Ex. A+B ⚫ Here, plus operator is placed between the two operands A and B.
  • 14. Postfix Notation ⚫ Postfix notation was given by Jan Łukasiewicz ⚫ The operator is placed after the operands Ex. Infix : ( A + B ) Postfix : A B + ⚫ A postfix operation does not even follow the rules of operator precedence. ⚫ No parentheses Ex. Infix : ( A + B ) * C Postfix : A B + C *
  • 15. Prefix Notation ⚫ The operator is placed before the operands Ex. Infix : A + B Prefix : + A B • The expression (A + B) * C is written as: * + A B C in the prefix notation
  • 16. Infix to Postfix Algorithm 1. Scan the infix expression from left to right. 2. If the scanned character is an operand, output it. 3. Else, 1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it. 2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.) 4. If the scanned character is an ‘(‘, push it to the stack. 5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the parenthesis. 6. Repeat steps 2-6 until infix expression is scanned. 7. Print the output 8. Pop and output from the stack until it is not empty.
  • 18. Infix to Prefix Algorithm ⚫ Step 1: Reverse the infix expression i.e A+B*C will become C*B+A. Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘. ⚫ Step 2: Obtain the “nearly” postfix expression of the modified expression i.e CB*A+. ⚫ Step 3: Reverse the postfix expression. Hence in our example prefix is +A*BC.
  • 20. Postfix to Infix Algorithm 1. While there are input symbol left 1.1 Read the next symbol from the input. 2. If the symbol is an operand 2.1 Push it onto the stack. 3. Otherwise, 3.1 the symbol is an operator. 3.2 Pop the top 2 values from the stack. 3.3 Put the operator, with the values as arguments and form a string. 3.4 Push the resulted string back to stack. 4. If there is only one value in the stack 4.1 That value in the stack is the desired infix string
  • 22. Postfix to Prefix Algorithm 1. Read the Postfix expression from left to right 2. If the symbol is an operand, then push it onto the Stack 3. If the symbol is an operator, then pop two operands from the Stack Create a string by concatenating the two operands and the operator before them. string = operator + operand2 + operand1 And push the resultant string back to Stack 4. Repeat the above steps until end of Prefix expression.
  • 24. References ⚫ https://prepinsta.com/ ⚫ https://www.geeksforgeeks.org/ ⚫ https://www.tutorialspoint.com/ ⚫ https://www.faceprep.in/