{"id":26800,"date":"2017-12-24T15:19:58","date_gmt":"2017-12-24T09:49:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26800"},"modified":"2017-12-24T15:19:58","modified_gmt":"2017-12-24T09:49:58","slug":"c-algorithm-infix-postfix-conversion-using-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-infix-postfix-conversion-using-stack\/","title":{"rendered":"C Algorithm &#8211; Infix to Postfix Conversion using Stack"},"content":{"rendered":"<p><strong>Infix expression:<\/strong>The expression of the form a op b. When an operator is in-between every pair of operands.<span id=\"more-142798\"><\/span><\/p>\n<p><strong>Postfix expression:<\/strong>The expression of the form a b op. When an operator is followed for every pair of operands.<\/p>\n<p><strong>Why postfix representation of the expression?<\/strong><br \/>\nThe compiler scans the expression either from left to right or from right to left.<\/p>\n<p>Consider the below expression: a op1 b op2 c op3 d<br \/>\nIf op1 = +, op2 = *, op3 = +<\/p>\n<p>The compiler first scans the expression to evaluate the expression b * c, then again scan the expression to add a to it. The result is then added to d after another scan.<\/p>\n<p>The repeated scanning makes it very in-efficient. It is better to convert the expression to postfix(or prefix) form before evaluation.<\/p>\n<p>The corresponding expression in postfix form is: abc*+d+. The postfix expressions can be evaluated easily using a stack. We will cover postfix expression evaluation in a separate post.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Algorithm<\/strong><br \/>\n<strong>1.<\/strong> Scan the infix expression from left to right.<br \/>\n<strong>2.<\/strong> If the scanned character is an operand, output it.<br \/>\n<strong>3. <\/strong>Else,<br \/>\n\u2026..<strong>3.1<\/strong> If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty), push it.<br \/>\n\u2026..<strong>3.2<\/strong> Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. Push the scanned operator to the stack.<br \/>\n<strong>4.<\/strong> If the scanned character is an \u2018(\u2018, push it to the stack.<br \/>\n<strong>5.<\/strong> If the scanned character is an \u2018)\u2019, pop and output from the stack until an \u2018(\u2018 is encountered.<br \/>\n<strong>6.<\/strong> Repeat steps 2-6 until infix expression is scanned.<br \/>\n<strong>7. <\/strong>Pop and output from the stack until it is not empty.<\/p>\n<p>Following is C implementation of the above algorithm<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/> <br\/>\/\/ Stack type<br\/>struct Stack<br\/>{<br\/>    int top;<br\/>    unsigned capacity;<br\/>    int* array;<br\/>};<br\/> <br\/>\/\/ Stack Operations<br\/>struct Stack* createStack( unsigned capacity )<br\/>{<br\/>    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));<br\/> <br\/>    if (!stack) <br\/>        return NULL;<br\/> <br\/>    stack-&gt;top = -1;<br\/>    stack-&gt;capacity = capacity;<br\/> <br\/>    stack-&gt;array = (int*) malloc(stack-&gt;capacity * sizeof(int));<br\/> <br\/>    if (!stack-&gt;array)<br\/>        return NULL;<br\/>    return stack;<br\/>}<br\/>int isEmpty(struct Stack* stack)<br\/>{<br\/>    return stack-&gt;top == -1 ;<br\/>}<br\/>char peek(struct Stack* stack)<br\/>{<br\/>    return stack-&gt;array[stack-&gt;top];<br\/>}<br\/>char pop(struct Stack* stack)<br\/>{<br\/>    if (!isEmpty(stack))<br\/>        return stack-&gt;array[stack-&gt;top--] ;<br\/>    return &#039;$&#039;;<br\/>}<br\/>void push(struct Stack* stack, char op)<br\/>{<br\/>    stack-&gt;array[++stack-&gt;top] = op;<br\/>}<br\/> <br\/> <br\/>\/\/ A utility function to check if the given character is operand<br\/>int isOperand(char ch)<br\/>{<br\/>    return (ch &gt;= &#039;a&#039; &amp;&amp; ch &lt;= &#039;z&#039;) || (ch &gt;= &#039;A&#039; &amp;&amp; ch &lt;= &#039;Z&#039;);<br\/>}<br\/> <br\/>\/\/ A utility function to return precedence of a given operator<br\/>\/\/ Higher returned value means higher precedence<br\/>int Prec(char ch)<br\/>{<br\/>    switch (ch)<br\/>    {<br\/>    case &#039;+&#039;:<br\/>    case &#039;-&#039;:<br\/>        return 1;<br\/> <br\/>    case &#039;*&#039;:<br\/>    case &#039;\/&#039;:<br\/>        return 2;<br\/> <br\/>    case &#039;^&#039;:<br\/>        return 3;<br\/>    }<br\/>    return -1;<br\/>}<br\/> <br\/> <br\/>\/\/ The main function that converts given infix expression<br\/>\/\/ to postfix expression. <br\/>int infixToPostfix(char* exp)<br\/>{<br\/>    int i, k;<br\/> <br\/>    \/\/ Create a stack of capacity equal to expression size <br\/>    struct Stack* stack = createStack(strlen(exp));<br\/>    if(!stack) \/\/ See if stack was created successfully <br\/>        return -1 ;<br\/> <br\/>    for (i = 0, k = -1; exp[i]; ++i)<br\/>    {<br\/>        \/\/ If the scanned character is an operand, add it to output.<br\/>        if (isOperand(exp[i]))<br\/>            exp[++k] = exp[i];<br\/>         <br\/>        \/\/ If the scanned character is an \u2018(\u2018, push it to the stack.<br\/>        else if (exp[i] == &#039;(&#039;)<br\/>            push(stack, exp[i]);<br\/>         <br\/>        \/\/ If the scanned character is an \u2018)\u2019, pop and output from the stack <br\/>        \/\/ until an \u2018(\u2018 is encountered.<br\/>        else if (exp[i] == &#039;)&#039;)<br\/>        {<br\/>            while (!isEmpty(stack) &amp;&amp; peek(stack) != &#039;(&#039;)<br\/>                exp[++k] = pop(stack);<br\/>            if (!isEmpty(stack) &amp;&amp; peek(stack) != &#039;(&#039;)<br\/>                return -1; \/\/ invalid expression             <br\/>            else<br\/>                pop(stack);<br\/>        }<br\/>        else \/\/ an operator is encountered<br\/>        {<br\/>            while (!isEmpty(stack) &amp;&amp; Prec(exp[i]) &lt;= Prec(peek(stack)))<br\/>                exp[++k] = pop(stack);<br\/>            push(stack, exp[i]);<br\/>        }<br\/> <br\/>    }<br\/> <br\/>    \/\/ pop all the operators from the stack<br\/>    while (!isEmpty(stack))<br\/>        exp[++k] = pop(stack );<br\/> <br\/>    exp[++k] = &#039;\\0&#039;;<br\/>    printf( &quot;%s\\n&quot;, exp );<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    char exp[] = &quot;a+b*(c^d-e)^(f+g*h)-i&quot;;<br\/>    infixToPostfix(exp);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>abcd^e-fgh*+^*+i-<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Infix to Postfix Conversion using Stack &#8211; Data Structure &#8211; Infix expression:The expression of the form a op b. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,79607],"tags":[79896,79892,79902,79899,79895,79894,79903,79898,79891,79904,79897,79901,79893,79890,79900,79905],"class_list":["post-26800","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-stack","tag-algorithm-for-evaluation-of-postfix-expression","tag-algorithm-for-infix-to-postfix-conversion-using-stack-in-c","tag-c-program-to-convert-infix-to-postfix-and-evaluate-using-stack","tag-c-program-to-evaluate-postfix-expression","tag-infix-to-postfix-algorithm-geeksforgeeks","tag-infix-to-postfix-algorithm-java","tag-infix-to-postfix-conversion-algorithm","tag-infix-to-postfix-conversion-in-c-using-stack-examples","tag-infix-to-postfix-conversion-using-stack-in-c","tag-infix-to-postfix-conversion-using-stack-in-c-with-output","tag-infix-to-postfix-conversion-using-stack-in-java","tag-infix-to-postfix-program-in-c","tag-infix-to-postfix-using-stack-c-code","tag-infix-to-prefix-algorithm","tag-infix-to-prefix-program-in-c","tag-isalnum-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26800"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26800\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}