2ND SEMESTER
OUTLINES
Introduction
Operations on Stack
PUSH Operation
POP Operation
PEEP Operation
Applications of Stack
Infix to Postfix Expression Conversion
Evaluation of Postfix Expression
Infix to Prefix Expression
Evaluation of Prefix Expression
Questions & Answer Discussion
2
Introduction to Stacks
Examples:
Stack of books
Stack of CDs
3
What is a Stack?
A stack is a data structure in which data is added
and removed at only one end called the top.
4
Stack Operations
PUSH(Adding element on to the top of the stack)
5
PUSH Operation
ALGORITHM: PUSH (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size
MAX_SIZE.
Output: This algorithm pushes (or inserts) an element
ITEM on to the STACK.
Step 1: If TOP=MAX_SIZE1, then
Pri t ta k O erflo a d e it.
Step 2: Set TOP=TOP+1
Step 3: Set STACK [TOP] =ITEM
Step 4: Exit
6
START
Is
top=MAX
1
?
Enter an element to be
inserted
Set top=top+1
Print Stack
Overflow
STOP
7
36
25
41
23
12
PUSH Operation on stack
INSERT 12
3
INSERT 36
2
INSERT 25
Max_size=4 1
INSERT 23
0 INSERT 41
1
8
POP Operation
ALGORITHM: POP (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size MAX_SIZE.
Output: This algorithm deletes the top element of the STACK
and assigns it to the variable ITEM.
Step 1: If TOP=1, then
Pri t ta k U derflo a d e it.
Step 2: Set ITEM=STACK [TOP]
Step 3: Set TOP=TOP1
Step 4: Exit
9
START
POP Operation Flow
Chart
Is
Top= 1?
Set item=stack
[Top]
Set top=top1
STOP
10
POP Operation On Stack
36 3 ( POP )
25 2 ( POP )
23 1 ( POP )
41 0 ( POP )
1 ( POP )
StACK UNDERFLOW 11
PEEP Operation
ALGORITHM: PEEP (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size MAX_SIZE.
Output: This algorithm returns the top most element of the
STACK.
12
PEEP Operation on Stack
12
36
25
23
41
13
APPLICATIONS OF STACK
15
For example:
To re erse the stri g EVEE the stri g is read fro left
to right and its characters are pushed . LIKE: onto a stack.
16
CHECKING OF PARENTHESES
17
For example:
VALID INPUTS INVALID INPUTS
{} {(}
({[]}) ([(()])
{[]()} {}[])
[ { ( { } [ ] ( { })}] [{)}(]}]
18
Infix Notation
Ex: A + B
19
Prefix Notation
operand.
Prefix Notation.
20
Postfix Notation
In Postfix notation, the operator comes after the Operand.
Notation.
21
Examples of infix to prefix and post fix
Infix PostFix Prefix
22
CONVERSION OF INFIX TO POSTFIX
EXPRESSION
ALGORITHM: INFIX_TO_POSTFIX (I, P)
Input: I is an arithmetic expression written in infix notation.
Output: This algorithm converts the infix expression to its
equivalent postfix expression P.
24
Infix to Postfix Conversion
Infix Expression
(a+bc)*d(e+f)
Postfix Expression
25
Infix to Postfix Conversion
Infix Expression
(a+bc)*d(e+f) )
Postfix Expression
26
Infix to Postfix Conversion
Infix Expression
a+bc)*d(e+f) )
Postfix Expression
(
(
27
Infix to Postfix Conversion
Infix Expression
+bc)*d(e+f))
Postfix Expression
a
(
(
28
Infix to Postfix Conversion
Infix Expression
bc)*d(e+f))
Postfix Expression
+ a
(
(
29
Infix to Postfix Conversion
Infix Expression
c)*d(e+f))
Postfix Expression
ab
+
(
(
30
Infix to Postfix Conversion
Infix Expression
c)*d(e+f))
Postfix Expression
ab+

(
(
31
Infix to Postfix Conversion
Infix Expression
)*d(e+f))
Postfix Expression
ab+c

(
(
32
Infix to Postfix Conversion
Infix Expression
*d(e+f))
Postfix Expression
ab+c
33
Infix to Postfix Conversion
Infix Expression
d(e+f))
Postfix Expression
ab+c
*
(
34
Infix to Postfix Conversion
Infix Expression
(e+f))
Postfix Expression
ab+cd
*
(
Stack 35
Infix to Postfix Conversion
Infix Expression
(e+f))
Postfix Expression
ab+cd*

