Infix to Postfix Conversion using Stack in C++ Last Updated : 18 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Infix expression is a common way of writing mathematical expressions where operators are written between the operands whereas postfix is a type of expression in which a pair of operands is followed by an operator. In this article, we will learn how to use a stack to convert an infix expression to a postfix expression in C++. Example: Input:Infix expression: “A+B*C”Output:Postfix expression: “ABC*+”Infix to Postfix Conversion using Stack in C++To convert an infix expression to a postfix expression using a std::stack in C++, we can follow the below approach: Approach:Create an empty stack for storing operators and a string for storing the result.Scan the infix expression from left to right.If the scanned character is an operand, append it to the result.If the scanned character is an operator, pop operators from the stack to the result until the top of the stack has an operator of lower precedence or the stack is empty, then push the scanned operator onto the stack.If the scanned character is ‘(’, push it onto the stack.If the scanned character is ‘)’, pop operators from the stack to the result until ‘(’ is encountered, and pop ‘(’ from the stack.After all characters are scanned, pop the remaining operators from the stack to the result.C++ Program to Convert an Infix Expression to a Postfix Expression using a Stack The following program illustrates how we can convert an infix expression to a postfix expression using a stack in C++. C++ // C++ Program to illustrate how to use a stack to convert // an infix expression to a postfix expression #include <iostream> #include <stack> #include <string> using namespace std; // Function to check the precedence of operators int precedence(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; } // Function to convert infix expression to postfix // expression string infixToPostfix(string infix) { stack<char> st; string postfix = ""; for (int i = 0; i < infix.length(); i++) { char c = infix[i]; // If the scanned character is an operand, add it to // output string. if (isalnum(c)) postfix += c; // If the scanned character is an '(', push it to // the stack. else if (c == '(') st.push('('); // If the scanned character is an ')', pop and to // output string from the stack until an '(' is // encountered. else if (c == ')') { while (st.top() != '(') { postfix += st.top(); st.pop(); } st.pop(); } // If an operator is scanned else { while (!st.empty() && precedence(c) <= precedence(st.top())) { postfix += st.top(); st.pop(); } st.push(c); } } // Pop all the remaining elements from the stack while (!st.empty()) { postfix += st.top(); st.pop(); } return postfix; } int main() { string infix = "A+B*C"; cout << "Infix Expression: " << infix << endl; cout << "Postfix Expression: " << infixToPostfix(infix) << endl; return 0; } OutputInfix Expression: A+B*C Postfix Expression: ABC*+ Time Complexity: O(N), here N is the length of infix expression.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Evaluate a Postfix Expression using Stack in C++? U ujjwalshrivastava2309 Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Evaluate a Postfix Expression using Stack in C++? The type of expression in which a pair of operands is followed by an operator is called a postfix expression. In this article, we will learn how we can use the stack data structure to evaluate the value of a postfix expression in C++. Example: Input: Postfix expression: "73*4+"Output: 25Evaluating P 3 min read How to Create Stack of Tuples in C++? In C++, a stack is a container adapter that provides a LIFO (Last In First Out) order of element insertion and deletion which is only possible at the end. A tuple is a container that can store elements of different types. In this article, we will learn how to create a stack of tuples in C++. Example 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Reverse a Word Using Stack in C++? In C++, we have a stack data structure that follows the Last In First Out (LIFO) principle. In this article, we will learn how to reverse a word using a stack in C++. Example: Input:word = "GeeksforGeeks"Output:Reversed Word: skeeGrofskeeGReverse a String Using Stack in C++To reverse a word using a 2 min read How to Create a Stack of Pairs in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A pair is a simple container that stores data in a key and value format. In this article, we will learn how to crea 2 min read How to Create a Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read Like