How to Evaluate a Postfix Expression
- Traverse the expression: We iterate through each character of the postfix expression from left to right.
- Push operands onto a stack: When encountering a number (operand), push it onto a stack.
- Apply operators: 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.
- End condition: After traversing the entire expression, the result will be the single value left on the stack.
Algorithm
- Initialize an empty stack.
- Read the postfix expression character by character:
- If the character is an operand, push it to the stack.
- If the character is an operator, pop two operands from the stack, apply the operator, and push the result back onto the stack.
- At the end, the value left in the stack is the result of the postfix expression.
Example
Let’s take the postfix expression:
53+82-*
This corresponds to the infix expression:
(5 + 3) * (8 - 2) = 48
C Program to Evaluate Postfix Expression
#include <stdio.h>
#include <ctype.h> // For isdigit() function
#include <stdlib.h> // For exit() function
#define MAX 100
// Stack structure
int stack[MAX];
int top = -1;
// Push function
void push(int value)
{
if (top == MAX - 1)
{
printf("Stack Overflow\n");
exit(1);
}
stack[++top] = value;
}
// Pop function
int pop()
{
if (top == -1)
{
printf("Stack Underflow\n");
exit(1);
}
return stack[top--];
}
// Function to evaluate postfix expression
int evaluatePostfix(char* expression)
{
int i = 0;
while (expression[i] != '\0')
{
// If the character is an operand (digit), push it to the stack
if (isdigit(expression[i]))
{
push(expression[i] - '0'); // Convert char to int
}
// If the character is an operator, pop two elements and apply the operator
else
{
int val1 = pop();
int val2 = pop();
switch (expression[i])
{
case '+': push(val2 + val1); break;
case '-': push(val2 - val1); break;
case '*': push(val2 * val1); break;
case '/': push(val2 / val1); break;
default:
printf("Invalid operator encountered\n");
exit(1);
}
}
i++;
}
// The result will be the last element in the stack
return pop();
}
int main()
{
char postfixExpression[] = "53+82-*";
printf("Postfix Expression: %s\n", postfixExpression);
int result = evaluatePostfix(postfixExpression);
printf("Result of the Postfix Expression: %d\n", result);
return 0;
}
Explanation of the Code
- Stack: We use an array
stack[]to implement the stack, withtopkeeping track of the top of the stack. - Push and Pop Functions: These handle adding and removing elements from the stack.
push(int value)adds an element to the top of the stack.pop()removes the top element from the stack and returns it.
- Main Function:
- The postfix expression
53+82-*is passed to theevaluatePostfix()function. - 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.
- The postfix expression
- Switch-Case: Used to apply the correct operator on the two popped values.
- Final Result: After evaluating the entire expression, the result is stored at the top of the stack, and we print it.
Example Walkthrough
For the expression 53+82-*:
- Push
5onto the stack. - Push
3onto the stack. - Encounter
+: Pop5and3, calculate5 + 3 = 8, push8. - Push
8onto the stack. - Push
2onto the stack. - Encounter
-: Pop8and2, calculate8 - 2 = 6, push6. - Encounter
*: Pop8and6, calculate8 * 6 = 48, push48.
Final result: 48.
Output
Postfix Expression: 53+82-*
Result of the Postfix Expression: 48
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.