{"id":26843,"date":"2017-12-24T15:40:34","date_gmt":"2017-12-24T10:10:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26843"},"modified":"2017-12-24T15:42:54","modified_gmt":"2017-12-24T10:12:54","slug":"c-algorithm-evaluation-postfix-expression","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-evaluation-postfix-expression\/","title":{"rendered":"C Algorithm &#8211; Evaluation of Postfix Expression"},"content":{"rendered":"<p>The Postfix notation is used to represent algebraic expressions. The expressions written in postfix form are evaluated faster compared to <span id=\"more-142800\"><\/span>infix notation as parenthesis are not required in postfix. We have discussed infix to postfix conversion. In this post, evaluation of postfix expressions is discussed.<\/p>\n<p>Following is algorithm for evaluation postfix expressions.<br \/>\n1) Create a stack to store operands (or values).<br \/>\n2) Scan the given expression and do following for every scanned element.<br \/>\n\u2026..a) If the element is a number, push it into the stack<br \/>\n\u2026..b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack<br \/>\n3) When the expression is ended, the number in the stack is the final answer<\/p>\n<p><strong>Example:<\/strong><br \/>\nLet the given expression be \u201c2 3 1 * + 9 -\u201c. We scan all elements one by one.<br \/>\n1) Scan \u20182\u2019, it\u2019s a number, so push it to stack. Stack contains \u20182\u2019<br \/>\n2) Scan \u20183\u2019, again a number, push it to stack, stack now contains \u20182 3\u2019 (from bottom to top)<br \/>\n3) Scan \u20181\u2019, again a number, push it to stack, stack now contains \u20182 3 1\u2019<br \/>\n4) Scan \u2018*\u2019, it\u2019s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result \u20183\u2019 to stack. Stack now becomes \u20182 3\u2019.<br \/>\n5) Scan \u2018+\u2019, it\u2019s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result \u20185\u2019 to stack. Stack now becomes \u20185\u2019.<br \/>\n6) Scan \u20189\u2019, it\u2019s a number, we push it to the stack. Stack now becomes \u20185 9\u2019.<br \/>\n7) Scan \u2018-\u2018, it\u2019s an operator, pop two operands from stack, apply the \u2013 operator on operands, we get 5 \u2013 9 which results in -4. We push the result \u2018-4\u2019 to stack. Stack now becomes \u2018-4\u2019.<br \/>\n8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C implementation of 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\">\/\/ C program to evaluate value of a postfix expression<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;ctype.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) return NULL;<br\/> <br\/>    stack-&gt;top = -1;<br\/>    stack-&gt;capacity = capacity;<br\/>    stack-&gt;array = (int*) malloc(stack-&gt;capacity * sizeof(int));<br\/> <br\/>    if (!stack-&gt;array) return NULL;<br\/> <br\/>    return stack;<br\/>}<br\/> <br\/>int isEmpty(struct Stack* stack)<br\/>{<br\/>    return stack-&gt;top == -1 ;<br\/>}<br\/> <br\/>char peek(struct Stack* stack)<br\/>{<br\/>    return stack-&gt;array[stack-&gt;top];<br\/>}<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\/> <br\/>void push(struct Stack* stack, char op)<br\/>{<br\/>    stack-&gt;array[++stack-&gt;top] = op;<br\/>}<br\/> <br\/> <br\/>\/\/ The main function that returns value of a given postfix expression<br\/>int evaluatePostfix(char* exp)<br\/>{<br\/>    \/\/ Create a stack of capacity equal to expression size<br\/>    struct Stack* stack = createStack(strlen(exp));<br\/>    int i;<br\/> <br\/>    \/\/ See if stack was created successfully<br\/>    if (!stack) return -1;<br\/> <br\/>    \/\/ Scan all characters one by one<br\/>    for (i = 0; exp[i]; ++i)<br\/>    {<br\/>        \/\/ If the scanned character is an operand (number here),<br\/>        \/\/ push it to the stack.<br\/>        if (isdigit(exp[i]))<br\/>            push(stack, exp[i] - &#039;0&#039;);<br\/> <br\/>        \/\/  If the scanned character is an operator, pop two<br\/>        \/\/ elements from stack apply the operator<br\/>        else<br\/>        {<br\/>            int val1 = pop(stack);<br\/>            int val2 = pop(stack);<br\/>            switch (exp[i])<br\/>            {<br\/>             case &#039;+&#039;: push(stack, val2 + val1); break;<br\/>             case &#039;-&#039;: push(stack, val2 - val1); break;<br\/>             case &#039;*&#039;: push(stack, val2 * val1); break;<br\/>             case &#039;\/&#039;: push(stack, val2\/val1);   break;<br\/>            }<br\/>        }<br\/>    }<br\/>    return pop(stack);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    char exp[] = &quot;231*+9-&quot;;<br\/>    printf (&quot;Value of %s is %d&quot;, exp, evaluatePostfix(exp));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Value of 231*+9- is -4<\/pre>\n<p>Time complexity of evaluation algorithm is O(n) where n is number of characters in input expression.<\/p>\n<p>There are following limitations of above implementation.<br \/>\n1) It supports only 4 binary operators \u2018+\u2019, \u2018*\u2019, \u2018-\u2018 and \u2018\/\u2019. It can be extended for more operators by adding more switch cases.<br \/>\n2) The allowed operands are only single digit operands. The program can be extended for multiple digits by adding a separator like space between all elements (operators and operands) of given expression.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Evaluation of Postfix Expression &#8211; Data Structure -The Postfix notation is used to represent algebraic expressions.<\/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,80041,80042,80044,80046,80045,80038,80039,80040,80043],"class_list":["post-26843","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-evaluation-of-prefix-expression","tag-c-program-to-convert-infix-to-postfix","tag-c-program-to-evaluate-infix-expression","tag-c-program-to-evaluate-postfix-expression-with-output","tag-evaluation-of-postfix-expression-using-stack-algorithm","tag-evaluation-of-postfix-expression-using-stack-examples","tag-evaluation-of-postfix-expression-using-stack-in-c","tag-evaluation-of-postfix-expression-using-stack-in-data-structure","tag-program-to-evaluate-postfix-expression-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26843","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=26843"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26843\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}