Skip to content

Instantly share code, notes, and snippets.

@solomkinmv
Created September 6, 2015 16:05
Show Gist options
  • Select an option

  • Save solomkinmv/e5eb0dcd63dbc4c3d1c8 to your computer and use it in GitHub Desktop.

Select an option

Save solomkinmv/e5eb0dcd63dbc4c3d1c8 to your computer and use it in GitHub Desktop.
static String InfixToPrefix(String infix) {
char[] array = infix.toCharArray();
LinkedList<Character> operatorsList = new LinkedList<Character>();
StringBuffer result = new StringBuffer();
for (int i = array.length - 1; i >= 0; i--) {
char ch = array[i];
if (ch > '0' && ch < '9') {
result.insert(0, ch);
} else if (ch != '(' && ch != ')') {
operatorsList.add(ch);
} else if (ch == '(') {
result.insert(0, operatorsList.removeLast());
}
}
return result.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment