Infix%20to%20postfix%20conversion - PowerPoint PPT Presentation

About This Presentation
Title:

Infix%20to%20postfix%20conversion

Description:

Infix to postfix conversion. Scan the Infix expression left to right ... Repeatedly pop and output all the operators/characters until '(' is popped from the stack. ... – PowerPoint PPT presentation

Number of Views:270
Avg rating:3.0/5.0
Slides: 23
Provided by: cse164
Category:

less

Transcript and Presenter's Notes

Title: Infix%20to%20postfix%20conversion


1
Infix to postfix conversion
  • 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.

2
Infix to postfix conversion
Stack
Infix Expression
( a b - c ) d ( e f )
Postfix Expression
3
Infix to postfix conversion
Stack
Infix Expression
a b - c ) d ( e f )
Postfix Expression
(
4
Infix to postfix conversion
Stack
Infix Expression
b - c ) d ( e f )
Postfix Expression
a
(
5
Infix to postfix conversion
Stack
Infix Expression
b - c ) d ( e f )
Postfix Expression
a

(
6
Infix to postfix conversion
Stack
Infix Expression
- c ) d ( e f )
Postfix Expression
a b

(
7
Infix to postfix conversion
Stack
Infix Expression
c ) d ( e f )
Postfix Expression
a b
-
(
8
Infix to postfix conversion
Stack
Infix Expression
) d ( e f )
Postfix Expression
a b c
-
(
9
Infix to postfix conversion
Stack
Infix Expression
d ( e f )
Postfix Expression
a b c -
10
Infix to postfix conversion
Stack
Infix Expression
d ( e f )
Postfix Expression
a b c -

11
Infix to postfix conversion
Stack
Infix Expression
( e f )
Postfix Expression
a b c - d

12
Infix to postfix conversion
Stack
Infix Expression
( e f )
Postfix Expression
a b c d
-
13
Infix to postfix conversion
Stack
Infix Expression
e f )
Postfix Expression
a b c d
(
-
14
Infix to postfix conversion
Stack
Infix Expression
f )
Postfix Expression
a b c d e
(
-
15
Infix to postfix conversion
Stack
Infix Expression
f )
Postfix Expression
a b c d e

(
-
16
Infix to postfix conversion
Stack
Infix Expression
)
Postfix Expression
a b c d e f

(
-
17
Infix to postfix conversion
Stack
Infix Expression
Postfix Expression
a b c d e f
-
18
Infix to postfix conversion
Stack
Infix Expression
Postfix Expression
a b c d e f -
19
includeltstdio.hgt includeltstdlib.hgt define
STACKSIZE 20 typedef struct int top char
itemsSTACKSIZE STACK void push(STACK ,
char) char pop(STACK ) void main() int
i char x,y, E20 / 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(i0E
i ! '\0'i) x Ei
20
if(xltz xgta) / Consider all lowercase
letter operands from a to z / printf(c,x) els
e if(x () push(s ,x) else if(x
)) ypop(s) while(y ! () printf(c,y)
ypop(s) else if(s.top -1
s.itemss.top () push(s ,x) else y
s.itemss.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)
21
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-gttop STACKSIZE-1) printf("St
ack is full\n") exit(1) /exit from the
function/ else sptr-gtitemssptr-gttop ps
22
char pop(STACK sptr) if(sptr-gttop
-1) printf("Stack is empty\n") exit(1) /exit
from the function/ else return
sptr-gtitemssptr-gttop--
Write a Comment
User Comments (0)
About PowerShow.com