Created
September 6, 2015 16:05
-
-
Save solomkinmv/e5eb0dcd63dbc4c3d1c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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