Prefix to Postfix Conversion

Prefix to postfix conversion” is a classic example of  stack data structure. Stacks 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.

Steps to convert prefix to postfix expression

  1. Scan the prefix string in reverse order.
  2. Initialize an empty string stack.
  3. If the scanned character is operand, push it into stack.
  4. Else if, the scanned character is operator, pop two strings from the stack, namely, temp1 and temp2, then push

temp1 temp2 operator  into stack.

  1. Repeat steps from 3 to 4 until all the characters of the strings are scanned.
  2. 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 converting prefix expression to postfix expression

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 converting prefix expression to postfix expression

#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:

  1. Infix to Postfix Conversion
  2. Infix to Prefix Conversion
  3. Prefix to Infix Conversion
  4. Postfix to Infix Conversion
  5. Postfix to Prefix Conversion
  6. Check whether given Parentheses String are Balanced Parentheses or Not.
  7. Next Greater Element
  8. Find Minimum number of bracket reversals required to make an expression balanced.
  9. Implement Queue Using Two Stacks.
  10. Merge Overlapping Intervals using Stacks
  11. Implement Stack Using Linked List
  12. Largest Rectangular Area in Histogram
  13. Length of Longest Valid Substring
  14. Reverse a String using Stack
  15. Implement two stacks in a single array
  16. Print Bracket Number
  17. Next Greater Frequency Element
  18. Sort a Stack using Temporary Stack
  19. Program to check Anagram Strings.
  20. Program to check whether two Strings are Rotation of each other or not.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *