Professional Documents
Culture Documents
Algorithm
Stacks
Topics
Example of Stack
Applications
Applications of Stack Data • Reversing a Word
Stack Representation • Evaluation of Arithmetic
Structure expressions
• Infix Notation
• Postfix Notation
Stacks
On popping an
element, we
return the
element
pointed to by
TOP and
reduce its
value.
Stack Representation
One-dimensional array
0
Stack Representation
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
Although stack is a simple data structure to
implement, it is very powerful. The most common
uses of a stack are:
❖ To reverse a word - Put all the letters in a stack
and pop them out. Because of the LIFO order of
stack, you will get the letters in reverse order.
Applications of ❖ In compilers - Compilers use the stack to calculate
Stack Data the value of expressions like 2 + 4 / 5 * (7 - 9) by
converting the expression to prefix or postfix
Structure form.
❖ In browsers - The back button in a browser saves
all the URLs you have visited previously in a stack.
Each time you visit a new page, it is added on top
of the stack. When you press the back button, the
current URL is removed from the stack and the
previous URL is accessed.
Reversing a word is a typical example of
recursion, but it uses stack to temporarily
store an element
Extracting characters one-by-one from the
input string and pushed onto the stack
Stack Example:
Popped off the stack and displayed Reversing a
Word
Stack reverses the order of the characters
because of its last-in-first-out characteristics
Stack Example: Reversing a Word
Evaluation of Expressions
A+B → AB+
A+B+C → AB+C+
A+B*C → ABC*+
Evaluation of Expressions: Infix to Postfix
(( A / B ) / C) * (D + E) -→
AB/
AB/C/
1 2 4 3 AB/C/DE+
AB/C/DE+*
( A + B ) / (C – A ) + D * E -→
AB+
AB+CA-
1 3 2 5 4
AB+CA-/
AB+CA-/DE*
AB+CA-/DE*+
Exercise
1. (A*B+C)/D-E*F
2. P/O+D-S*R
3. J*Q+S-V/B-N
4. H-(A+K)+E*D
5. L/(F-R)+(O*R-(W/D))
Postfix Notation
5. If the scanned character is ‘)’, pop the stack and output it until ‘(‘ is
encountered, and discard both the parenthesis.
Infix Notation
2 + 3 * 4 -1 = 2 + 12 – 1
= 14 – 1
= 13
(4 + 5) * 6 = 9 * 6
= 54
How about infix and postfix notation using algebraic
expression?
Example using algebraic expressions to come up
with an integer value.
Postfix Notation
2 3 4 * + 1 - = 2(12) + 1 -
= (14)1 –
= 13
45+6*= 9*6
= 54
Evaluation of Expressions: Infix to Prefix
(( A / B ) / C) * (D + E) →
/AB
//ABC
1 2 4 3 //ABC+DE
*//ABC+DE
( A + B ) / (C – A ) + D * E →
+AB
+AB-CA
1 3 2 5 4
/+AB-CA
/+AB-CA*DE
+/+AB-CA*DE
Prefix Notation
Converting Infix to Prefix
The Algorithm would be:
1. Reverse the infix expression i.e A+B*C will become C*B+A.
Note while reversing each ‘(‘ will become ‘)’ and each ‘)’
becomes ‘(‘.
2. Obtain the postfix expression of the modified expression
i.e CB*A+.
3. Reverse the postfix expression. Hence in the example prefix
is +A*BC.
Exercise
Convert the following infix notation to prefix notation
1. (A*B+C)/D-E*F
2. P/O+D-S*R
3. J*Q+S-V/B-N
4. H-(A+K)+E*D
5. L/(F-R)+(O*R-(W/D))