DATA STRUCTURES PROGRAM • STACKS & EXPRESSIONS
Evaluate a Postfix Expression
Learn how to evaluate a postfix expression using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Value = 17
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stddef.h>
#define CAPACITY 50
struct Stack { int values[CAPACITY]; int top; };
void stack_init(struct Stack *stack) { stack->top = -1; }
int stack_empty(const struct Stack *stack) { return stack->top < 0; }
void stack_push(struct Stack *stack, int value)
{
if (stack->top + 1 >= CAPACITY) { puts("Stack overflow."); exit(EXIT_FAILURE); }
stack->values[++stack->top] = value;
}
int stack_pop(struct Stack *stack)
{
if (stack_empty(stack)) { puts("Stack underflow."); exit(EXIT_FAILURE); }
return stack->values[stack->top--];
}
int stack_peek(const struct Stack *stack)
{
if (stack_empty(stack)) { puts("Stack is empty."); exit(EXIT_FAILURE); }
return stack->values[stack->top];
}
int main(void)
{
const char postfix[] = "23*54*+9-";
struct Stack stack; stack_init(&stack);
for (size_t index = 0; postfix[index] != '\0'; index++) {
char token = postfix[index];
if (isdigit((unsigned char) token)) stack_push(&stack, token - '0');
else {
int right = stack_pop(&stack), left = stack_pop(&stack);
switch (token) {
case '+': stack_push(&stack, left + right); break;
case '-': stack_push(&stack, left - right); break;
case '*': stack_push(&stack, left * right); break;
case '/': stack_push(&stack, left / right); break;
}
}
}
printf("Value = %d\n", stack_pop(&stack));
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Value = 17
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Push operands and replace each operator with the result of its two latest operands.
- Display the computed result.
Push operands and replace each operator with the result of its two latest operands.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(n)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
