Stack
Stack
ICT2101
Dr. Mohammad Abu Yousuf
[email protected]
1
STACKS
2
What is STACKS ?
3
What is STACKS ?
4
Basic operations
5
Array representation of stacks
6
Array representation of stacks
7
Push Operation
• Push an item onto the top of the stack (insert an item)
8
Push Operation
9
Pop Operation
• Pop an item off the top of the stack (delete an item)
10
Pop Operation
11
Here are the minimal operations we'd need for an abstract stack (and their
typical names):
o Push: Places an element/value on top of the stack.
o Pop: Removes value/element from top of the stack.
o IsEmpty: Reports whether the stack is Empty or not.
o IsFull: Reports whether the stack is Full or not.
12
13
14
Application of the Stack (Arithmetic
Expressions)
INFIX , POST FIX AND PRE FIX NOTATIONS :
16
Application of the Stack (Arithmetic
Expressions)
• Example:
(66 + 22) * 5 – 567 / 42
to postfix
66 22 + 5 * 567 42 / –
17
Transforming Infix Expression into Postfix
Expression
• The following algorithm transform the infix expression Q
into its equivalent postfix expression P. It uses a stack to
temporary hold the operators and left parenthesis.
18
Transforming Infix Expression into Postfix
Expression
19
• Convert Q: A+( B * C – ( D / E ^ F ) * G ) * H into postfix form
showing stack status .
• Now add “)” at the end of expression
A+( B * C – ( D / E ^ F ) * G ) * H ) and also Push a “(“ on Stack.
Example
20
Evaluation of Postfix Expression
• If P is an arithmetic expression written in postfix
notation. This algorithm uses STACK to hold operands,
and evaluate P.
21
Evaluation of Postfix Expression
22
Evaluation of Postfix Expression
23
Thank you
24