Stack - cvrce

DATA STRUCTURE USING C (BE2106)
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.
• To add (push) an item to the stack, it must be
placed on the top of the stack.
• To remove (pop) an item from the stack, it must
be removed from the top of the stack too.
• Thus, the last element that is pushed into the
stack, is the first element to be popped out of
the stack.
i.e., A stack is a Last In First Out (LIFO) data
structure
4
Stack Operations
• PUSH(Adding element on to the top of the stack)
• POP(Deleting the top most element of the stack)
• PEEP(Displaying the top most element of the
stack)
• DISPLAY(Visiting all the stack elements)
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_SIZE-1, 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=MAX1
?
Enter an element to be
inserted
Set top=top+1
Print “Stack
Overflow”
Set stack [Top] =Item
STOP
7
PUSH Operation on stack
36
25
41
23
12
StACK OVERFLOW
Top=MAX_SIZE-1
INSERT 12
Max_size=4
3
INSERT 36
2
INSERT 25
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=TOP-1
Step 4: Exit
9
START
POP Operation Flow
Chart
Is
Top= -1?
Set item=stack
[Top]
Set top=top-1
Print the
popped
element
Print “Stack
Underflow”
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.
Step 1: If TOP=-1, then
Pri t “ta k is e pt
Step 2: Return STACK [TOP]
Step 3: Exit
a d e it.
12
PEEP Operation on Stack
12
36
25
23
41
13
APPLICATIONS OF STACK
1. Conversion of Infix expression to equivalent
Postfix Expression
2. Evaluation of Postfix Expression
3. Conversion of Infix expression to equivalent
Prefix Expression
4. Evaluation of Prefix Expression
5. Implementation of Recursion
6. Reversing a string
14
REVERSING A STRING
I. To reverse a string , the characters of string are
pushed onto the stack one by one as the string
is read from left to right.
• Once all the characters of string are pushed
onto stack, they are popped one by one. Since
the character last pushed in comes out first,
subsequent pop operation results in the reversal
of the string.
15
For example:
To re erse the stri g ‘EVE‘“E the stri g is read fro left
to right and its characters are pushed . LIKE: onto a stack.
16
CHECKING OF PARENTHESES
•
Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.
17
For example:
VALID INPUTS
INVALID INPUTS
{}
({[]})
{[]()}
[ { ( { } [ ] ( { })}]
{(}
([(()])
{}[])
[{)}(]}]
18
Infix Notation
 Infix notation is the common arithmetic and logical formula
