{"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=\u201dbanner\u201d]\n<p>Following is C implementation of above algorithm.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20evaluate%20value%20of%20a%20postfix%20expression%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%23include%20%3Cctype.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20Stack%20type%0Astruct%20Stack%0A%7B%0A%20%20%20%20int%20top%3B%0A%20%20%20%20unsigned%20capacity%3B%0A%20%20%20%20int*%20array%3B%0A%7D%3B%0A%20%0A%2F%2F%20Stack%20Operations%0Astruct%20Stack*%20createStack(%20unsigned%20capacity%20)%0A%7B%0A%20%20%20%20struct%20Stack*%20stack%20%3D%20(struct%20Stack*)%20malloc(sizeof(struct%20Stack))%3B%0A%20%0A%20%20%20%20if%20(!stack)%20return%20NULL%3B%0A%20%0A%20%20%20%20stack-%3Etop%20%3D%20-1%3B%0A%20%20%20%20stack-%3Ecapacity%20%3D%20capacity%3B%0A%20%20%20%20stack-%3Earray%20%3D%20(int*)%20malloc(stack-%3Ecapacity%20*%20sizeof(int))%3B%0A%20%0A%20%20%20%20if%20(!stack-%3Earray)%20return%20NULL%3B%0A%20%0A%20%20%20%20return%20stack%3B%0A%7D%0A%20%0Aint%20isEmpty(struct%20Stack*%20stack)%0A%7B%0A%20%20%20%20return%20stack-%3Etop%20%3D%3D%20-1%20%3B%0A%7D%0A%20%0Achar%20peek(struct%20Stack*%20stack)%0A%7B%0A%20%20%20%20return%20stack-%3Earray%5Bstack-%3Etop%5D%3B%0A%7D%0A%20%0Achar%20pop(struct%20Stack*%20stack)%0A%7B%0A%20%20%20%20if%20(!isEmpty(stack))%0A%20%20%20%20%20%20%20%20return%20stack-%3Earray%5Bstack-%3Etop\u2013%5D%20%3B%0A%20%20%20%20return%20\u2019%24\u2019%3B%0A%7D%0A%20%0Avoid%20push(struct%20Stack*%20stack%2C%20char%20op)%0A%7B%0A%20%20%20%20stack-%3Earray%5B%2B%2Bstack-%3Etop%5D%20%3D%20op%3B%0A%7D%0A%20%0A%20%0A%2F%2F%20The%20main%20function%20that%20returns%20value%20of%20a%20given%20postfix%20expression%0Aint%20evaluatePostfix(char*%20exp)%0A%7B%0A%20%20%20%20%2F%2F%20Create%20a%20stack%20of%20capacity%20equal%20to%20expression%20size%0A%20%20%20%20struct%20Stack*%20stack%20%3D%20createStack(strlen(exp))%3B%0A%20%20%20%20int%20i%3B%0A%20%0A%20%20%20%20%2F%2F%20See%20if%20stack%20was%20created%20successfully%0A%20%20%20%20if%20(!stack)%20return%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20Scan%20all%20characters%20one%20by%20one%0A%20%20%20%20for%20(i%20%3D%200%3B%20exp%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20the%20scanned%20character%20is%20an%20operand%20(number%20here)%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20push%20it%20to%20the%20stack.%0A%20%20%20%20%20%20%20%20if%20(isdigit(exp%5Bi%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20push(stack%2C%20exp%5Bi%5D%20-%20\u20190\u2032)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20%20If%20the%20scanned%20character%20is%20an%20operator%2C%20pop%20two%0A%20%20%20%20%20%20%20%20%2F%2F%20elements%20from%20stack%20apply%20the%20operator%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20val1%20%3D%20pop(stack)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20val2%20%3D%20pop(stack)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20switch%20(exp%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019%2B\u2019%3A%20push(stack%2C%20val2%20%2B%20val1)%3B%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2032-\u2018%3A%20push(stack%2C%20val2%20-%20val1)%3B%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019*\u2019%3A%20push(stack%2C%20val2%20*%20val1)%3B%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019%2F\u2019%3A%20push(stack%2C%20val2%2Fval1)%3B%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%20pop(stack)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20char%20exp%5B%5D%20%3D%20%22231*%2B9-%22%3B%0A%20%20%20%20printf%20(%22Value%20of%20%25s%20is%20%25d%22%2C%20exp%2C%20evaluatePostfix(exp))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}