stack
DESCRIPTION
Basic concept of stackTRANSCRIPT
Prepared By:Harekrushna Patel
Tejas Patel
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..
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.
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.
The bunch of plates in the kitchen Books on a floor
When TOS=-1 and we try to operate pop operation ‘empty stack’– ‘Underflow’
TOS=-1
Push OperationHere Top Of Stack=-1,Max=10
TOS=-1
Push OperationHere Top Of Stack will become
0,Max=10,To insert some values like 12,
12
TOS=0
Push OperationHere Top Of Stack will become
1,Max=10,To insert some values like 12,34
12 34
TOS=1
Push OperationHere Top Of Stack will become
2,Max=10,To insert some values like 12,34,17
12 34 17
TOS=2
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
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
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
Pop OperationHere Top Of Stack will become
4,Max=10
12 34 17 26 46
TOS=4
Push OperationHere Top Of Stack will become
5,Max=10Let’s insert value say 76
12 34 17 26 46 76
TOS=5
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
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
• 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
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
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
Completely parenthesize the infix expression
Move each operator to the space held by the corresponding right parenthesis
Remove all parentheses
infixExp
postfixExp
( a + b - c ) * d – ( e + f )
infixExp
postfixExp
a + b - c ) * d – ( e + f )
(
stackExp
infixExp
postfixExp
+ b - c ) * d – ( e + f )
(
a
stackExp
infixExp
postfixExp
b - c ) * d – ( e + f )
(
a
+
stackExp
infixExp
postfixExp
- c ) * d – ( e + f )
(
a b
+
stackExp
infixExp
postfixExp
c ) * d – ( e + f )
(
a b +
-
stackExp
infixExp
postfixExp
) * d – ( e + f )
(
a b + c
-
stackExp
infixExp
postfixExp
* d – ( e + f )
a b + c -
stackExp
infixExp
postfixExp
d – ( e + f )
a b + c -
*
stackExp
infixExp
postfixExp
– ( e + f )
a b + c - d
*
stackExp
infixExp
postfixExp
( e + f )
a b + c – d *
-
stackExp
infixExp
postfixExp
e + f )
a b + c – d *
-
(
stackExp
infixExp
postfixExp
+ f )
a b + c – d * e
-
(
stackExp
infixExp
postfixExp
f )
a b + c – d * e
-
(
+
stackExp
infixExp
postfixExp
)
a b + c – d * e f
-
(
+
stackExp
infixExp
postfixExp
a b + c – d * e f +
-
stackExp
infixExp
postfixExp
a b + c – d * e f + -
stackExp
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.
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.
6. After all characters are scanned, we will have only one element in the stack. Return top Stack.
636+5*9/- // original expression
636+5*9/- // original expression695*9/- // 3+6 evaluated
636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated
636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated65- // 45/9 evaluated
636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated65- // 45/9 evaluated1 // 6-5 evaluated
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.
* - + 4 3 5 / + 2 4 3 // original expression
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
* - + 4 3 5 / + 2 4 3 // original expression
* - 7 5 / + 2 4 3 // 4+3 evaluated
* 2 / + 2 4 3 // 7-5 evaluated
* - + 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
* - + 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 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
• 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)
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.
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.
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.
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$*/-
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.
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
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
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C