(
Stack 36
Infix to Postfix Conversion
Infix Expression
e+f))
Postfix Expression
( ab+cd*

(
Stack 37
Infix to Postfix Conversion
Infix Expression
+f))
Postfix Expression
ab+cd*e
(

(
Stack 38
Infix to Postfix Conversion
Infix Expression
f))
Postfix Expression
+
ab+cd*e
(

(
Stack 39
Infix to Postfix Conversion
Infix Expression
))
Postfix Expression
+
( ab+cd*ef

(
Stack 40
Infix to Postfix Conversion
Infix Expression
)
Postfix Expression
ab+cd*ef+

(
Stack 41
Infix to Postfix Conversion
Infix Expression
Postfix Expression
ab+cd*ef+
Stack 42
Evaluation of Postfix Expression
ALGORITHM: EVALUATE_POSTFIX (P)
Input: P is an arithmetic expression in postfix notation.
Output: This algorithm evaluates the postfix expression P and
assigns the result to the variable VALUE.
43
Contd
Step 4: If an operator OP is encountered, then
(a)Pop the two top elements of the STACK,
where A is the top element and B is the next
to top element.
(b)Evaluate B OP A.
(c)Push the result of the evaluation on to the
STACK.
[End of If]
[End of Step 2 loop]
Step 5: Set VALUE equal to the top most element of the STACK.
Step 6: EXIT
44
Evaluation of Postfix Expression
Postfix Expression
5 6 2 + * 12 4 / 
Result
Stack 45
Evaluation of Postfix Expression
Postfix Expression
5 6 2 + * 12 4 /  )
Result
Stack 46
Evaluation of Postfix Expression
Postfix Expression
6 2 + * 12 4 /  )
Result
5
Stack 47
Evaluation of Postfix Expression
Postfix Expression
2 + * 12 4 /  )
Result
6
5
Stack 48
Evaluation of Postfix Expression
Postfix Expression
+ * 12 4 /  )
Result
2
6
5
Stack 49
Evaluation of Postfix Expression
Postfix Expression
* 12 4 /  )
Result
8
5
Stack 50
Evaluation of Postfix Expression
Postfix Expression
12 4 /  )
Result
40
Stack 51
Evaluation of Postfix Expression
Postfix Expression
4 /  )
Result
12
40
Stack 52
Evaluation of Postfix Expression
Postfix Expression
/  )
Result
4
12
40
Stack 53
Evaluation of Postfix Expression
Postfix Expression
 )
Result
3
40
Stack 54
Evaluation of Postfix Expression
Postfix Expression
)
Result
37
Stack 55
Evaluation of Postfix Expression
Postfix Expression
Result
37
Stack 56
CONVERSION OF INFIX TO PREFIX
EXPRESSION
ALGORITHM: INFIX_TO_PREFIX (I, P)
Input: I is an arithmetic expression written in infix notation.
Output: This algorithm converts the infix expression to its
equivalent prefix expression P.
57
Contd
Step 5: If an operator OP is encountered, then
(a)Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Output Stack(Stack2)
which has higher precedence than OP.
(b)Push the operator OP on to the STACK.
[End of if]
tep 6: If a left pare theses is e ou tered, the
(a) Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Output Stack(Stack2)
u til a right pare theses is e ou tered.
e o e the right pare theses . [Do t add it to the
Output Stack(Stack2).]
[End of if]
[End of Step 2 loop]
Step 7: Pop the elements from the Output Stack(Stack2).
Step 8: Exit
58
Infix to Prefix Conversion
Infix Expression
(a+bc)*d(e+f)
Output Stack(Stack2)
Stack 59
Infix to Prefix Conversion
Infix Expression
((a+bc)*d(e+f)
Output Stack(Stack2)
)
Stack 60
Infix to Prefix Conversion
Infix Expression
((a+bc)*d(e+f
Output Stack(Stack2)
)
)
Stack 61
Infix to Prefix Conversion
Infix Expression
((a+bc)*d(e+
Output Stack(Stack2)
f
)
)
Stack 62
Infix to Prefix Conversion
Infix Expression
((a+bc)*d(e
Output Stack(Stack2)
+ f
)
)
Stack 63
Infix to Prefix Conversion
Infix Expression
( ( a + b  c ) * d (
Output Stack(Stack2)
fe
+
)
)
Stack 64
Infix to Prefix Conversion
Infix Expression
((a+bc)*d
Output Stack(Stack2)
fe+
)
Stack 65
Infix to Prefix Conversion
Infix Expression
((a+bc)*d
Output Stack(Stack2)
fe+

)
Stack 66
Infix to Prefix Conversion
Infix Expression
((a+bc)*
Output Stack(Stack2)
fe+d

)
Stack 67
Infix to Prefix Conversion
Infix Expression
((a+bc)
Output Stack(Stack2)
fe+d
*

)
Stack 68
Infix to Prefix Conversion
Infix Expression
((a+bc
Output Stack(Stack2)
)
fe+d
*

)
Stack 69
Infix to Prefix Conversion
Infix Expression
((a+b
Output Stack(Stack2)
)
fe+dc
*

)
Stack 70
Infix to Prefix Conversion
Infix Expression
((a+b

Output Stack(Stack2)
)
fe+dc
*

)
Stack 71
Infix to Prefix Conversion
Infix Expression
((a+

Output Stack(Stack2)
)
fe+dcb
*

)
Stack 72
Infix to Prefix Conversion
Infix Expression
+ ((a

Output Stack(Stack2)
)
fe+dcb
*

)
Stack 73
Infix to Prefix Conversion
Infix Expression
+ ((

Output Stack(Stack2)
)
fe+dcba
*

)
Stack 74
Infix to Prefix Conversion
Infix Expression
(
Output Stack(Stack2)
fe+dcba+
*

)
Stack 75
Infix to Prefix Conversion
Infix Expression
Output Stack(Stack2)
fe+dcba+*
Stack 76
Infix to Prefix Conversion
Infix Expression
Output Stack(Stack2)
fe+dcba+*
Prefix Expression
*+abcd+ef
Stack 77
Evaluation of Prefix Expression
ALGORITHM: EVALUATE_PREFIX (P)
Input: P is an arithmetic expression in prefix notation.
Output: This algorithm evaluates the prefix expression P and
assigns the result to the variable VALUE.
78
Evaluation of Prefix Expression
Step 4: If an operator OP is encountered, then
(a)Pop the two top elements of the STACK,
where A is the top element and B is the next
to top element.
(b)Evaluate A OP B.
(c)Push the result of the evaluation(b) on to the
STACK.
[End of If]
[End of Step 2 loop]
Step 5: Set VALUE equal to the top most element of the STACK.
Step 6: EXIT
79
Evaluation of Prefix Expression
Prefix Expression
 * 5 + 6 2 / 12 4
