Postfix to Prefix Conversion in Java

Postfix to Prefix conversion in Java:

As we have already discussed about infix ,prefix, and postfix operation .

In this java tutorial we will discuss how to convert an postfix to prefix operation in java.

for example :

  • Input : Postfix : AB+CD-*
  • Output : Prefix : *+AB-CD

Let us see how to this this operation :

Volatile in C++

ALGORITHM:

Move to the given expression from left to right, one at a time

  • If the character is operand do push operation to stack.
  • If the character is operator then do the following 
    1. Pop operand from the stack, say it’s p1.
    2. Pop operand from the stack, say it’s p2.
    3. do operation on p1 and p2 and push in stack.
  • Once the expression end  initialize the result string and pop out from the stack and add it to the result.
  • show the result.
Postfix to prefix conversion 1

Code for Infix to Prefix in java using stack

Run
import java.util.Stack;
public class Main {

    static boolean isOperator(char x){
        switch (x){
            case '-':
            case '+':
            case '/':
            case '*':
            case '^':
                return true;
        }
        return false;
    }

    public static String convert(String exp){

        Stack<String> st = new Stack<>();
        for (int i = 0; i < exp.length() ; i++) {

            char c = exp.charAt(i);

            if(isOperator(c)){
                String s1 = st.pop();
                String s2 = st.pop();
                String temp = c + s2 + s1;
                st.push(temp);
            }else{
                st.push(c+"");
            }
        }
        String result = st.pop();
        return result;
    }

    public static void main(String[] args) {
        String postfix = "AB+CD-*";
        System.out.println("Postfix exp: " + postfix);
        System.out.println("Prefix exp:"  + convert(postfix));
    }
}
output:

postfix exp:

AB+CD-*

prefix exp:

*+AB-CD

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java