4. To apply the stack operations for solving
various applications.
5. 1. Classify the various applications of Stack ADT.
(U/C)
2. Convert an infix expression to postfix
expression using Stack Data Structure. (Ap/C)
3. Compute the given postfix expression using
Stack ADT. (Ap/C)
4. Demonstrate backtracking approach using
Stack ADT with examples. (Ap/C)
6.
7. Four common stack applications are:
Reversing data,
Parsing,
Postponing and
Backtracking
8. Reversing - {1 2 3 4} becomes {4 3 2 1}
Examples:
Reversing a list
Convert decimal to binary
11. Pseudocode
1 read (number)
2 loop (number > 0)
1 set digit to number modulo 2
2 print (digit)
3 set number to quotient of number / 2
3 end loop
Input:10
Output:0101 which is reverse of 1010STACK
12.
13. Parsing is any logic that breaks an expression of
data into independent pieces for further
processing.
One common programming problem is
unmatched parentheses in an algebraic
expression.
When parentheses are unmatched, two types
of errors can occur: the opening parenthesis
can be missing or the closing parenthesis can
be missing
14.
15. Solution using STACK:
Whenever we find an opening parentheses in
a program, we push it into the stack.
When we find a closing parentheses, we pop
its matching opening parentheses from the
stack
16.
17. A stack can be useful when the application
requires that the use of data be postponed
for a while.
Examples:
Infix to postfix transformation
Postfix expression evaluation
18. Formats for arithmetic expressions:
1. Infix expression
The operator comes between the operands.
Ex: A+B
2. Prefix expression
The operator comes before the operands.
Ex: +AB
3. Postfix expression
The operator comes after the operands.
Ex: AB+
19. In the infix notation, we need to use
parentheses to control the evaluation
of the operators.
We thus have an evaluation method that
includes parentheses and two operator
priority classes.
Priority 2 : * /
Priority 1 : + -
Priority 0 : (
20. 1. Fully parenthesize the expression using any
explicit parentheses and the arithmetic
precedence.
2. Change all infix notations in each
parenthesis to postfix notation, starting
from the innermost expressions.
Conversion to postfix notation is done by
moving the operator to the location of the
expression’s closing parenthesis.
3. Remove all parentheses.
23. Keep track of operators in the infix expression
If any operand is found, append it to the output
expression
If any operator is found, push it into the stack
such that:
If its priority is higher than the operator at the
top of the stack, go ahead and push it onto the
stack.
If the current operator’s priority is lower than
or equal to that of the operator at the top of
the stack than, then top operator is popped out
of stack and placed in the output expression;
thus the current operator becomes top operator.
24. Conversion of Infix to Postfix
Step 1: Consider the next element in the input.
Step 2: If it is operand, display it.
Step 3: If it is opening parenthesis, insert it on
stack.
Step 4: If it is an operator, then If stack is empty,
insert operator on stack.
If the top of stack is opening parenthesis, insert the
operator on stack
If it has higher priority than the top of stack, insert the
operator on stack.
Else, delete the operator from the stack and display it,
repeat Step 4
25. Step 5: If it is a closing parenthesis, delete
the operator from stack and display them
until an opening parenthesis is encountered.
Delete and discard the opening parenthesis.
Step 6: If there is more input, go to Step
Step 7: If there is no more input, delete the
remaining operators to output.
28. Backtracking is another stack use found in
applications such as computer gaming,
decision analysis, and expert systems.
Examples:
Goal Seeking
Eight Queens Problem
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60. The postfix form of the expression
(A+B)∗(C∗D-E)∗F/G is:
A) AB+CD ∗ E−FG/∗∗
B) AB+CD ∗ E−F∗∗G/
C) AB+CD ∗ E−∗F∗G/
D) AB+CDE∗ −∗F∗G/
67. Applications of stack
Reversing data
Reversing list
Convert decimal to binary
Parsing
Unmatched Parentheses
Postponement
Infix to postfix conversion
Evaluating postfix expression
Backtracking
Goal seeking
Eight queens problem