0% found this document useful (0 votes)
6 views

Infix to Postfix Conversion in using stack in C-2

Uploaded by

drgopkumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Infix to Postfix Conversion in using stack in C-2

Uploaded by

drgopkumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Infix to Postfix Conversion

using stack in C

Understanding the
Need, Algorithm,
and Code
Presented
Implementation
by Eshan H
01 - Introduction

02 - Overview of the Algorithm

03 - Code Implementation

04 - Example Conversion (using code)


01 - Introduction

Why Infix to postfix? Key Points:


1. Elimination of Parentheses
Infix notation
(e.g., A + B * C) is 2. Ease of Evaluation
more readable for
humans, while postfix 3. Compiler Design
notation 4. Avoiding Ambiguities
(e.g., A B C * +) is
simpler for computers
to evaluate.
Overview of the Algorithm

Step 1: Initialize an Empty Stack

Step 2: Read each symbol from the infix expression

Step 3: if Operand : Append to postfix

Step 4: if Operator : Pop operators from stack to postfix until stack is empty or

has an operator of lower precedence

Step 5: if ‘(’ : Push onto stack

Step 6: if ‘)’ : POP and append until a ‘(’ is encountered

Step 7: End of expression : POP remaining operators to postfix


Code Implementation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void Evaluate();
void push(char);
char pop();
int prec(char);
char infix[30], postfix[30], stack[30];
int top = -1;
void main() {
printf("Enter the valid infix expression: "); scanf("%s", infix);
Evaluate();
printf("The entered infix expression is: %s\n", infix);
printf("The corresponding postfix expression is: %s\n", postfix);
}
Code Implementation
void Evaluate() { while (prec(stack[top]) >= prec(symb)) {
char symb, temp; temp = pop();
int i = 0, j = 0; postfix[j++] = temp;
push('#'); // Start with a sentinel value }
for (i = 0; infix[i] != '\0'; i++) { push(symb);
symb = infix[i]; break;
switch (symb) { default: postfix[j++] = symb; // If operand, add it directly to postfix break; } } // Pop
case '(': push(symb); all remaining operators in stack
break; case ')': while (top > 0) {
temp = pop(); temp = pop();
while (temp != '(') { postfix[j++] = temp;
postfix[j++] = temp; }
temp = pop(); postfix[j] = '\0'; // Null-terminate the postfix expression }
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '$':
Code Implementation
void push(char item) { int prec(char symb) {
int p;
top++; switch (symb) {
case '#': p = -1;
stack[top] = item;
break;
} case '(': case ')': p = 0;
break;
case '+':
char pop() { case '-': p = 1;
char item; break;
case '*':
item = stack[top]; case '/':
top--; case '%': p = 2;
break;
return item; case '^':
} case '$': p = 3;
break;
default: p = 0; break;
}
return p;
}
Example Conversion (using code)

Conversion of infix expression A + B * C to postfix :


1.Initialize: 5.Process * :
Stack: [#] Operator, has higher precedence than +, so push to stack.
Postfix: [] Stack: [#, +, *]
2.Process A: Postfix: [A, B]
Operand, add to postfix. 6.Process C :
Stack: [#] Operand, add to postfix.
Postfix: [A] Stack: [#, +, *]
3.Process +: Postfix: [A, B, C]
Operator, push to stack (stack is empty above #). 7.End of Expression:
Stack: [#, +] Pop remaining operators from the stack.
Postfix: [A] Stack: [#]
4.Process B: Postfix: [A, B, C, *, +]
Operand, add to postfix. The final postfix expression is A B C * +
Stack: [#, +]
Postfix: [A, B]
Thank You

Comingup next
Evaluation of Postfix Expression

You might also like