2. WHAT IS A STACK?
IT’S A DATA STRUCTURE WHERE YOU CAN ADD OR
REMOVE ITEMS ONLY FROM THE TOP!
OPERATIONS:
• PUSH : When you ADD an Item
to stack
• POP : when you REMOVE an
item from the stack
STACK is a LIFO Data Structure
3. PUSH AND POP
I n i t i a l i z e
S t a c k s
S
SSSS
SSS
bat
bat
ball
bat
cat
ball
bat
cat
ball
bat
rat
ball
bat
rat
ball
bat
ball
S . p o p ( )S . p u s h ( r a t )X = S . p o pX = S . p o p ( )
S . p u s h ( c a t )S . p u s h ( b a l l )S . p u s h ( b a t )
4. HOW ARE STACKS IMPLEMENTED ?
ARRAY Implementation
56 56
s.push(56) s.pop()s.push(90)top=-1
3
2
1
0
3
2
1
0
3
2
1
0
3
2
1
056
90
top = top =
top = 90
5. EXPRESSIONS
( A + [ D - { B * C } )
) A + B (
Parenthesis Plays an important role in expressions
6. TYPES OF EXPRESSIONS
INFIX PREFIX POSTFIX
How do we determine the expressions?
The relative position of
the operator to operands
+AB AB+A+B
PREFIX POSTFIXINFIX
7. WHY DO WE NEED PREFIX, POSTFIX?
NO need of precedence rules
NO need of parenthesis
Easier for Computer to parse