In: Computer Science
Introduced to data structure using java
2)One of the applications of stack is to convert infix expressions to postfix. Given the following infix expression, use the stack method to convert it to postfix. Show your work.
x - y / (a + b) + z
3)write a generic method to find the maximum of three variables of generic type (i.e. type T).
Note that you must use the compareTo method to compare the values. Also, the code must return one of the values. Therefore the return type must be T.
import java.util.*;
public class DataStructuresEg
{
//Method to convert infix expression to postfix
public static void infixToPostfix(String infix)
{
//Stack stk to store values
Stack<Character> stk=new Stack<Character>();
//String to store postfix expression
String postfix="";
//initializing variable i
int i=0;
//Loop to access each character in input expression
while(i<infix.length())
{
//if the input character is a letter or a digit
if(Character.isLetter(infix.charAt(i))||Character.isDigit(infix.charAt(i)))
{
//add the input character to string postfix
postfix+=" "+infix.charAt(i);
}
//else if the input character is an ')'
else if(infix.charAt(i)=='(')
{
//push the input character into stack
stk.push('(');
}
//else if the input character is ')'
else if(infix.charAt(i)==')')
{
//Repeat while stack is not empty and stack of top is "("
while(stk.peek()!='(' && !stk.isEmpty())
{
//Delete the top element in stack and add to postfix
postfix+=" "+stk.pop();
}
//if top element in stack is'('
if(stk.peek()=='(')
//Delete the top element in stack
stk.pop();
}
//else if the input character is blankspace do nothing
else if(infix.charAt(i)==' ')
{}
//Else if the element is an operator
else
{
//Repeat while stack is not empty and
//precedence of stack of top is greaater thanor equalto precedence of input
while( !stk.isEmpty()&&(prec(infix.charAt(i))<=prec(stk.peek())))
//Delete the top element in stack and add to postfix
postfix+=" "+stk.pop();
//push the input character to stack
stk.push(infix.charAt(i));
}
//increase i
i++;
}
//Repeat while stack is not empty
while(!stk.isEmpty())
//Pop elements in stack and add to postfix
postfix+=" "+stk.pop();
System.out.println("Postfix Expression is");
//Print the postfix expression
System.out.println(postfix);
}
//Function to find precedence of operator
static int prec(char op)
{
int p=0;
switch(op)
{
//if input is ^ precedence value is 3
case '^':p=3;
break;
//if input is * or / or % precedence value is 2
case '*':p=2;
break;
case '/':
case '%': p=2;
break;
//if input is + or - precedence value is 1
case '+':p=1;
break;
case '-':
p=1;
break;
}
return p;
}
//Generic function tofind maximum of three
public static <T extends Comparable> void maxOfThree(T a,T b,T c)
{
//Declaring generic variable max tostore mximum value
T max;
//if a is greater than b and c
if(a.compareTo(b)>=0 && a.compareTo(b)>=0)
max=a;
//if b is greater than a and c
else if(b.compareTo(c)>=0)
max=b;
else
max=c;
System.out.println("Maximum of three is: "+max);
}
public static void main(String[] args)
{
//calling function to find maximum of three inputs of different types
maxOfThree(3,36, 8);
maxOfThree(39.7,36.9, 0.8);
maxOfThree('a','d','z');
System.out.println("Enter infix expreession:");
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
//calling function to find postfix expression
infixToPostfix(input);
}
}
Sample output