1 / 17

Stack application: postponing data usage

Stack application: postponing data usage. Arithmetic statement. Example: A+B+C How computers generate it????. Aritmetic expression written in INFIX as above example However compiler change to PREFIX/POSTFIX for calculating purposes. Cont.

holden
Download Presentation

Stack application: postponing data usage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack application: postponing data usage

  2. Arithmetic statement • Example: • A+B+C • How computers generate it????

  3. Aritmetic expression written in INFIX as above example • However compiler change to PREFIX/POSTFIX for calculating purposes

  4. Cont.. HOW TO CONVERT INFIX EXPRESSION TO POSTFIX AND PREFIX??? QUESTION?

  5. CAN BE CONVERT INTO PRASE TREE

  6. WE NEED EVALUATE THE EXPRESSION BY USING STACK CONCEPT

  7. ALGORITHM: find postfix expression value Initialize an empty stack Repeat the following until the end of the expression is encountered: Get the token(/constant, variable, arithmetic operator) in the postfix expression. If token is an operand, push it onto the stack.

  8. If it is an operator, then Pop top two values from the stack. If stack doesn’t not contain two items, error due to a malformed postfix Apply the operator to these two values Push the resulting value back onto the stack When the end of the expression encountered, its value is on top of the stack ( and, in fact, must be only value in the stack)

  9. CONVERT INFIX TO POSTFIX EXPRESSION • E.G: 7+2*3? Stack output 7 7 2 7 2 3 723*+

  10. Algorithm: convert infix to postfix

  11. Hierarchy of operator priority

  12. Converting expression from infix to prefix using stack • It is a bit trickier algorithm in this algorithm we first reverse the input expression so that a+b*c will become c*b+a and then we do the conversion and then again the output string is reversed. Doing this has advantage that except for some minor modifications the algorithm for infix prefix remains almost same as the one for Infix postfix.

  13. algorithm • Reverse the input string • Examine the next element in the input • If it is operand, add it to output string • If it is closing parenthesis, push it on stack • If it is an operator, then • If stack is empty, push operator on stack • If the top of stack is closing parenthesis, push operator on stack • If it has same or higher priority than the top stack, push operator on stack • Else pop the operator from the stack and add it to output string, repeat step 5

  14. Cont.. • If it is a opening parenthesis, pop operators from stack and add them to output string until a closing parenthesis is encountered. Pop and discard the closing parenthesis. • If there is more input go to step 2 • If there is no more input, pop the remaining operators and add them to output string. • Reverse the output string

  15. Example to convert from infixpostfix

  16. CONT..

More Related