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 :
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
- Pop operand from the stack, say it’s p1.
- Pop operand from the stack, say it’s p2.
- 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.
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
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
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- 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)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- 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
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java
Login/Signup to comment