Learn How To Convert Postfix To Prefix Notation using Stack in C Programming Language. Before you proceed further with this code, you must know the complete operations of stack data structure.
The Postfix notation is also known as Reverse Polish Notation and the Prefix notation is also known as Polish Notation.
Postfix To Prefix Conversion Example
Postfix String: 44+
Prefix String: +44
Algorithm To Convert Postfix Expression into Prefix Notation
- Scan the Postfix Expression from Left To Right.
- If the character is an Operand, then Push it on to the Stack.
- If the character is an Operator, then Pop Operand 1 and Operand 2 and concatenate them and Push the result on the Stack.
- Repeat the above steps until the Postfix Expression is scanned completely.
- To get the Prefix Expression, Pop the remaining elements of the Stack.
C Program For Postfix To Prefix Conversion using Stack Data Structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include<string.h> #include<stdio.h> #include<stdlib.h> #define MAX 20 char str[MAX], stack[MAX]; int top = -1; char pop() { return stack[top--]; } void push(char ch) { stack[++top] = ch; } void postfix_to_prefix(char expression[]) { int count, length; length = strlen(expression); printf("\nPrefix Expression:\t"); for(count = length - 1; count >= 0; count--) { printf("%c", expression[count]); } } int main() { char postfix_expression[35]; printf("\nEnter Postfix Expression:\t"); scanf("%s", postfix_expression); postfix_to_prefix(postfix_expression); printf("\n"); return 0; } |
Output

In case you get any compilation errors or any doubts in this C Program For Conversion of Postfix Expression into Prefix Expression, let us know about it in the Comment Section below.
You are an idiot