Prefix to Postfix Conversion
“Prefix to postfix conversion” is a classic example of stack data structure. Stack can be used to convert given prefix expression to corresponding postfix expression.
Operator: Operator are symbols that instruct the computer to perform simple and single tasks. Examples of operators includes + (Addition), – (Subtraction), * (multiplication),… and many more.
Operand: Operands are values or variable on which operator works its tasks. Examples of Operand includes “a”, “b”, 23, 12,.. and many more.
The steps required for Prefix to Postfix Conversion are as follows:
- Scan the prefix string in reverse order.
- Initialize an empty string stack.
- If the scanned character is operand, push it into stack.
- Else if, the scanned character is operator, pop two strings from the stack, namely, temp1 and temp2, then push
temp1 temp2 operator into stack.
- Repeat steps from 3 to 4 until all the characters of the strings are scanned.
- Lastly, single postfix string will be left in stack, pop it and return it.
Example
Suppose if we want to convert the following prefix expression to postfix:
Algorithm for Prefix to Postfix Conversion
Algo prefix_2_postfix (prefix)
{ // input- valid prefix expression
//output- equivalent postfix expression
1. Createstack (stack).
2. i=sizeof(prefix)-1.
3. loop(i>=0)
{
a.if prefix[i] is an operator
{
operand2=popstack().
operand1=popstack().
temp= concatenate operand1 + operand2 + prefix[i].
pushstack (temp).
}
b.else if prefix[i] is an operand
Then push prefix[i] into stack.
c.Decrement i with 1.
}
4. postfix= popstack().
5. Return postfix.
}// END OF ALGO.
C++ Program for Prefix to Postfix Conversion
/* Program for Prefix to Postfix Conversion */
#include <iostream>
#include <string>
#define sizes 100
using namespace std;
class stack
{
string item[sizes];
int top;
public:
stack()
{
top=-1;
}
void push(string str)
{
if(top==sizes-1)
{
cout<<"stack overflow!!";
return;
}
top++;
item[top]=str;
}
string pop()
{
if(top==-1)
{
cout<<"stack underflow!!";
return "abc";
}
string temp;
temp=item[top];
top--;
return temp;
}
};
int main(int argc, char** argv)
{
int i;
string prefix;
stack st;
cout<<"Enter valid prefix expression: ";
cin>>prefix;
for(i=prefix.size()-1;i>=0;i--)
{
if(prefix[i]=='+' || prefix[i]=='-' || prefix[i]=='*' || prefix[i]=='/' || prefix[i]=='^')
{
string op1,op2,temp;
op1=st.pop();
op2=st.pop();
temp=op1+op2+prefix[i];
st.push(temp);
}
else
{
string flag;
flag=flag+prefix[i];
st.push(flag);
}
}
cout<<"The equivalent postfix expression: "<<st.pop();
return 0;
}
OUTPUT: Enter valid prefix expression: +a/*bc-de The equivalent Postfix expression: abc*de-/+
Related Posts:
- Prefix to Infix Conversion
- Infix to Postfix Conversion
- Infix to Prefix Conversion
- Postfix to Infix Conversion
- Postfix to Prefix Conversion
- Check whether given Parentheses String are Balanced Parentheses or Not.
- Next Greater Element
- Delete Alternate Nodes of the Linked List.
- Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
- Find Minimum number of bracket reversals required to make an expression balanced.
- Implement Queue Using Two Stacks.
- Merge Overlapping Intervals using Stacks
- Implement Stack Using Linked List
- Largest Rectangular Area in Histogram
- Length of Longest Valid Substring
- Reverse a String using Stack
- Implement two stacks in a single array
- Print Bracket Number
- Next Greater Frequency Element
- Sort a Stack using Temporary Stack