SlideShare a Scribd company logo
1 of 47
Data structures 11-Stacks
using Lists-->BY BABY PATHAK
JNV KHAIRTHAL,ALWAR
Data structure
•The term refers to a data collection with
well defined operations and
behaviour or properties.
Stack
• It is a linear data structure implemented in LIFO
manner.
• LIFO-->LAST IN,FIRST OUT
• Insertions and deletions are restricted to occur only
at one end-stack's top.
• Stack is a dynamic structure.
• Dynamics means it grows with increase in number of
PUSH FUNCTION
• Top = none
40
18
40
top
Push 40 PUSH 18
POP OPERATION
18
40
TOP
Fig: Initial Stack
40
TOP
Click to add text
POP 1 ELEMENT
SOME TERMS RELATED TO STACK
Peek: refers to inspecting the value at the stack's top without
removing it.Also referred to as inspection.
Overflow:This is an error.It occurs when we try to push on a stack
which is already full.
Underflow: This error occurs when one tries to pop/delete item
from an empty stack.
Example of a stack
• Ques: Given a stack of capacity 4 which is initially empty,draw
pictures of the stack after each of the following stacks.Initially the
Stack is empty.
• (I) Stack is empty
Push a
a
Top = 0
Push b
a b
Top = 1
Push c
a b c
Top = 2
pop
a b
Top = 1
Push d
a b d
Top = 2
pop
a b
Top = 1
POLISH STRINGS
• Polish strings ,named after a Polish mathematician ,Jan Lukaiewicz,
refers to the notation in which the operator symbol is placed either
before its operands or after its operands.
Infix Notation Prefix notation postfix notation
A+B +AB AB+
(A-C)*B *-ACB AC-B*
A+(B*C) +A*BC ABC*+
(A+(B*C))/(C-(D*B) /+A*BC-C*DB ABC*+ CDB*-/
HOW TO
CONVERT AN
INFIX
EXPRESSION
TO POST FIX
EXPRESSION
We follow an evaluation order while converting an
infix expression to postfix expression .Evaluation
order is
1 . Brackets or Parenthesis
2. Exponentiation
3. Multiplication or Division
4. Addition or Subtraction
What if operators
of the same
priority are in
one expression
The operators with the
same priority are evaluated
from left to right.
Ways to
convert an infix
expression to
postfix
expression
1. Converting
Manually
01
2.Converting
using Stack
02
Steps for
Manual
Conversion
Determine
Determine the
actual evaluation
order by inserting
braces.
Convert
convert the
expression in the
innermost braces
into postfix
notation by
putting the
operator after the
operands.
Repeat
Repeat
step 2.until entire
expression is
converted
into post fix
notation.
Convert (A+B)*C/D into post fix expression
• Step1: Determine the actual evaluation order by inserting braces,
• So.. if we go from left to right ..after (A+B) evaluation(why A+B evaluated
first??.....because they are in parenthesis.
• So .after A+B EVALUATION ,* should be evaluated..
• Therefore expression now is
• ((A+B)*C)/D
((A+B)*C)/D...SO NOW CONVERT IT INTO POST
FIX
• STEP1: evaluate innermost brackets first..
• So now it is: (AB+*C)/D
• NOW evaluate next level parenthesis
• AB+C*/D
• Now evaluate the last operator left
• AB+C*D/
Q.2 Convert
((A+B)*C/D+E^F
)/G INTO POST
FIX
INSERT PARENTHESIS
1. (A+B)*C/D+(E^F)/G
2.((A+B)*C)/D+(E^F)/G
3.(((A+B)*C)/D)+(E^F)/G
4.((((A+B)*C)/D)+(E^F))/G
((((A+B)*C)/D)+(E^F))/G...NOW
CONVERT INTO POSTFIX
EVALUATE INNERMOST BRACES FIRST.
(((AB+*C)/D)+(E^F))/G
((AB+C*/D)+(E^F))/G
(AB+C*D/+(E^F))/G
(AB+C*D/EF^+)/G
AB+C*D/EF^+G/
Convert to
postfix: A*(B+(C+D)*(E+F)/
G)*H
EVALUATE
A+[(B+C)+(D+E)*F]/G
EVALUATE NOT A OR NOT B AND NOT C
• In logical expression, preference of NOT is highest ,then comes the
preference of AND....OR has the least precedence
• ((NOT A) OR ((NOT B) AND (NOT C)))
• FIRST NOT'S WILL BE EVALUATED...
• THEN AND WILL BE EVALUATED
• AND THEN OR WILL BE EVALUATED
INFIX TO POSTFIX CONVERSION USING STACK
Evaluation of Postfix
expression using
Stacks
The steps are:
Repeat Repeat it till the end of the expression
Push back Push back the result of the evaluation.
Pop Pop the two operands from the stack,if the element is a
binary operator..In case of Unary operator,pop only one element.
Push Push the element in the stack if it is an operand.
Read Read the expression from left to right;
Evaluate: true false true NOT false true OR NOT
AND OR AND
Read from left to right.
I. 'true' is operand; pushed into
stack
TRUE
II. 'false' is operand; pushed into stack
FALSE
TRUE
III. 'true' is operand;
pushed into stack
true
false
true
IV.'NOT' is an Unary
Operator
• Evaluate one operand I.e.
True...NOT(TRUE) = FALSE. Push the result
'FALSE' into the stack
FALSE
FALSE
TRUE
V. 'FALSE' is
operand; pushed
FALSE
FALSE
FALSE
TRUE
VI. 'TRUE' is
operand,
pushed Stack
• ….
true
false
false
false
true
VII. 'OR' is operator.
• Push two operands I.e. true, FALSE.. Evaluate
false OR true = true.
• Push true into the stack
TRUE
FALSE
FALSE
TRUE
VIII. 'NOT' is unary
operator
• POP ONE OPERAND...NOT(TRUE) =
FALSE
• PUSH FALSE ,INTO THE STACK
FALSE
FALSE
FALSE
TRUE
IX. AND IS OPERATOR
• POP TWO OPERANDS...false AND false =
false
• PUSH result 'FALSE' into the stack
false
false
x. 'OR' is
operator. Pop
two operands
• False OR false = False.
• Push the result False onto the
stack
false
true
XI.'AND' is operator
• POP two operands I.e.
false,true
• True AND false = false..
• Push result back on STACK
FALSE
FINALLY....
NO MORE ELEMENTS POP FROM STACK,THEREFORE ,THE
RESULT IS'FALSE'
EVALUATE 562+*12 4/- in tabular form..
showing stack status after every step.
step Input symbol/element Stack Intermediate calculations
1, 5 PUSH 5
2. 6 PUSH 5,6
3. 2 PUSH 5,6,2
4. + POP(2 ELEMENTS) &
evaluate
5. 6+2 = 8
5 Result pushed.. push 8 5,8
6. * Pop(2 elements) &
evaluate
# empty 5*8 = 40
7. Push result(40) 40
8. 12 push 40,12
9. 4 push 40,12,4
Convert (TRUE and FALSE) or not(FALSE or
TRUE)
STEP SYMBOL STACK POSTFIX
EXPRESSION
DESCRIPTION
0. [
1. ( [( PUSH (
2. TRUE [( TRUE
3. and [(and TRUE PUSH and
4. FALSE [(and TRUE FALSE
5. ) [ TRUE FALSE and POP and
6. or [or TRUE FALSE and PUSH or
7. not [or not TRUE FALSE and
8. ( [or not( TRUE FALSE and
9. FALSE [or not( TRUE FALSE and
FALSE
10. or [or not(or TRUE FALSE
Evaluate 30,5,2^,12,6,/,+,-
step Input symbol/element stack Intemediate calculation
1 (
2. 30..push (,30
3. 5..push (,30,5
4. 2...push (,30,5,2
5. ^ ...pop two operands (30, 5^2=25..push 25
6. (30,25
7. 12..push on stack (30,25,12
8. 6...push (30,25,12,6
9. /..pop two operands (30,25 12/6 = 2..push 2
10. (30,25,2
11. +...pop two operands (30 25+2 = 27 push 27
12 (30,27

More Related Content

Similar to STACK USING LISTS.pptx

Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfKalpana Mohan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
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
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptxHalid Assen
 
COMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptxCOMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptxasdadad5
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Jay Patel
 

Similar to STACK USING LISTS.pptx (20)

Stacks
StacksStacks
Stacks
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
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 - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
Stack
StackStack
Stack
 
COMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptxCOMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptx
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
Stack
StackStack
Stack
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
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
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
“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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
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
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
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
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
“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...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
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
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
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
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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...
 

STACK USING LISTS.pptx

  • 1. Data structures 11-Stacks using Lists-->BY BABY PATHAK JNV KHAIRTHAL,ALWAR
  • 2. Data structure •The term refers to a data collection with well defined operations and behaviour or properties.
  • 3. Stack • It is a linear data structure implemented in LIFO manner. • LIFO-->LAST IN,FIRST OUT • Insertions and deletions are restricted to occur only at one end-stack's top. • Stack is a dynamic structure. • Dynamics means it grows with increase in number of
  • 4. PUSH FUNCTION • Top = none 40 18 40 top Push 40 PUSH 18
  • 5. POP OPERATION 18 40 TOP Fig: Initial Stack 40 TOP Click to add text POP 1 ELEMENT
  • 6. SOME TERMS RELATED TO STACK Peek: refers to inspecting the value at the stack's top without removing it.Also referred to as inspection. Overflow:This is an error.It occurs when we try to push on a stack which is already full. Underflow: This error occurs when one tries to pop/delete item from an empty stack.
  • 7. Example of a stack • Ques: Given a stack of capacity 4 which is initially empty,draw pictures of the stack after each of the following stacks.Initially the Stack is empty. • (I) Stack is empty
  • 10. Push c a b c Top = 2
  • 12. Push d a b d Top = 2
  • 14.
  • 15. POLISH STRINGS • Polish strings ,named after a Polish mathematician ,Jan Lukaiewicz, refers to the notation in which the operator symbol is placed either before its operands or after its operands.
  • 16. Infix Notation Prefix notation postfix notation A+B +AB AB+ (A-C)*B *-ACB AC-B* A+(B*C) +A*BC ABC*+ (A+(B*C))/(C-(D*B) /+A*BC-C*DB ABC*+ CDB*-/
  • 17. HOW TO CONVERT AN INFIX EXPRESSION TO POST FIX EXPRESSION We follow an evaluation order while converting an infix expression to postfix expression .Evaluation order is 1 . Brackets or Parenthesis 2. Exponentiation 3. Multiplication or Division 4. Addition or Subtraction
  • 18. What if operators of the same priority are in one expression The operators with the same priority are evaluated from left to right.
  • 19. Ways to convert an infix expression to postfix expression 1. Converting Manually 01 2.Converting using Stack 02
  • 20. Steps for Manual Conversion Determine Determine the actual evaluation order by inserting braces. Convert convert the expression in the innermost braces into postfix notation by putting the operator after the operands. Repeat Repeat step 2.until entire expression is converted into post fix notation.
  • 21. Convert (A+B)*C/D into post fix expression • Step1: Determine the actual evaluation order by inserting braces, • So.. if we go from left to right ..after (A+B) evaluation(why A+B evaluated first??.....because they are in parenthesis. • So .after A+B EVALUATION ,* should be evaluated.. • Therefore expression now is • ((A+B)*C)/D
  • 22. ((A+B)*C)/D...SO NOW CONVERT IT INTO POST FIX • STEP1: evaluate innermost brackets first.. • So now it is: (AB+*C)/D • NOW evaluate next level parenthesis • AB+C*/D • Now evaluate the last operator left • AB+C*D/
  • 23. Q.2 Convert ((A+B)*C/D+E^F )/G INTO POST FIX INSERT PARENTHESIS 1. (A+B)*C/D+(E^F)/G 2.((A+B)*C)/D+(E^F)/G 3.(((A+B)*C)/D)+(E^F)/G 4.((((A+B)*C)/D)+(E^F))/G
  • 24. ((((A+B)*C)/D)+(E^F))/G...NOW CONVERT INTO POSTFIX EVALUATE INNERMOST BRACES FIRST. (((AB+*C)/D)+(E^F))/G ((AB+C*/D)+(E^F))/G (AB+C*D/+(E^F))/G (AB+C*D/EF^+)/G AB+C*D/EF^+G/
  • 27. EVALUATE NOT A OR NOT B AND NOT C • In logical expression, preference of NOT is highest ,then comes the preference of AND....OR has the least precedence • ((NOT A) OR ((NOT B) AND (NOT C))) • FIRST NOT'S WILL BE EVALUATED... • THEN AND WILL BE EVALUATED • AND THEN OR WILL BE EVALUATED
  • 28. INFIX TO POSTFIX CONVERSION USING STACK
  • 29.
  • 31. The steps are: Repeat Repeat it till the end of the expression Push back Push back the result of the evaluation. Pop Pop the two operands from the stack,if the element is a binary operator..In case of Unary operator,pop only one element. Push Push the element in the stack if it is an operand. Read Read the expression from left to right;
  • 32.
  • 33. Evaluate: true false true NOT false true OR NOT AND OR AND Read from left to right. I. 'true' is operand; pushed into stack TRUE
  • 34. II. 'false' is operand; pushed into stack FALSE TRUE
  • 35. III. 'true' is operand; pushed into stack true false true
  • 36. IV.'NOT' is an Unary Operator • Evaluate one operand I.e. True...NOT(TRUE) = FALSE. Push the result 'FALSE' into the stack FALSE FALSE TRUE
  • 37. V. 'FALSE' is operand; pushed FALSE FALSE FALSE TRUE
  • 38. VI. 'TRUE' is operand, pushed Stack • …. true false false false true
  • 39. VII. 'OR' is operator. • Push two operands I.e. true, FALSE.. Evaluate false OR true = true. • Push true into the stack TRUE FALSE FALSE TRUE
  • 40. VIII. 'NOT' is unary operator • POP ONE OPERAND...NOT(TRUE) = FALSE • PUSH FALSE ,INTO THE STACK FALSE FALSE FALSE TRUE
  • 41. IX. AND IS OPERATOR • POP TWO OPERANDS...false AND false = false • PUSH result 'FALSE' into the stack false false
  • 42. x. 'OR' is operator. Pop two operands • False OR false = False. • Push the result False onto the stack false true
  • 43. XI.'AND' is operator • POP two operands I.e. false,true • True AND false = false.. • Push result back on STACK FALSE
  • 44. FINALLY.... NO MORE ELEMENTS POP FROM STACK,THEREFORE ,THE RESULT IS'FALSE'
  • 45. EVALUATE 562+*12 4/- in tabular form.. showing stack status after every step. step Input symbol/element Stack Intermediate calculations 1, 5 PUSH 5 2. 6 PUSH 5,6 3. 2 PUSH 5,6,2 4. + POP(2 ELEMENTS) & evaluate 5. 6+2 = 8 5 Result pushed.. push 8 5,8 6. * Pop(2 elements) & evaluate # empty 5*8 = 40 7. Push result(40) 40 8. 12 push 40,12 9. 4 push 40,12,4
  • 46. Convert (TRUE and FALSE) or not(FALSE or TRUE) STEP SYMBOL STACK POSTFIX EXPRESSION DESCRIPTION 0. [ 1. ( [( PUSH ( 2. TRUE [( TRUE 3. and [(and TRUE PUSH and 4. FALSE [(and TRUE FALSE 5. ) [ TRUE FALSE and POP and 6. or [or TRUE FALSE and PUSH or 7. not [or not TRUE FALSE and 8. ( [or not( TRUE FALSE and 9. FALSE [or not( TRUE FALSE and FALSE 10. or [or not(or TRUE FALSE
  • 47. Evaluate 30,5,2^,12,6,/,+,- step Input symbol/element stack Intemediate calculation 1 ( 2. 30..push (,30 3. 5..push (,30,5 4. 2...push (,30,5,2 5. ^ ...pop two operands (30, 5^2=25..push 25 6. (30,25 7. 12..push on stack (30,25,12 8. 6...push (30,25,12,6 9. /..pop two operands (30,25 12/6 = 2..push 2 10. (30,25,2 11. +...pop two operands (30 25+2 = 27 push 27 12 (30,27