Write the algorithm for converting infix expression to postfix expression?



Write the algorithm for converting infix expression to postfix expression?..

Answer / Sumeet Bhandari

1. Create a stack and an empty output string.
2. Traverse the input infix expression from left to right (inorder).
3. If the current character is an operand, append it to the output string.
4. If the current character is an operator, pop two operands from the stack and append the operator along with them in the reverse order to the output string. Perform the operation if necessary.
5. If the current character is a left parenthesis '(', push it onto the stack.
6. If the current character is a right parenthesis ')', pop operators from the stack and append them to the output string until a matching left parenthesis is found.
7. After traversing the entire infix expression, all remaining operands in the stack are popped and appended to the output string.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Data Structures Interview Questions

Can treemap have null values?

1 Answers  


Explain the most efficient method to reverse a linked list?

1 Answers  


What is Another name of Dynamically allocating memory.

1 Answers  


What is sorting problem?

1 Answers  


What are the types of linked list?

1 Answers  


Which is better hashset or treeset?

1 Answers  


Can we null keys in treemap?

1 Answers  


What are the four types of variables?

1 Answers  


What is the use of data structure in real life?

1 Answers  


For which header list, the last node contains the null pointer?

1 Answers  


Write a recursive c function to calculate the height of a binary tree.

1 Answers  


Is hashtable throw concurrentmodificationexception?

1 Answers  


Categories