answersLogoWhite

0


Best Answer

Algorithm to Convert Infix to Prefix Form

Suppose A is an arithmetic expression written in infix form. The algorithm finds equivalent prefix expression B.

Step 1. Push ")" onto STACK, and add "(" to end of the A

Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty

Step 3. If an operand is encountered add it to B

Step 4. If a right parenthesis is encountered push it onto STACK

Step 5. If an operator is encountered then:

a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator.

b. Add operator to STACK

Step 6. If left parenthesis is encontered then

a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)

b. Remove the left parenthesis

Step 7. Exit

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

Basically, the shunting algorithm (infix to postfix) works like this (I'm writing this from memory, BTW, so I apologize in advance for any mistakes):
1. You create a stack and an output string.
2. Then you read the infix string one token at a time; each token is either an identifier or an operator:
a. If the token is an identifier (eg, a variable or a constant), then you append it onto the end of the output string.
b. If the token is an operator, then you compare its precedence with the precedence of the operator on the top of the stack.
i. If the operator on the top of stack has lower precedence, then push the new operator onto the stack.
ii. Else, pop the operator from the top of stack and append it to the output string. Then push the new operator onto the stack.
otherwise
c. If the token is an open parenthesis, then push it onto the stack.
d. If the token is a close parenthesis, then pop and append the operators from the stack until the open parenthesis is popped. Discard both parentheses.

Google for "shunting algorithm" for a more detailed and better better description. You might also find reference to an algorithm for converting infix to prefix.
Algorithm_for_infix_to_prefix_conversion

refer this page for your question.....

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Infix: "1 + 2"
Prefix: "+ 1 2"
Postfix: "1 2 +"

The difference is where you put the operator: Betweeen (infix), before (prefix) or after (postfix) the arguments you operate on.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Algorithm for infix to prefix conversion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the time complexity of infix to post fix conversion algorithm?

O(nlogn)


Write an algorithm in c to convert an infix expression to a postfix expressionexecute your algorithm with the following infix expression as your input. m nk pgh a bc?

An algorithm can not be written with the following infix expression without knowing what the expression is. Once this information is included a person will be able to know how to write the algorithm.


What are the three kinds of affixes?

Prefix, suffix and infix


Program to convert infix to prefix?

I dont have the idea about the program but I know that prefix means the first starting letters of a particular things. I really think so there is a progam to convert infix to prefix but i might have misunderstood your question can you make it little simpler please.


Convert infix to prefix to postfix?

(a + b) * c / ((x - y) * z)


Which data structure convert logical to physical address?

Linear data structure is used to convert the logical address to physical address .Stack is used in this and the various conversion such as postfix,prefix and infix notation are come in this


Prefix suffix and infix examples with meaning and word definition?

An example of a prefix in the English language is pre, meaning before. An example of a suffix would be ing, meaning a verbal action. An example of an infix would be ful, meaning full of.


What is prefix expression?

Example: prefix: * 2 + 3 4 infix: 2 * (3+4) postfix: 2 3 4 + *


Algorithm for conversion from prefix to infix?

The belt-and-braces technique is easy enough: > > prefix_to_infix(stream, stack) > if stack is not empty > pop a node off the stack > if this node represents an operator > write an opening parenthesis to stream > prefix_to_infix(stream, stack) > write operator to stream > prefix_to_infix(stream, stack) > write a closing parenthesis to stream > else > write value to stream > endif > endif > endfunc


Who invented postfix and infix?

infix: old Egyptians/Assirs some thousands year before prefix: Jan Łukasiewicz (Polish Notation) postfix: Burks, Warren, and Wright (Reverse Polish Notation)


Prefix to postfix conversion using C programming?

To convert an infix expression to a postfix expression in C programming, you can use the Shunting Yard algorithm. This algorithm allows you to scan the infix expression from left to right, and based on the precedence of operators, convert it to a postfix expression. You can use a stack to hold operators and output queue to store the final postfix expression. By following the algorithm, you can convert the infix expression to postfix successfully.


What are the prefixes and suffixes in punctual compared to the prefixes and suffixes in punctilitous?

The prefix in "punctual" is "pun-" and the suffix is "-ual". In "punctilious", the prefix is "pun-", the infix is "-ctil-", and the suffix is "-ious".