Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the conversions of expressions of stacks in C language
Stack is a linear data structure where data is inserted and removed only at one end. Stacks are particularly useful for converting expressions between different notations like infix, prefix, and postfix.
Stack Operations
Before understanding expression conversions, let's review the basic stack operations −
Push Operation
Inserts an element at the top of the stack −
if (top == n-1)
printf("Stack overflow");
else {
top++;
stack[top] = item;
}
Pop Operation
Removes and returns the top element from the stack −
if (top == -1)
printf("Stack underflow");
else {
item = stack[top];
top--;
}
Types of Expressions
There are three types of expressions in C language −
- Infix expression − Operator is between operands. Example: A + B
- Prefix expression − Operator is before operands. Example: + A B
- Postfix expression − Operator is after operands. Example: A B +
Infix to Postfix Conversion Algorithm
The following algorithm converts infix expressions to postfix using a stack −
- If the input symbol is an operand, add it to the output
- If the input symbol is '(', push it onto the stack
- If the input symbol is ')', pop all operators until '(' is found
- If the input symbol is an operator, compare its precedence with the stack top
- If end of input, pop all remaining operators from stack
Example: Infix to Postfix Conversion
Here's a complete program that converts an infix expression to postfix notation −
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char c) {
if (top < MAX - 1) {
stack[++top] = c;
}
}
char pop() {
if (top >= 0) {
return stack[top--];
}
return '\0';
}
int precedence(char op) {
switch (op) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
default: return 0;
}
}
void infixToPostfix(char infix[], char postfix[]) {
int i = 0, j = 0;
while (infix[i] != '\0') {
char c = infix[i];
if (isalnum(c)) {
postfix[j++] = c;
}
else if (c == '(') {
push(c);
}
else if (c == ')') {
while (top >= 0 && stack[top] != '(') {
postfix[j++] = pop();
}
pop(); // Remove '('
}
else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (top >= 0 && stack[top] != '(' &&
precedence(stack[top]) >= precedence(c)) {
postfix[j++] = pop();
}
push(c);
}
i++;
}
while (top >= 0) {
postfix[j++] = pop();
}
postfix[j] = '\0';
}
int main() {
char infix[] = "A+B*C/D-E";
char postfix[MAX];
printf("Infix Expression: %s<br>", infix);
infixToPostfix(infix, postfix);
printf("Postfix Expression: %s<br>", postfix);
return 0;
}
Infix Expression: A+B*C/D-E Postfix Expression: ABC*D/+E-
Conversion Steps
The conversion process for expression A+B*C follows these steps −
| Input | Stack | Output |
|---|---|---|
| A | empty | A |
| + | + | A |
| B | + | AB |
| * | +* | AB |
| C | +* | ABC |
| end | empty | ABC*+ |
Conclusion
Stack-based expression conversion is fundamental in compiler design and expression evaluation. The stack maintains operator precedence and handles parentheses correctly, making it an ideal data structure for parsing mathematical expressions.
