Stacks1 Ass1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

public class Solution {

public int evalRPN(String[] A) {


Stack<Integer> stack = new Stack<Integer>();

for(int i=0;i<A.length;i++)
{
if(A[i].equals("+") || A[i].equals("-") || A[i].equals("*") ||
A[i].equals("/"))
{
int x = stack.peek();
stack.pop();
int y = stack.peek();
stack.pop();
int temp = 0;
if(A[i].equals("+"))
{
temp = y+x;
}
else if(A[i].equals("-"))
{
temp = y-x;
}
else if(A[i].equals("*"))
{
temp = y*x;
}
else
{
temp = y/x;
}
stack.push(temp);
}
else
{
stack.push(Integer.parseInt(A[i]));
}
}
return stack.peek();
}
}

You might also like