notation, in which operators are written infix-style between
the operands they act on
Ex: A + B
19
Prefix Notation
• In Prefix notation, the operator comes before the
operand.
• The Infix expression A+B will be written as +AB in its
Prefix Notation.
• Prefix is also called Polish Notation
20
Postfix Notation
• In Postfix notation, the operator comes after the Operand.
• The Infix expression A+B will be written as AB+ in its Postfix
Notation.
• Postfix is also called Reverse Polish Notation
21
Examples of infix to prefix and post fix
Infix
PostFix
Prefix
A+B
AB+
+AB
(A+B) * (C + D)
AB+CD+*
*+AB+CD
A-B/(C*D^E)
ABCDE^*/-
-A/B*C^DE
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.
“tep 1: Add to the e d of the i fi e pressio I a d push
o to the “TACK.
Step 2: Scan I from left to right and repeat Step 3 to Step 6 for
each element of I until the STACK is empty.
Step 3: If an operand is encountered, add it to P.
“tep 4: If a left pare theses is e ou tered, push it o to the
STACK.
23
Contd…
Step 5: If an operator OP is encountered, then
(a)Repeatedly pop from the STACK and add each operator
o the top of the “TACK to the Postfi e pressio P
which has same precedence as or higher precedence than
OP.
(b)Push the operator OP on to the STACK.
[End of if]
“tep 6: If a right pare theses is e ou tered, the
(a) Repeatedly pop from the STACK and add each operator
o the top of the “TACK to the Postfi e pressio P
u til a left pare theses is e ou tered.
‘e o e the left pare theses . [Do t add it to the
postfix expression P.]
[End of if]
[End of Step 2 loop]
Step 7: Exit
24
Infix to Postfix Conversion
Infix Expression
(a+b-c)*d–(e+f)
Postfix Expression
25
Infix to Postfix Conversion
Infix Expression
(a+b-c)*d–(e+f) )
Postfix Expression
(
26
Infix to Postfix Conversion
Infix Expression
a+b-c)*d–(e+f) )
Postfix Expression
(
(
27
Infix to Postfix Conversion
Infix Expression
+b-c)*d–(e+f))
Postfix Expression
a
(
(
28
Infix to Postfix Conversion
Infix Expression
b-c)*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+c–d
*
(
Stack
35
Infix to Postfix Conversion
Infix Expression
(e+f))
Postfix Expression
ab+c–d*
(
Stack
36
Infix to Postfix Conversion
Infix Expression
e+f))
Postfix Expression
(
ab+c–d*
(
Stack
37
Infix to Postfix Conversion
Infix Expression
+f))
Postfix Expression
(
ab+c–d*e
(
Stack
38
Infix to Postfix Conversion
Infix Expression
f))
+
(
-
Postfix Expression
ab+c–d*e
(
Stack
39
Infix to Postfix Conversion
Infix Expression
))
+
(
Postfix Expression
ab+c–d*ef
(
Stack
40
Infix to Postfix Conversion
Infix Expression
)
Postfix Expression
ab+c–d*ef+
(
Stack
41
Infix to Postfix Conversion
Infix Expression
Postfix Expression
ab+c–d*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.
“tep 1: Add right pare theses to the e d of P.
Step 2: Scan P from left to right and repeat Step 3 to Step 4 for
ea h ele e t of P u til the is e ou tered.
Step 3: If an operand is encountered, push it on to the STACK.
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.
“tep 1: Add to the egi i g of the i fi e pressio I a d
push
o to the “TACK.
Step 2: Scan I from right to left and repeat Step 4 to Step 6 for
each element of I until the STACK is empty.
Step 3: If an operand is encountered, PUSH it on to the
Output
Stack(Stack2).
“tep 4: If a right pare theses is e ou tered, push it o to the
STACK.
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+b-c)*d–(e+f)
Output Stack(Stack2)
Stack
59
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e+f)
Output Stack(Stack2)
)
Stack
60
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e+f
Output Stack(Stack2)
)
)
Stack
61
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e+
Output Stack(Stack2)
f
)
)
Stack
62
Infix to Prefix Conversion
Infix Expression
((a+b-c)*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+b-c)*d–
Output Stack(Stack2)
fe+
)
Stack
65
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d
Output Stack(Stack2)
fe+
)
Stack
66
Infix to Prefix Conversion
Infix Expression
((a+b-c)*
Output Stack(Stack2)
fe+d
)
Stack
67
Infix to Prefix Conversion
Infix Expression
((a+b-c)
Output Stack(Stack2)
*
fe+d
)
Stack
68
Infix to Prefix Conversion
Infix Expression
((a+b-c
)
*
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.
“tep 1: Add a left pare theses at the egi i g of P.
Step 2: Scan P from right to left and repeat Step 3 to Step 4
for ea h ele e t of P u til the is e ou tered.
Step 3: If an operand is encountered, push it on to the STACK.
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
4
Stack
82
Evaluation of Prefix Expression
Prefix Expression
(-*5+62/
Result
12
4
Stack
83
Evaluation of Prefix Expression
Prefix Expression
(-*5+62
Result
3
Stack
84
Evaluation of Prefix Expression
Prefix Expression
(-*5+6
Result
2
3
Stack
85
Evaluation of Prefix Expression
Prefix Expression
(-*5+
Result
6
2
3
Stack
86
Evaluation of Prefix Expression
Prefix Expression
(-*5
Result
8
3
Stack
87
Evaluation of Prefix Expression
Prefix Expression
(-*
Result
5
8
3
Stack
88
Evaluation of Prefix Expression
Prefix Expression
(Result
40
3
Stack
89
Evaluation of Prefix Expression
Prefix Expression
(
Result
37
Stack
90
Evaluation of Prefix Expression
Prefix Expression
Result
37
Stack
91
Questions
1. The term "push" and "pop" is related to the
a. Array
b. Lists
c. Stack
d. All of above
c. Stack
92
Questions
2. Which of the following name does not relate to stacks?
a. FIFO lists
b. LIFO list
c. Piles
d. Push-down lists
a. FIFO Lists
93
Questions
3.Which of the following can be an application of stack?
a) Finding factorial
b) Tower of Hanoi
c) Infix to Postfix Conversion
d) All of the above
d)All of the above
94
Questions
4.The data structure which is one ended is ..................
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
7.User perform the following operations on stack size 5,Then
Push(1), pop(), push(2), push(3), pop(),
push(4), pop(), pop(),
push(5)
At the end of last operation, total number of elements present in the
stack is---a)3
b)1
c)2
d)4
b) 1
98
Questions
8.For the following operations on stack of size 5 then
Push(1),
push(4),
pop(),
pop(), push(2),
pop(), pop(),
push(6)
push(3),
push(5),
pop(),
pop(),
Which of the following statement is correct for stack
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
10.Expressions in which operator is written after the operand is called-----a. Infix Expression
b. Prefix Expression
c. Postfix Expression
c. Postfix Expression
101
Questions
11.Stack can not be used to -----a. Implementation of Recursion
b. Evaluation of postfix expression
c. Allocating resources and scheduling
d. Reversing String
c. Allocating resources and scheduling
102
Questions
12.Well formed parentheses can be implemented using
a. list
b. queue
c. hash map
d. stack
d. stack
103
Questions
13.Find the equivalent prefix expression from the following infix
expression
a+b-c/d*(e-f)/g
-+ab/*/cd-efg
104