In the first step itself of converting an infix to prefix can someone explain in simple terms why should we reverse the string? Is there any alternative method to convert?
相关问题
- Ada beginner Stack program
- Parsing a Chemistry Formula in Python
- Prolog Tree Traversal
- pass by reference in assembly
- Assembler Stack Alignment (or better misaligned ex
相关文章
- Threading in C# , value types and reference types
- Stack<> implementation in C#
- Java, Printing the stack values
- Is there a stack space for every thread?
- Use domain as “prefix” on CakePHP
- Good Infix to Prefix implementation in Python that
- How we access stack variables without poping them?
- Do you know any opensource JQuery dropdown menu fo
Yes, you are absolutely right that if you have to convert infix to prefix then you have to scan the string from right to left.
Why not from left to right?
If you scan from left to right then you will require future knowledge of operators in the string.
Example 1 :
Now, when you convert it from left to right then you should have the knowledge of + that is yet to appear in the string. This looks simple in this particular example, now consider another example given below.
Example 2:
Now, if you scan from left to right then when you scan 0th index of string then the program should have knowledge of - which is going to appear in 7th index of string which could be a hectic job.
So the safest way to do is to scan the string from right to left.