2. 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..
3. 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.
4. 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.
5. The bunch of plates in the kitchen
Books on a floor
6. When TOS=-1 and we try to operate pop
operation ‘empty stack’– ‘Underflow’
TOS=-1
15. Push Operation
Here Top Of Stack will become 5,Max=10
Let’s insert value say
76
12 34 17 26 46 76
TOS=5
16. Push Operation
Here Top Of Stack will become 6,Max=10
Let’s insert value say
76,28
12 34 17 26 46 76 28
TOS=6
17. Push Operation
Here Top Of Stack will become 7,Max=10
Let’s insert value say
76,28,39
12 34 17 26 46 76 28 39
TOS=7
18. • Push Operation
Here Top Of Stack will become 8,Max=10
Let’s insert value say
76,28,39,20
12 34 17 26 46 76 28 39 20
TOS=8
19. Push Operation
Here Top Of Stack will become 9,Max=10
Let’s insert value say
76,28,39,20,9
12 34 17 26 46 76 28 39 20 9
TOS=9
20. Push Operation
Here Top Of Stack will become 10 and Max is
also 10
Now, When we are inserting element say 87
‘stack is full’ Called ‘Overflow’
12 34 17 26 46 76 28 39 20 9
TOS=10
21. Completely parenthesize the infix expression
Move each operator to the space held by the
corresponding right parenthesis
Remove all parentheses
22. infixExp
( a + b - c ) * d – ( e + f )
postfixExp
23. infixExp
a + b - c ) * d – ( e + f )
postfixExp
(
stackExp
24. infixExp
+ b - c ) * d – ( e + f )
postfixExp
(
a
stackExp
25. infixExp
b - c ) * d – ( e + f )
postfixExp
(
a
+
stackExp
26. infixExp
- c ) * d – ( e + f )
postfixExp
(
a b
+
stackExp
27. infixExp
c ) * d – ( e + f )
postfixExp
(
a b +
-
stackExp
28. infixExp
) * d – ( e + f )
postfixExp
(
a b + c
-
stackExp
29. infixExp
* d – ( e + f )
postfixExp
a b + c -
stackExp
30. infixExp
d – ( e + f )
postfixExp
a b + c -
*
stackExp
31. infixExp
– ( e + f )
postfixExp
a b + c - d
*
stackExp
32. infixExp
( e + f )
postfixExp
a b + c – d *
-
stackExp
33. infixExp
e + f )
postfixExp
a b + c – d *
(
-
stackExp
34. infixExp
+ f )
postfixExp
a b + c – d * e
(
-
stackExp
35. infixExp
f )
postfixExp
a b + c – d * e
+
(
-
stackExp
39. 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.
40. 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.
41. 6. After all characters are scanned, we will have
only one element in the stack. Return top Stack.
47. 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.
54. • 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)
55. 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.
56. 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.
57. 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.
59. 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.