
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Prefix to Postfix Conversion in C++
In this problem, we are given a prefix expression. Our task is to print the postfix conversion of the given expression.
Prefix expression is those expressions which have operators before the operands.
Example: +AB.
Postfix expressions are those expressions which have operators after operands in the expressions.
Example: AB/
The conversion of prefix to postfix should not involve the conversion to infix.
Let’s take an example to understand the problem,
Input: /+XY+NM Output: XY+NM+/ Explanation: infix -> (X+Y)/(N+M)
To solve this problem, we will first traverse the whole postfix expression in an reverse order. And we will be using the stack data structure for our processing. And do the following for cases of elements found of traversal
Case: if the symbol is operand -> push(element) in stack.
Case: if symbol is operator -> 2*pop(element) from stack. And then push sequence of operand - operand - operator.
Program to show the implementation of our algorithm
Example
#include <iostream> #include <stack> using namespace std; bool isOperator(char x) { switch (x) { case '+': case '-': case '/': case '*': return true; } return false; } string convertToPostfix(string prefix) { stack<string> expression; int length = prefix.size(); for (int i = length - 1; i >= 0; i--) { if (isOperator(prefix[i])) { string op1 = expression.top(); expression.pop(); string op2 = expression.top(); expression.pop(); string temp = op1 + op2 + prefix[i]; expression.push(temp); } else expression.push(string(1, prefix[i])); } return expression.top(); } int main() { string prefix = "*-AB/+CD*XY"; cout<<"Prefix expression : "<<prefix<<endl; cout<<"Postfix expression : "<<convertToPostfix(prefix); return 0; }
Output
Prefix expression : *-AB/+CD*XY Postfix expression : AB-CD+XY*/*
- Related Articles
- Prefix and Postfix Expressions in Data Structure
- Prefix to Infix Conversion in C++
- Precedence of postfix ++ and prefix ++ in C/C++
- Difference between prefix and postfix operators in C#?
- What is the difference between prefix and postfix operators in C++?
- Differentiate between the prefix and postfix forms of the ++ operator in java?
- Postfix to Infix in C++
- Convert Infix to Postfix Expression
- Program to evaluate Postfix Notation in C++
- Evaluate Postfix Expression
- How to create infix to postfix converter in JavaScript?
- What are postfix operators in C++?
- What are postfix operators in C#?
- What is Postfix Notation?
- Learn How to Setup Email Services using Postfix in Linux
