Hi, i need to program this algorthim using 2 stack in java, for example to evaluate such a given valid infix expression : (5*3)+(4/(6-2)).
i need help, i don't know much about stacks..
Create two empty stacks, one for operands, and one for operators.
Read infix expression as a string ;
while(there are more tokens in the string){
if(current token is an operand)
push token in the operand stack;
else if(current token is an operator){
push token in the operator stack;
else if (current token is a left parentheses)
ignore
else if (current token is a right parentheses)
{
operand1 = pop form operand stack;
operand2 = pop from operand stack;
operator = pop from operator stack
result = operand2 operator operand1;
push result in the operand stack;
}
while(there are more operators in the operator stack){
{
operand1 = pop form operand stack;
operand2 = pop from operand stack;
operator = pop from operator stack
result = operand2 operator operand1;
push result in the operand stack;
}
pop result from the operand stack and print it;