Result
Stack 80
Evaluation of Prefix Expression
Prefix Expression
(  * 5 + 6 2 / 12 4
Result
Stack 81
Evaluation of Prefix Expression
Prefix Expression
(  * 5 + 6 2 / 12
Result
Stack 82
Evaluation of Prefix Expression
Prefix Expression
(*5+62/
Result
12
Stack 83
Evaluation of Prefix Expression
Prefix Expression
(*5+62
Result
Stack 84
Evaluation of Prefix Expression
Prefix Expression
(*5+6
Result
Stack 85
Evaluation of Prefix Expression
Prefix Expression
(*5+
Result
Stack 86
Evaluation of Prefix Expression
Prefix Expression
(*5
Result
Stack 87
Evaluation of Prefix Expression
Prefix Expression
(*
Result
Stack 88
Evaluation of Prefix Expression
Prefix Expression
(
Result
40
Stack 89
Evaluation of Prefix Expression
Prefix Expression
(
Result
37
Stack 90
Evaluation of Prefix Expression
Prefix Expression
Result
37
Stack 91
Questions
a. Array
b. Lists
c. Stack
d. All of above
c. Stack
92
Questions
a. FIFO lists
b. LIFO list
c. Piles
d. Pushdown lists
a. FIFO Lists
93
Questions
a) Finding factorial
b) Tower of Hanoi
c) Infix to Postfix Conversion
d) All of the above
94
Questions
a) Queue
b) Stack
c) Tree
d) Graph
b) Stack
95
Questions
5.In the stack, if user try to remove element from the empty stack then it
is called 
a)Garbage Collection
b)Overflow Stack
c)Underflow Stack
d)Empty Collection
c) Underflow Stack
96
Questions
6. User push 1 element in the stack having already five elements and
having stack size as 5 then stack becomes
a)User flow
b)Underflow
c)Crash
d)Overflow
d)Overflow
97
Questions
b) 1
98
Questions
a)Underflow Occurs
b)Overflow Occurs
c)Stack operation will be performed smoothly
d)None of these
a)Underflow Occurs
99
Questions
9.What will be the postfix expression for the following infix expression
b*c+d/e
a. b*cde/+
b. bc*de/+
c. bcd*e/+
d. bc*de+/
b. bc*de/+
100
Questions
a. Infix Expression
b. Prefix Expression
c. Postfix Expression
c. Postfix Expression
101
Questions
a. Implementation of Recursion
b. Evaluation of postfix expression
c. Allocating resources and scheduling
d. Reversing String
102
Questions
a. list
b. queue
c. hash map
d. stack
d. stack
103
Questions
+ab/*/cdefg
104