Dsa CH 4 Stacks Application
Dsa CH 4 Stacks Application
Quick Introduction
Stacks are linear data structures.
A stack is a particular kind of abstract data type or
collection in which the only operations on the collection
are the addition of an entity to the collection, known as
push and removal of an entity, known as pop
All deletions and insertions occur at one end of the
stack known as the TOP.
Data going into the stack first, leaves out last.
Stacks are also known as LIFO data structures (Last-In,
First-Out).
Basic Stack Operations
push – Adds an item to the top of a stack.
Prefix: + a b
Infix: a + b (what we use in grammar school)
Postfix: a b +
a) A * B - ( C + D ) + E empty empty
b) * B - ( C + D ) + E empty A
c) B - ( C + D ) + E * A
d) - ( C + D ) + E * A B
e) - ( C + D ) + E empty A B *
f) ( C + D ) + E - A B *
g) C + D ) + E - ( A B *
h) + D ) + E - ( A B * C
i) D ) + E - ( + A B * C
j) ) + E - ( + A B * C D
k) + E - A B * C D +
l) + E empty A B * C D + -
m) E + A B * C D + -
n) + A B * C D + - E
o) empty A B * C D + - E +
Postfix Evaulation
Read the input symbol from left to right
Operand: push
Operator: pop 2 operands, do the math, pop result
back onto stack
1 2 3 + *
Polish Notation
Introduced in 1924
Operators are placed before operand
Eg: + a b
Infix to Prefix Conversion
1. Scan the expression from right to left for each
character.
2. If the input is an operand then place it in the output
string
3. If the input is operator then push it into the stack.
❖ If the input operator is having highest or equal priority
than stack top operator then push it into the stack.
9 empty 9
- - 9
4 - 9 4
* - * 9 4
5 - * 9 4 5
+ - + 9 4 5 *
3 - + 9 4 5 * 3
* - + * 9 4 5 * 3
2 - + * 9 4 5 * 3 2
empty 9 4 5 * 3 2 * + -
Output : - + * 2 3 * 5 4 9
Prefix Evaluation
Scan the expression from Right to left
If operand – push into the stack
Output : 17
Quick test
Stacks are _____________data structures.
A stack is a particular kind of abstract data type or
collection in which the only operations on the collection
are the addition of an entity to the collection, known as
_____ and removal of an entity, known as __________
All deletions and insertions occur at one end of the
stack known as the ________.
Data going into the stack first, leaves out last.
Stacks are also known as _____ data structures
Quick test
1. Process of inserting an element in stack is called
____________
a) Create ANS: B
b) Push
c) Evaluation
d) Pop
2. Process of removing an element from stack is called
_____
a) Create
b) Push ANS: D
c) Evaluation
d) Pop
Quick test
3. In a stack, if a user tries to remove an element from
an empty stack it is called _________
a) Underflow ANS: A
b) Empty collection
c) Overflow
d) Garbage Collection
4. Pushing an element into stack already having five elements
and stack size of 5, then stack becomes ___________
a) Overflow
b) Crash
ANS: A
c) Underflow
d) User flow
Quick test
5.Which element would get first deleted from the stack?
a) The element in the middle of the stack ANS: B
b) The element at the top of the stack
c) The element at the bottom of the stack
Stack Applications
INFIX to prefix, postfix – problem, algorithm, code