stack
DESCRIPTION
Basic concept of stackTRANSCRIPT
![Page 1: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/1.jpg)
Prepared By:Harekrushna Patel
Tejas Patel
![Page 2: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/2.jpg)
A Stack is a Linear Data Structure. The Data Items in the stack are inserted and
deleted from one end. That is called Top of the stack.
Stack follows LIFO (Last In First Out) Mechanism.
Stacks are mostly used in system software. Like compilers and Operating Systems, etc..
![Page 3: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/3.jpg)
Insert values into stack is called push operation.
Reading Values from stack is called pop operation.
The Stack starts with position of 0(zero) . The Maximum Position of Stack is n-1.
![Page 4: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/4.jpg)
When The position of Stack is zero. Then, the stack is underflow.
When The position of Stack is grater than n-1. Then, the stack is called overflow.
Once an item is popped from stack, it is no longer available.
![Page 5: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/5.jpg)
The bunch of plates in the kitchen Books on a floor
![Page 6: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/6.jpg)
When TOS=-1 and we try to operate pop operation ‘empty stack’– ‘Underflow’
TOS=-1
![Page 7: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/7.jpg)
Push OperationHere Top Of Stack=-1,Max=10
TOS=-1
![Page 8: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/8.jpg)
Push OperationHere Top Of Stack will become
0,Max=10,To insert some values like 12,
12
TOS=0
![Page 9: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/9.jpg)
Push OperationHere Top Of Stack will become
1,Max=10,To insert some values like 12,34
12 34
TOS=1
![Page 10: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/10.jpg)
Push OperationHere Top Of Stack will become
2,Max=10,To insert some values like 12,34,17
12 34 17
TOS=2
![Page 11: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/11.jpg)
Push OperationHere Top Of Stack will become
3,Max=10,To insert some values like 12,34,17,26
12 34 17 26
TOS=3
![Page 12: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/12.jpg)
Push OperationHere Top Of Stack will become
4,Max=10,To insert some values like 12,34,17,26,46
12 34 17 26 46
TOS=4
![Page 13: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/13.jpg)
Push OperationHere Top Of Stack will become
5,Max=10,To insert some values like 12,34,17,26,46,53
12 34 17 26 46 53
TOS=5
![Page 14: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/14.jpg)
Pop OperationHere Top Of Stack will become
4,Max=10
12 34 17 26 46
TOS=4
![Page 15: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/15.jpg)
Push OperationHere Top Of Stack will become
5,Max=10Let’s insert value say 76
12 34 17 26 46 76
TOS=5
![Page 16: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/16.jpg)
Push OperationHere Top Of Stack will become
6,Max=10Let’s insert value say 76,28
12 34 17 26 46 76 28
TOS=6
![Page 17: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/17.jpg)
Push OperationHere Top Of Stack will become
7,Max=10Let’s insert value say 76,28,39
12 34 17 26 46 76 28 39
TOS=7
![Page 18: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/18.jpg)
• Push OperationHere Top Of Stack will become
8,Max=10Let’s insert value say 76,28,39,20
12 34 17 26 46 76 28 39 20
TOS=8
![Page 19: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/19.jpg)
Push OperationHere Top Of Stack will become
9,Max=10Let’s insert value say 76,28,39,20,9
12 34 17 26 46 76 28 39 20 9
TOS=9
![Page 20: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/20.jpg)
Push OperationHere Top Of Stack will become 10
and Max is also 10Now, When we are inserting element
say 87 ‘stack is full’ Called ‘Overflow’12 34 17 26 46 76 28 39 20 9
TOS=10
![Page 21: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/21.jpg)
Completely parenthesize the infix expression
Move each operator to the space held by the corresponding right parenthesis
Remove all parentheses
![Page 22: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/22.jpg)
infixExp
postfixExp
( a + b - c ) * d – ( e + f )
![Page 23: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/23.jpg)
infixExp
postfixExp
a + b - c ) * d – ( e + f )
(
stackExp
![Page 24: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/24.jpg)
infixExp
postfixExp
+ b - c ) * d – ( e + f )
(
a
stackExp
![Page 25: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/25.jpg)
infixExp
postfixExp
b - c ) * d – ( e + f )
(
a
+
stackExp
![Page 26: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/26.jpg)
infixExp
postfixExp
- c ) * d – ( e + f )
(
a b
+
stackExp
![Page 27: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/27.jpg)
infixExp
postfixExp
c ) * d – ( e + f )
(
a b +
-
stackExp
![Page 28: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/28.jpg)
infixExp
postfixExp
) * d – ( e + f )
(
a b + c
-
stackExp
![Page 29: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/29.jpg)
infixExp
postfixExp
* d – ( e + f )
a b + c -
stackExp
![Page 30: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/30.jpg)
infixExp
postfixExp
d – ( e + f )
a b + c -
*
stackExp
![Page 31: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/31.jpg)
infixExp
postfixExp
– ( e + f )
a b + c - d
*
stackExp
![Page 32: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/32.jpg)
infixExp
postfixExp
( e + f )
a b + c – d *
-
stackExp
![Page 33: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/33.jpg)
infixExp
postfixExp
e + f )
a b + c – d *
-
(
stackExp
![Page 34: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/34.jpg)
infixExp
postfixExp
+ f )
a b + c – d * e
-
(
stackExp
![Page 35: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/35.jpg)
infixExp
postfixExp
f )
a b + c – d * e
-
(
+
stackExp
![Page 36: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/36.jpg)
infixExp
postfixExp
)
a b + c – d * e f
-
(
+
stackExp
![Page 37: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/37.jpg)
infixExp
postfixExp
a b + c – d * e f +
-
stackExp
![Page 38: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/38.jpg)
infixExp
postfixExp
a b + c – d * e f + -
stackExp
![Page 39: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/39.jpg)
Steps:1. Scan the expression from left to right.2. Initialise an empty stack.3. If the scanned character is an operand,
add it to the stack. If the scanned character is an operator, there will be at least two operands in the stack.
![Page 40: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/40.jpg)
4. If the scanned character is an Operator, then we store the top most element of the stack(top Stack) in a variable temp. Pop the stack. Now evaluate top Stack(Operator) temp. Let the result of this operation be ret Val. Pop the stack and Push ret Val into the stack.
Repeat this step till all the characters are scanned.
![Page 41: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/41.jpg)
6. After all characters are scanned, we will have only one element in the stack. Return top Stack.
![Page 42: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/42.jpg)
636+5*9/- // original expression
![Page 43: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/43.jpg)
636+5*9/- // original expression695*9/- // 3+6 evaluated
![Page 44: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/44.jpg)
636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated
![Page 45: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/45.jpg)
636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated65- // 45/9 evaluated
![Page 46: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/46.jpg)
636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated65- // 45/9 evaluated1 // 6-5 evaluated
![Page 47: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/47.jpg)
Steps:1. Scan the expression from right to left.2. An operand is pushed on top of the
stack.3. In case of operator, two operand are
popped from the stack, evaluated and pushed back on to stack.
4. Final result is found on top of stack.
![Page 48: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/48.jpg)
* - + 4 3 5 / + 2 4 3 // original expression
![Page 49: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/49.jpg)
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
![Page 50: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/50.jpg)
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
* 2 / + 2 4 3 // 7-5 evaluated
![Page 51: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/51.jpg)
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
* 2 / + 2 4 3 // 7-5 evaluated* 2 / 6 3 // 2+4 evaluated
![Page 52: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/52.jpg)
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
* 2 / + 2 4 3 // 7-5 evaluated* 2 / 6 3 // 2+4 evaluated* 2 2 // 6/3 evaluated
![Page 53: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/53.jpg)
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
* 2 / + 2 4 3 // 7-5 evaluated* 2 / 6 3 // 2+4 evaluated* 2 2 // 6/3 evaluated 4 // 2*2
evaluated
![Page 54: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/54.jpg)
• Any sort of nesting (such as parentheses)
• Evaluating arithmetic expressions (and other sorts of expression)
• Implementing function or method calls• Keeping track of previous choices (as
in backtracking)• Keeping track of choices yet to be
made (as in creating a maze)
![Page 55: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/55.jpg)
Steps:1. Scan the Infix string from left to right.2. Initialise an empty stack.3. If the scanned character is an operand,
add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
![Page 56: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/56.jpg)
4. If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (top Stack). If top Stack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and top Stack has precedence over the character.
![Page 57: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/57.jpg)
5. Repeat this step till all the characters are scanned.
6. After all characters are scanned, we have to add any character that the stack may have to the Postfix string. If stack is not empty add top Stack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.
![Page 58: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/58.jpg)
Infix
A+BA+B-C
(A+B)*(C-D)A$B*C-D+E/F/(G+H)
((A+B)*C(DE))$(F+G)
A-B/(C*D$E)
Postfix
AB+AB+C-AB+CD-*AB$C*D-EF/GH+/+AB+C*DE--FG+$ABCDE$*/-
![Page 59: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/59.jpg)
Steps:1. Reverse the infix expression.2. Make every ‘(‘ (opening bracket) as ‘)’
(closing bracket) and vice versa.3. Convert the modified expression to
postfix form.4. Reverse the postfix expression.
![Page 60: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/60.jpg)
Infix
A+BA+B-C
(A+B)*(C-D)A$B*C-D+E/F/(G+H)
((A+B)*C(DE))$(F+G)
A-B/(C*D$E)
Prefix
+AB-+ABC*+AB-CD+-*$ABCD//EF+GH$-*+ABC-DE+FG-A/B*C$DE
![Page 61: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/61.jpg)
Goal: Move stack of rings to another peg Rule 1: May move only 1 ring at a time Rule 2: May never have larger ring on
top of smaller ring
![Page 62: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/62.jpg)
A B C
![Page 63: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/63.jpg)
A B C
![Page 64: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/64.jpg)
A B C
![Page 65: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/65.jpg)
A B C
![Page 66: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/66.jpg)
A B C
![Page 67: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/67.jpg)
A B C
![Page 68: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/68.jpg)
A B C
![Page 69: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/69.jpg)
A B C
![Page 70: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/70.jpg)
A B C
![Page 71: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/71.jpg)
A B C
![Page 72: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/72.jpg)
A B C
![Page 73: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/73.jpg)
A B C
![Page 74: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/74.jpg)
A B C
![Page 75: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/75.jpg)
A B C
![Page 76: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/76.jpg)
A B C
![Page 77: Stack](https://reader035.vdocuments.mx/reader035/viewer/2022062418/555e376cd8b42a63048b4a83/html5/thumbnails/77.jpg)
A B C