Infix to Postfix conversion
SOUMEN SANTRA
MCA, M.TECH, SCJP, MCP
ALGORITHM
Scan the Infix expression left to right
 If the character x is an operand
 Output the character into the Postfix Expression
 If the character x is a left or right parenthesis
 If the character is “(“
 Push it into the stack
 if the character is “)”
 Repeatedly pop and output all the operators/characters
until “(“ is popped from the stack.
 If the character x is a is a regular operator
 Step 1: Check the character y currently at the top of the
stack.
 Step 2: If Stack is empty or y=‘(‘ or y is an operator of
lower precedence than x, then push x into stack.
 Step 3: If y is an operator of higher or equal precedence
than x, then pop and output y and push x into the stack.
When all characters in infix expression are processed repeatedly pop the
character(s) from the stack and output them until the stack is empty.
Infix Expression
Postfix Expression
( a + b - c ) * d – ( e + f )
Stack
Infix to postfix conversion
a + b - c ) * d – ( e + f )
(
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
+ b - c ) * d – ( e + f )
(
a
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
b - c ) * d – ( e + f )
(
a
+
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
- c ) * d – ( e + f )
(
a b
+
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
c ) * d – ( e + f )
(
a b +
-
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
) * d – ( e + f )
(
a b + c
-
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
* d – ( e + f )
a b + c -
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
d – ( e + f )
a b + c -
*
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
– ( e + f )
a b + c - d
*
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
( e + f )
a b + c – d *
-
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
e + f )
a b + c – d *
-
(
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
+ f )
a b + c – d * e
-
(
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
f )
a b + c – d * e
-
(
+
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
)
a b + c – d * e f
-
(
+
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
a b + c – d * e f +
-
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
a b + c – d * e f + -
Infix Expression
Postfix Expression
Stack
Infix to postfix conversion
#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 20
typedef struct{
int top;
char items[STACKSIZE];
}STACK;
void push(STACK *, char);
char pop(STACK *);
void main()
{
int i;
char x, y, E[20] ; /* Assume that Infix Expression E contains single-digit
integers/parenthesis/operators*/
STACK s;
s.top = -1; /* Initialize the stack is */
printf("Enter the Infix Expression:");
scanf("%s",E);
for(i=0;E[i] != '0';i++){
x= E[i];
IMPLEMENTATION IN C
if(x<=’z’ && x>=’a’) /* Consider all lowercase letter operands from a to z */
printf(“%c”,x);
else if(x == ’(’)
push(&s ,x);
else if(x == ’)’){
y=pop(&s) ;
while(y != ‘(‘){
printf(“%c”,y);
y=pop(&s) ;
}
}
else {
if(s.top ==-1 || s.items[s.top] == ‘(‘)
push(&s ,x);
else {
y = s.items[s.top]; /* y is the top operator in the stack*/
if( y==’*’ || y==’/’){ /* precedence of y is higher/equal to x*/
printf(“%c”, pop(&s));
push(&s ,x);
}
else if ( y==’+’ || y==’-’)
if( x==’+’ || x==’-’) { /* precedence of y is equal to x*/
printf(“%c”, pop(&s));
push(&s ,x);
}
else /* precedence of y is less than x*/
push(&s ,x);
}
}
}
while(s.top != -1)
printf(“%c”,pop(&s));
}
void push(STACK *sptr, char ps) /*pushes ps into stack*/
{
if(sptr->top == STACKSIZE-1){
printf("Stack is fulln");
exit(1); /*exit from the function*/
}
else
sptr->items[++sptr->top]= ps;
}
char pop(STACK *sptr)
{
if(sptr->top == -1){
printf("Stack is emptyn");
exit(1); /*exit from the function*/
}
else
return sptr->items[sptr->top--];
}
THANK YOU
GIVE FEEDBACK

Infix to Postfix Conversion Using Stack

  • 1.
    Infix to Postfixconversion SOUMEN SANTRA MCA, M.TECH, SCJP, MCP
  • 2.
    ALGORITHM Scan the Infixexpression left to right  If the character x is an operand  Output the character into the Postfix Expression  If the character x is a left or right parenthesis  If the character is “(“  Push it into the stack  if the character is “)”  Repeatedly pop and output all the operators/characters until “(“ is popped from the stack.  If the character x is a is a regular operator  Step 1: Check the character y currently at the top of the stack.  Step 2: If Stack is empty or y=‘(‘ or y is an operator of lower precedence than x, then push x into stack.  Step 3: If y is an operator of higher or equal precedence than x, then pop and output y and push x into the stack. When all characters in infix expression are processed repeatedly pop the character(s) from the stack and output them until the stack is empty.
  • 3.
    Infix Expression Postfix Expression (a + b - c ) * d – ( e + f ) Stack Infix to postfix conversion
  • 4.
    a + b- c ) * d – ( e + f ) ( Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 5.
    + b -c ) * d – ( e + f ) ( a Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 6.
    b - c) * d – ( e + f ) ( a + Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 7.
    - c )* d – ( e + f ) ( a b + Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 8.
    c ) *d – ( e + f ) ( a b + - Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 9.
    ) * d– ( e + f ) ( a b + c - Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 10.
    * d –( e + f ) a b + c - Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 11.
    d – (e + f ) a b + c - * Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 12.
    – ( e+ f ) a b + c - d * Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 13.
    ( e +f ) a b + c – d * - Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 14.
    e + f) a b + c – d * - ( Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 15.
    + f ) ab + c – d * e - ( Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 16.
    f ) a b+ c – d * e - ( + Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 17.
    ) a b +c – d * e f - ( + Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 18.
    a b +c – d * e f + - Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 19.
    a b +c – d * e f + - Infix Expression Postfix Expression Stack Infix to postfix conversion
  • 20.
    #include<stdio.h> #include<stdlib.h> #define STACKSIZE 20 typedefstruct{ int top; char items[STACKSIZE]; }STACK; void push(STACK *, char); char pop(STACK *); void main() { int i; char x, y, E[20] ; /* Assume that Infix Expression E contains single-digit integers/parenthesis/operators*/ STACK s; s.top = -1; /* Initialize the stack is */ printf("Enter the Infix Expression:"); scanf("%s",E); for(i=0;E[i] != '0';i++){ x= E[i]; IMPLEMENTATION IN C
  • 21.
    if(x<=’z’ && x>=’a’)/* Consider all lowercase letter operands from a to z */ printf(“%c”,x); else if(x == ’(’) push(&s ,x); else if(x == ’)’){ y=pop(&s) ; while(y != ‘(‘){ printf(“%c”,y); y=pop(&s) ; } } else { if(s.top ==-1 || s.items[s.top] == ‘(‘) push(&s ,x); else { y = s.items[s.top]; /* y is the top operator in the stack*/ if( y==’*’ || y==’/’){ /* precedence of y is higher/equal to x*/ printf(“%c”, pop(&s)); push(&s ,x); }
  • 22.
    else if (y==’+’ || y==’-’) if( x==’+’ || x==’-’) { /* precedence of y is equal to x*/ printf(“%c”, pop(&s)); push(&s ,x); } else /* precedence of y is less than x*/ push(&s ,x); } } } while(s.top != -1) printf(“%c”,pop(&s)); } void push(STACK *sptr, char ps) /*pushes ps into stack*/ { if(sptr->top == STACKSIZE-1){ printf("Stack is fulln"); exit(1); /*exit from the function*/ } else sptr->items[++sptr->top]= ps; }
  • 23.
    char pop(STACK *sptr) { if(sptr->top== -1){ printf("Stack is emptyn"); exit(1); /*exit from the function*/ } else return sptr->items[sptr->top--]; }
  • 24.