answersLogoWhite

0


Best Answer

struct stack { char ele; struct stack *next; }; void push(int); int pop(); int precedence(char); struct stack *top = NULL; int main() { char infix[20], postfix[20]; int i=0,j=0; printf("ENTER INFIX EXPRESSION: "); gets(infix); while(infix[i]!='\0') { if(isalnum(infix[i])) postfix[j++]=infix[i]; else { if(top==NULL) push(infix[i]); else { while(top!=NULL && (precedence(top->ele)>=precedence(infix[i]))) postfix[j++]=pop(); push(infix[i]); } } ++i; } while(top!=NULL) postfix[j++]=pop(); postfix[j]='\0'; puts(postfix); getchar(); return 0; } int precedence(char x) { switch(x) { case '^': return 4; case '*': case '/': return 3; case '+': case '-': return 2; default: return 0; } } void push(int x) { int item; struct stack *tmp; if(top==NULL) { top=(struct stack *)malloc(sizeof(struct stack)); top->ele=x; top->next=NULL; } else { tmp=top; top->ele=x; top->next=tmp; } } int pop() { struct stack *tmp; int item; if(top==NULL) puts("EMPTY STACK"); else if(top->next==NULL) { tmp=top; item=top->ele; top=NULL; free(tmp); } else { tmp=top; item=top->ele; top=top->next; free(tmp); } return item; }

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you convert a prefix expression to postfix using recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is prefix expression?

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


What is the difference between prefix and postfix increment operator in c plus plus?

Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not. int a = 1; int b = ++a; // both a and b are now equal to 2 int a = 1; int b = a++; // a is equal to 2 and b is equal to 1


Why parenthesis are never needed in prefix or postfix notation?

Because there is not an "order of operations" in prefix or postfix notation. The order in which you put the numbers and operators is the order in which calculation occurs.


How do infix notation and postfix notation differ?

It's simply a matter of where the operators are placed in relation to their operands: infix: X + Y prefix: + X Y postfix: X Y + All of the above are equivalent. Prefix notation is also known as Polish notation, hence postfix is also known as reverse Polish notation. Given the infix equation A * B + C / D, the order of evaluation is always parenthesis, orders, divide/multiply, add/subtract (PODMAS), thus we must multiply A by B first, then divide C by D, and finally add the two results together. If we wish to perform the addition first, then we must re-write the equation with parenthesis: A * (B + C) / D. With postfix and prefix notation, operator precedence becomes superfluous because we always evaluate these expressions in left-to-right order: Infix A * B + C / D becomes postfix A B * C D / + or prefix / * A + B C D Infix A * (B + C) / D becomes postfix A B C + * D / or prefix + * A B / C D When we eliminate operator precedence with postfix or prefix notation, we greatly simplify the algorithm required to evaluate complex expressions. For example, given the postfix expression A B C + * D /, we simply read the symbols one at a time, placing them on a stack, until we encounter an operator. We then pop the first two elements off the stack, perform the operation, and then pop the result back on the stack. We repeat this process until there are no more symbols left, at which point the stack holds just one value: the result. With prefix notation, we place the operators on the stack instead of the operands. When we read the first operand we simply store it in an accumulator. We continue pushing operators onto the stack until we encounter the second operand, at which point we can pop the first operator off the stack, perform the operation and update the accumulator. We repeat this process until there are no symbols left, at which point the accumulator holds the final result. Note that when presented with an infix expression, a machine has to convert the expression to the equivalent prefix or postfix expression before it can be evaluated. By eliminating this conversion process, computation by machine can be performed with much greater speed.


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)

Related questions

How to convert prefix to postfix?

convert to perfixed to postfixed


Convert infix to prefix to postfix?

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


What is prefix expression?

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


What is the difference between prefix and postfix increment operator in c plus plus?

Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not. int a = 1; int b = ++a; // both a and b are now equal to 2 int a = 1; int b = a++; // a is equal to 2 and b is equal to 1


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 is a 'post fix expression' in java programming?

Postfix expressions are expressions where the operator is at the end of the expression. These include the "++" (increment) and "--" (decrement) operators. Most Java expressions use in-fix notation (e.g. "a + b") but the increment and decrement operators can be postfix ("e.g. "a++" to increment variable a) or even prefix (e.g. "++a").


Why parenthesis are never needed in prefix or postfix notation?

Because there is not an "order of operations" in prefix or postfix notation. The order in which you put the numbers and operators is the order in which calculation occurs.


Code for convertion of prefix to postfix?

I don't know what you mean with "conversion". Prefix means the "++" or "--" is in front of the variable:++x;Post-fix means it is after the variable:x++;The examples do the same, but if the "++" or "--" is part of a larger expression, prefix will be evaluated before the remaining expression; postfix after:a = 5;b = ++a; // First increment a, then assign to b - b will be 6a = 5;b = a++; // First assign to b, then increment - b will be 5----What they (probably) mean is converting from '+ * 5 3 1' to '5 3 * 1 +'


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


How do infix notation and postfix notation differ?

It's simply a matter of where the operators are placed in relation to their operands: infix: X + Y prefix: + X Y postfix: X Y + All of the above are equivalent. Prefix notation is also known as Polish notation, hence postfix is also known as reverse Polish notation. Given the infix equation A * B + C / D, the order of evaluation is always parenthesis, orders, divide/multiply, add/subtract (PODMAS), thus we must multiply A by B first, then divide C by D, and finally add the two results together. If we wish to perform the addition first, then we must re-write the equation with parenthesis: A * (B + C) / D. With postfix and prefix notation, operator precedence becomes superfluous because we always evaluate these expressions in left-to-right order: Infix A * B + C / D becomes postfix A B * C D / + or prefix / * A + B C D Infix A * (B + C) / D becomes postfix A B C + * D / or prefix + * A B / C D When we eliminate operator precedence with postfix or prefix notation, we greatly simplify the algorithm required to evaluate complex expressions. For example, given the postfix expression A B C + * D /, we simply read the symbols one at a time, placing them on a stack, until we encounter an operator. We then pop the first two elements off the stack, perform the operation, and then pop the result back on the stack. We repeat this process until there are no more symbols left, at which point the stack holds just one value: the result. With prefix notation, we place the operators on the stack instead of the operands. When we read the first operand we simply store it in an accumulator. We continue pushing operators onto the stack until we encounter the second operand, at which point we can pop the first operator off the stack, perform the operation and update the accumulator. We repeat this process until there are no symbols left, at which point the accumulator holds the final result. Note that when presented with an infix expression, a machine has to convert the expression to the equivalent prefix or postfix expression before it can be evaluated. By eliminating this conversion process, computation by machine can be performed with much greater speed.


What is an expression for how would you convert 600 kilograms into grams?

Since the prefix "kilo" means thousand, you multiply by 1000.


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)