Algorithm to convert infix expression to postfix form

The following algorithm transforms the infix expression X into its equivalent postfix expression Y. The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression Y will be constructed from left to right using the operands from X and the operators which are removed from STACK.

We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of X. The algorithm is completed when STACK is empty.

Algorithm

Suppose X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y.

1. push "(" onto stack, and add ")" to the end of X.

2. scan X from left to right and repeat Steps 3 to 6 for each element of X until the STACK is empty :

3. if an operand is encountered, add it to Y.

4. if a left parenthesis is encountered, push it onto STACK.

5. if an operator is encountered, then
(a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) which has the same precedence as or higher precedence than operator.
(b) Add operator to STACK.
/*End of If structure */

6. if a right parenthesis is encountered, then :
(a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to Y].
/* end of If structure */
/* end of Step 2 loop */

7. exit.

This is an Algorithm to Convert Infix expression to Postfix. If you have any questions then comment below.

Summary (सारांश)

Infix expression को postfix form में बदलने का Algorithm कंप्यूटर साइंस के Data Structures और Algorithms विषय का एक बहुत ही महत्वपूर्ण टॉपिक है। इस लेख में हम सीखेंगे कि कैसे किसी गणितीय एक्सप्रेशन (expression) को Stack डेटा स्ट्रक्चर की मदद से इनफिक्स से पोस्टफिक्स नोटेशन में बदला जाता है। इस article/PDF में आपको निम्नलिखित मुख्य बातें सीखने को मिलेंगी:

  • Infix और Postfix expressions के बीच का बुनियादी अंतर क्या है।
  • Stack का उपयोग करके ऑपरेटर्स और ब्रैकेट्स (brackets) को मैनेज करने का तरीका।
  • ऑपरेटर्स की precedence (प्राथमिकता) को ध्यान में रखते हुए उन्हें सही क्रम में व्यवस्थित करना।
  • स्टेप-बाय-स्टेप Algorithm जो आपको कोडिंग या प्रोग्रामिंग में इसे लागू करने में मदद करेगा।
छात्र इस article से आसानी से समझ सकेंगे कि कैसे कंप्यूटर बिना किसी कन्फ्यूजन के कठिन से कठिन mathematical expressions को हल करता है। यदि आप B.Tech, BCA, MCA या किसी भी Computer Science कोर्स की परीक्षा की तैयारी कर रहे हैं, तो यह टॉपिक Exam preparation के लिए बेहद जरूरी है। परीक्षा में अक्सर इस Algorithm पर आधारित conversion के Numerical प्रश्न पूछे जाते हैं, जिन्हें आप इस लेख की मदद से चुटकियों में हल करना सीख जाएंगे।

1 thought on “Algorithm to convert infix expression to postfix form”

Leave a Comment