Prefix,Infix,postfix using stack PDF

Document Details

JudiciousCornflower

Uploaded by JudiciousCornflower

Tags

infix notation postfix notation prefix notation stack algorithm

Summary

This document provides examples and explanations of how to perform operations using stacks to convert between infix, prefix and postfix notations.

Full Transcript

Stack Prefix, Infix and Postfix Notation Infix, Prefix & Postfix Infix to Postfix (A+B/C*(D+E)-F) Symbol Stack Postfix Symbol Stack Postfix ) (+*(+) ABC/DE ( (...

Stack Prefix, Infix and Postfix Notation Infix, Prefix & Postfix Infix to Postfix (A+B/C*(D+E)-F) Symbol Stack Postfix Symbol Stack Postfix ) (+*(+) ABC/DE ( ( (+* ABC/DE+ A ( A - (+* ABC/DE+ + (+ A (+ ABC/DE+* B (+ AB ( ABC/DE+*+ / (+/ AB (- ABC/DE+*+ C (+/ ABC F (- ABC/DE+*+F * (+ ABC/ ) (-) ABC/DE+*+ (+* ABC/ ABC/DE+*+- ( (+*( ABC/ D (+*( ABC/D + (+*(+ ABC/D E (+*(+ ABC/DE A+B*C+D (A+B)*(C+D) A*B+C*D Symb Stack Postfix Symb Stack Postfix Symb Stack Postfix ol ol ol * A A ( ( A A + + + A A ( A * * A B + AB + (+ A B * AB * +* AB B (+ AB + + AB* C +* ABC ) (+) AB+ C + AB*C + ABC* * * AB+ * +* AB*C + ABC*+ ( *( AB+ D +* AB*CD D + ABC*+D C *( AB+C + AB*CD* ABC*+D+ + *(+ AB+C AB*CD*+ D *(+ AB+CD ) *(+) AB+CD+ AB+CD+* (A+B*C)/(D-E-F) A+B+C+D Symb Stack Postfix ol Symb Stack Postfix ol A A A A + (+ A + + A B (+ AB B + AB * (+* AB + + AB+ C (+* ABC C + AB+C ) (+*) ABC + + AB+C+ ABC*+ D + AB+C+D / / ABC*+ AB+C+D+ ( /( ABC*+ D /( ABC*+D - /(- ABC*+D E /(_ ABC*+DE - /(- ABC*+DE- F /(- ABC*+DE-F-/ General Infix to Postfix Conversation A*B+C*D A * B + C * D * * * * + + + + A B * C D * + Postfix Evaluation 456*+ 4 5 6 * + 6 5 5 30 4 4 4 4 34 Postfix Evaluation 78+32+/ 7 8 + 3 2 + / 2 8 3 3 5 7 7 15 15 15 15 3 Postfix Evaluation (10+6/2*(9+10)-12)=>10,6,2,/,9,10,+,*,+,12,- Postfix to Infix Steps to Convert Postfix to Infix Start Iterating the given Postfix Expression from Left to right If Character is operand, then push it into the stack. If Character is operator, then pop top 2 Characters which is operands from the stack. After popping create a string in which coming operator will be in between the operands. push this newly created string into stack. Above process will continue till expression have characters left At the end only one value will remain if there is integers in expressions. If there is character, then one string will be in output as infix expression. abc-+de-+ abc-+de-+ a b c - + d e - + c b b e a a a a d d AB+CD+* ABC*+D+ AB+CD+* ++A*BCD Symbol Stack Symbol Stack Symbol Stack A A A A D D B A,B B A,B C D,C C A,B,C + A+B B D,C,B * A,B*C C A+B,C * D,C*B + A+B*C D A+B,C,D A D,C*B,A D A+B*C,D + A+B,C+D + D,C*B+A + A+B*C+D * A+B*C+D + D+C*B+A A+B*C+D Infix to Prefix A+B*C C * B + A * * + + C B * A + Evaluation Prefix +9*26 6 2 * 9 + 2 9 6 6 12 12 21 Infix to Prefix using Stack K+L-M*N+(O^P)*W/U/V*T+Q InputExp Stack Prefix Q+T*V/U/W*(P^O)+N*M-L+K P +*/ QTV M ++* QTVUWPO^*/ QTVU - ++- QTVUWPO^*// QTVU L ++- QTVUWPO^*// QTVUW + ++-+ QTVUWPO^*//*^OPWUVTQ (A+B)*(C+D) (D+C)*(B+A) Infix to Prefix Symbol Stack Prefix ( ( A+B*C+D D ( D Symbol Stack Prefix + (+ D D D C (+ DC + + D ) (+) DC C + DC DC+ * +* DC * * DC+ B +* DCB ( *( DC+ + + DCB* B *( DC+B ++ DCB* + *(+ DC+B A ++ DCB*A A *(+ DC+BA + DCB*A+ ) *(+) DC+BA DCB*A++ DC+BA+ ++A*BCD DC+BA+* *+AB+CD Prefix to Infix *+AB+CD +*AB*CD +++ABCD Symbol Stack Symbol Stack Symbol Stack D D D D D D C D,C C D,C C D,C + D+C * D *C B D,C,B B D+C,B B D*C,B A D,C,B,A A D+C,B,A A D*C,B,A + D,C,B +A + D+C,B+A * D*C, B *A + D,C +B+A * D+C*B+A + D*C +B*A + D +C+B+A A+B*C+D A*B+C*D A+B+C+D

Use Quizgecko on...
Browser
Browser