Infix To Postfix Conversion
import java.util.Stack;
public class InfixToPostfix {
public static void main(String[] args){
String infix = "(A+B)*(C-D)";
String postfix = convertInfixToPostfix(infix);
System.out.println(postfix);
}
public static int precedence(char operator){
switch(operator){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}
public static String convertInfixToPostfix(String infix){
StringBuilder postfix = new StringBuilder();
Stack<Character> st = new Stack<>();
for(char ch : infix.toCharArray()){
if(Character.isLetterOrDigit(ch)){
postfix.append(ch);
}
else if(ch == '('){
st.push(ch);
}
else if(ch == ')'){
while(!st.isEmpty() && st.peek() != '('){
postfix.append(st.pop());
}
if(!st.isEmpty() && st.peek() == '('){
st.pop();
}
}
else if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'){
while(!st.isEmpty() && precedence(ch) <= precedence(st.pop())){
postfix.append(st.pop());
}
st.push(ch);
}
}
while(!st.isEmpty()){
postfix.append(st.pop());
}
return postfix.toString();
}
}
Infix To Prefix Conversion
import java.util.Stack;
public class InfixToPrefix{
public static void main(String[] args){
String infix = "(A+B)*(C-D)";
String prefix = convertInfixToPrefix(infix);
System.out.println(prefix);
}
public static int precedence(char operator){
switch(operator){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}
public static String convertInfixToPrefix(String infix){
StringBuilder reverse = new StringBuilder(infix);
String reverseInfix = reverse.reverse().toString();
reverseInfix = reverseInfix.replace('(', '#').replace(')', '(').replace('#', ')');
StringBuilder prefix = new StringBuilder();
Stack<Character> st = new Stack<>();
for(char ch : reverseInfix.toCharArray()){
if(Character.isLetterOrDigit(ch)){
prefix.append(ch);
}
else if(ch == '('){
st.push(ch);
}
else if(ch == ')'){
while(!st.isEmpty() && st.peek() != '('){
prefix.append(st.pop());
}
if(!st.isEmpty() && st.peek() == '('){
st.pop();
}
}
else if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'){
while(!st.isEmpty() && precedence(ch) <= precedence(st.peek())){
prefix.append(st.pop());
}
st.push(ch);
}
}
while(!st.isEmpty()){
prefix.append(st.pop());
}
return prefix.reverse().toString();
}
}
Postfix To Prefix Conversion
import java.util.Stack;
public class PostfixToPrefix{
public static void main(String[] args){
String postfix = "AB+CD-*";
String prefix = convertPostfixToPrefix(postfix);
System.out.println(prefix);
}
public static boolean isOperator(char ch){
return ch == '/' || ch == '*' || ch == '-' || ch == '+' || ch == '^';
}
public static String convertPostfixToPrefix(String postfix){
Stack<String> st = new Stack<>();
for(char ch : postfix.toCharArray()){
if(!isOperator(ch)){
st.push(ch + "");
}
else {
String operand2 = st.pop();
String operand1 = st.pop();
String prefix = ch + operand1 + operand2;
st.push(prefix);
}
}
return st.pop();
}
}
Postfix To Infix Conversion
import java.util.Stack;
public class PostfixToInfix{
public static void main(String[] args){
String postfix = "AB+CD-*";
String infix = convertPostfixToInfix(postfix);
System.out.println(infix);
}
public static boolean isOperator(char ch){
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
public static String convertPostfixToInfix(String postfix){
Stack<String> st = new Stack<>();
for(char ch : postfix.toCharArray()){
if(!isOperator(ch)){
st.push(ch + "");
}
else {
String operand2 = st.pop();
String operand1 = st.pop();
String infix = "(" + operand1 + ch + operand2 + ")";
st.push(infix);
}
}
return st.pop();
}
}
Prefix To Infix Conversion
import java.util.Stack;
public class PrefixToInfix{
public static void main(String[] args){
String prefix = "*+AB-CD";
String infix = convertPrefixToInfix(prefix);
System.out.println(infix);
}
public static boolean isOperator(char ch){
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
public static String reverse(String prefix){
StringBuilder reverseWord = new StringBuilder(prefix);
return reverseWord.reverse().toString();
}
public static String convertPrefixToInfix(String prefix){
Stack<String> st = new Stack<>();
String reversePrefix = reverse(prefix);
for(char ch : reversePrefix.toCharArray()){
if(!isOperator(ch)){
st.push(ch + "");
}
else{
String operand1 = st.pop();
String operand2 = st.pop();
String infix = "(" + operand1 + ch + operand2 + ")";
st.push(infix);
}
}
return st.pop();
}
}
Prefix to Postfix Conversion
import java.util.Stack;
public class PrefixToPostfix{
public static void main(String[] args){
String prefix = "*+AB-CD";
String postfix = convertPrefixToPostfix(prefix);
System.out.println(postfix);
}
public static boolean isOPerator(char ch){
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
public static String reverse(String prefix){
StringBuilder reverseWord = new StringBuilder(prefix);
return reverseWord.reverse().toString();
}
public static String convertPrefixToPostfix(String prefix){
Stack<String> st = new Stack<>();
String reversePrefix = reverse(prefix);
for(char ch : reversePrefix.toCharArray()){
if(!isOperator(ch)){
st.psuh(ch);
}
else{
String operand1 = st.pop();
String operand2 = st.pop();
String postfix = operand1 + operand2 + ch;
st.push(postfix);
}
}
return st.pop();
}
}