# Postfix to Infix Conversion using Stack Data Structure (With C++ Program Code)

In this tutorial we will convert in Postfix Expression to a infix Expression using Stack Data structure. We will understand the Rules to convert an postfix expression to infix expression and also understand the pseudocode. Lastly we will write a C++ program to perform postfix to infix expression conversion.

**Rules for Postfix to Infix using stack DS –**

*Scan POSTFIX expression from LEFT to RIGHT**IF the incoming symbol is a OPERAND, PUSH it onto the Stack**IF the incoming symbol is a OPERATOR, POP 2 OPERANDs from the Stack, ADD this incoming OPERATOR in between the 2 OPERANDs, ADD ‘(‘ & ‘)’ to the whole expression & PUSH this whole new expression string back into the Stack.**At the end POP and PRINT the full INFIX expression from the Stack.*

**PSEUDOCODE –**

**C++ Program to Postfix to Infix Conversion using Stack DS –**

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 40 41 42 43 44 45 46 47 48 49 | #include <iostream> #include <stack> using namespace std; bool isOperand(char c) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { return true; } else { return false; } } string PostfixToInfix(string postfix) { stack<string> s; for (int i = 0; i < postfix.length(); i++) { if (isOperand(postfix[i])) { string op(1, postfix[i]); //string op=postfix[i]; s.push(op); } else { string op1 = s.top(); s.pop(); string op2 = s.top(); s.pop(); s.push("(" + op2 + postfix[i] + op1 + ")"); } } return s.top(); } int main() { string infix, postfix; cout << "Enter a POSTFIX Expression :" << endl; cin >> postfix; cout << "POSTFIX EXPRESSION: " << postfix << endl; infix = PostfixToInfix(postfix); cout << endl << "INFIX EXPRESSION: " << infix; return 0; } |