SlideShare une entreprise Scribd logo
1  sur  39
DATA STRUCTURES
USING
‘C’
Evaluation

Of
Postfix
Evaluation

Of
Postfix
-BY AKHIL AHUJA
What is Postfix expression?
 IF THE OPERATOR SYMBOLS ARE PLACED AFTER ITS
OPERANDS , THEN THE EXPRESSION IS IN POSTFIX
NOTATION.
 Postfix expression is also known as Reverse Polish
Notation(RPN). In RPN the operators follow their operands.
 Despite the name , Reverse Polish Notation is not exactly
the reverse of polish notation , for the operands they are
still written in conventional manner.
For eg : “*63”in polish notation and “63*” in reverse polish
notation.
In this , expression is different but the answer is same
(i.e 18)
Precedence of operators:
1. ‘$’ or ‘^’

Highest Precedence

2. ‘*’ and ‘/’

High Precedence

3. ’+’ and ‘-’

Low Precedence

4. ‘=‘

Lower Precedence

5. ‘(‘ and ‘)’

Lowest Precedence
Fundamental principles followed while
evaluating the postfix expression :
1.Read the expression from left to right.
2.If there comes an operand , push it into the stack.
3.If there comes an operator , pop operand 1 and
operand 2 and then :

A . Push ‘(‘
B . Push ‘operand 2’
C . Push ‘operator’
D . Push ‘operand 1’
E . Push ‘)’
Advantages of Postfix
Expression:
 (A). Postfix notation is easier to work with. In a postfix
expression operators operands appear before the
operators, there is no need of operator precedence and
other rules. In postfix expression the topmost operands
are popped off and operator is applied and the result is
again pushed to the stack and thus finally the stack
contains a single value at the end of the process.
 (B). In postfix expression, there are no parentheses
and therefore the order of evaluation will be
determined by the positions of the operators and
related operands in the expression.
Algorithm for Postfix Expression :
1.Read the element.
2.If element is an operand then:
Push the element into the stack.
3.If element is an operator then :
Pop two operands from the stack.
Evaluate expression formed by two operand and the
operator.
Push the result of expression in the stack end.
4.If no more elements then:
Pop the result
Else
Goto step 1.
How to evaluate postfix (manually)
Going from left to right, if you see an operator, apply it to
the previous two operands (numbers)

Example:
A B

C

*

D

/

+

E

(B*C)
((B*C)/D)
(A+(B*C)/D)

((A+(B*C)/D)-(E-F))
Equivalent infix: A+ B * C / D– (E– F)

F

-

(E – F)

-
Here’s an another example
4

3

*

6

7

+ 5 -

+

)
4

3

*

6

7

+ 5 -

+

)

Push the opening bracket ‘(‘ into the stack .

STACK
4

(
STACK

3

*

6

7

+ 5 -

+

Operand ‘4’ push it into the stack

)
4

3

4
(
STACK

*

6

7

+ 5 -

+

)

Operand ‘3’ push it into the stack.
4

3

*

3
4
(
STACK

6

7

+ 5 -

Now comes Operator ‘*’

+

)
4

3

*

6

7

+ 5 -

*

3
4
(
STACK

Pop operand ‘3’ and ‘4’

+

)
4

3

*

6

7

+ 5 -

4* 3

(
STACK

Evaluate the expression
4*3 =12
Pus it into the stack

+

)
4

3

*

6

7

+ 5 -

4* 3

(
STACK

Evaluate the expression
4*3 =12

+

)
4

3

*

6

7

+ 5 -

4 * 3 12

Push it into the stack

(
STACK

+

)
4

3

*

6

7

+ 5 -

+

Operand ‘6’ push it into the
stack

12
(
STACK

)
4

3

*

6

Operand ‘7’ push it into the
stack

7 +
7

6
12
(
STACK

5

-

+

)
4

3

*

6

7

+ 5 -

Operand ‘7’ and ‘6’
Are popped

Operand ‘+’ push it into the
stack

7
6
12
(
STACK

+

)
4

3

*

6

7

+ 5 6+7

Evaluate ‘6+7’
=13

12
(
STACK

+

)
4

3

*

6

7

+ 5 13

Push it into the stack

12
(
STACK

+

)
4

3

*

6

7

Operand ‘5’ push it into the stack

5
+ 5 -

13

12
(
STACK

+

)
4

3

*

6

Operands ‘5’ and ‘13’ are
popped

7

+ 5 - + )

5
13

Now operator ‘-’ occurs

12
(
STACK
4

3

*

6

7

+ 5 -

Evaluating “13-5”
We get ‘8’

12
(
STACK

+

)
4

3

*

6

7

+ 5 8

Now, push the resultant value i.e ‘8’
in the stack

12
(
STACK

+

)
4

3

*

Operator ‘+’

6

7

+ 5 -

+ )

8

12
(
STACK
4

3

*

6

7

+ 5 -

+

12 + 8
By Evaluating , we get

20
(
STACK

)
4

3

*

6

7

+ 5 -

+

20
So push ‘20’ in stack

(
STACK

)
4

3

*

6

Now atlast ‘)’ occurs
Now , pop the element till
the opening bracket

7

+ 5 -

+ )

20
(
STACK
4

3

*

6

7

+ 5 -

20

+

)

)

(
STACK
4

3

*

6

7

+ 5 -

+

)

20
Now push the element

AND THE FINAL ANSWRER IS ’20’
()
STACK
Evaluation of postfix expression
Evaluation of postfix expression
Evaluation of postfix expression
Evaluation of postfix expression
Evaluation of postfix expression

Contenu connexe

Tendances

Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
mua99
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
Muzamil Hussain
 

Tendances (20)

Evaluation of postfix expression using stack
Evaluation of postfix expression using stackEvaluation of postfix expression using stack
Evaluation of postfix expression using stack
 
Stacks
StacksStacks
Stacks
 
Stack application
Stack applicationStack application
Stack application
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
stack presentation
stack presentationstack presentation
stack presentation
 
stack
stackstack
stack
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 

En vedette

Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
Haqnawaz Ch
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
ecomputernotes
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
Tech_MX
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
Kumar
 

En vedette (20)

Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
The Double Helix - Spaced Learning
The Double Helix - Spaced LearningThe Double Helix - Spaced Learning
The Double Helix - Spaced Learning
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
 
5.stack
5.stack5.stack
5.stack
 
Expression trees
Expression treesExpression trees
Expression trees
 
C applications
C applicationsC applications
C applications
 
Computer notes - Expression Tree
Computer notes - Expression TreeComputer notes - Expression Tree
Computer notes - Expression Tree
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
Polish nootation
Polish nootationPolish nootation
Polish nootation
 
Stack
StackStack
Stack
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
 

Similaire à Evaluation of postfix expression

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
 
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
haaamin01
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
Jay Patel
 

Similaire à Evaluation of postfix expression (20)

Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Lecture6
Lecture6Lecture6
Lecture6
 
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
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with example
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
 
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
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in ds
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Stack
StackStack
Stack
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
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
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
Stack application
Stack applicationStack application
Stack application
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

Evaluation of postfix expression