Hello i need help with the popping of stacks.
i want to pop them all once i go into the Clear() method, but im having problems with the codings. here's my code:
import java.util.Stack;
class InfixPost
{
static Stack inputStack;
static String output = "";
public String infixToPostfix(String input) {
inputStack = new Stack();
for (int i = 0; i < input.length(); i++) {
char current = input.charAt(i);
if (current == '+' || current == '-') {
isOperator(current, 1);
} else if (current == '*' || current == '/') {
isOperator(current, 2);
} else {
output += current;
}
}
while(!inputStack.isEmpty()){
char top = (Character) inputStack.pop();
output += top;
}
return output;
}
public static void isOperator(char c, int prec) {
while (!inputStack.isEmpty()) {
char top = (Character) inputStack.pop();
int topPrec = 0;
if (top == '+' || top == '-') {
topPrec = 1;
} else {
topPrec = 2;
}
if (topPrec >= prec) {
output += top;
}else{
inputStack.push(top);
break;
}
}
inputStack.push(c);
}
public void Clear(){
while(!inputStack.isEmpty()){
inputStack.pop();
}
}
}
how can i pop all of the stacks contents? im having problems with my Clear() method.
any advices will be gladly accepted. thanks in advance.