DS - 3.1Stacks Applications
DS - 3.1Stacks Applications
Stack Applications 1
Algebraic Expressions
Stack Applications 2
Infix, Postfix and Prefix Expressions
• Infix
– Expressions in which operands surround the operators
– Example: A+B-C
Stack Applications 3
Example: Conversion From Infix to Postfix (1)
• Infix: A+B*C
Stack Applications 4
Example: Conversion From Infix to Postfix (2)
• Infix: ( (A+B)*C-(D-E) ) $ (F+G)
Stack Applications 5
Infix, Postfix and Prefix Expressions – Examples
A-B/(C*D^E) ? ?
Stack Applications 6
Why Do We Need Prefix and Postfix? (1)
Stack Applications 7
Why Do We Need Prefix and Postfix? (2)
• Postfix and prefix do not rely on operator priority and are easier to
parse
– No ambiguity and no brackets are required
Stack Applications 8
Conversion of Infix Expression to Postfix
• Precedence function
– prcd(op1, op2)
– op1 and op2 are characters representing operators
• Examples
– prcd(‘*’,’+’) returns TRUE
– prcd(‘+’,’+’) returns TRUE
– prcd(‘+’,’*’) returns FALSE
Stack Applications 9
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 1
0
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ + A +
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 1
1
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ + A +
while (!empty(opstk) ) { B AB +
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 1
2
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ + A +
while (!empty(opstk) ) { B AB +
topsymb = pop(opstk); * AB +*
add topsymb to the postfix string;
} /* end while */
Stack Applications 1
3
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ + A +
while (!empty(opstk) ) { B AB +
topsymb = pop(opstk); * AB +*
add topsymb to the postfix string;
} /* end while */ C ABC +*
Stack Applications 1
4
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ + A +
while (!empty(opstk) ) { B AB +
topsymb = pop(opstk); * AB +*
add topsymb to the postfix string;
} /* end while */ C ABC +*
ABC* +
Stack Applications 1
5
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A+B*C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ + A +
while (!empty(opstk) ) { B AB +
topsymb = pop(opstk); * AB +*
add topsymb to the postfix string;
} /* end while */ C ABC +*
ABC* +
Stack Applications ABC*+ 1
6
Algorithm to Convert Infix to Postfix – Practice
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A*B+C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 17
Algorithm to Convert Infix to Postfix – Practice
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string; Example: A*B+C
} /* end while */
symb Postfix string opstk
push(opstk, symb);
} /* end else */ A A
} /* end while */
/* output any remaining operators */ * A *
while (!empty(opstk) ) { B AB *
topsymb = pop(opstk); + AB* +
add topsymb to the postfix string;
} /* end while */ C AB*C +
AB*C+
Stack Applications 18
What If Expression Contains Parenthesis?
• Precedence function prcd(op1, op2) has to be modified
– prcd( op, ‘(‘ ) = FALSE For any operator op other than ‘)’
– prcd( op, ‘)‘ ) = TRUE For any operator op other than ‘(‘
Stack Applications 19
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ )
push(opstk, symb);
else //pop the parenthesis & discard
it topsymb = pop(opstk);
} /* end else */
} /* end while */
while (!empty(opstk) ) { // remaining
ops topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
0
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard
it topsymb = pop(opstk);
} /* end else */
} /* end while */
while (!empty(opstk) ) { // remaining
ops topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
1
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk);
} /* end else */
} /* end while */
while (!empty(opstk) ) { // remaining
ops topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
2
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk); + A (+
} /* end else */
} /* end while */
while (!empty(opstk) ) { // remaining
ops topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
3
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk); + A (+
} /* end else */ B AB (+
} /* end while */
while (!empty(opstk) ) { // remaining
ops topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
4
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk); + A (+
} /* end else */ B AB (+
} /* end while */
while (!empty(opstk) ) { // remaining ) AB+
ops topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
5
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk); + A (+
} /* end else */ B AB (+
} /* end while */
while (!empty(opstk) ) { // remaining ) AB+
ops topsymb = pop(opstk); * AB+ *
add topsymb to the postfix string;
} /* end while */
Stack Applications 2
6
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk); + A (+
} /* end else */ B AB (+
} /* end while */
while (!empty(opstk) ) { // remaining ) AB+
ops topsymb = pop(opstk); * AB+ *
add topsymb to the postfix string; C AB+C *
} /* end while */
Stack Applications 2
7
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{ topsymb = pop(opstk);
Example: (A+B)*C
add topsymb to the postfix string;
} /* end while */ symb Postfix string opstk
if ( empty(opstk)|| symb != ‘)’ ) ( (
push(opstk, symb);
else //pop the parenthesis & discard A A (
it topsymb = pop(opstk); + A (+
} /* end else */ B AB (+
} /* end while */
while (!empty(opstk) ) { // remaining ) AB+
ops topsymb = pop(opstk); * AB+ *
add topsymb to the postfix string; C AB+C *
} /* end while */
Stack Applications AB+C* 2
8
Conversion of Infix Expression to Postfix – Rules
• Token is an operand
– Append it to the end of postfix string
• Token is a left parenthesis
– Push it on the opstk
• Token is a right parenthesis
– Pop the opstk until the corresponding left parenthesis is removed
– Append each operator to the end of the postfix string
• Token is an operator, *, /, +, or –
– Push it on the opstk
– First remove any operators already on the opstk that have higher or
equal precedence and append them to the postfix string
• Input expression has been completely processed
– Any operators still on the opstk can be removed and appended to the
end of the postfix string
Stack Applications 29
Conversion of Infix Expression to Postfix – Practice
symb Postfix string opstk
3
Stack Applications 0
Conversion of Infix Expression to Postfix – Practice
symb Postfix string opstk
( (
• Example: ((A-(B+C))*D) $ (E+F) ( ((
A A ((
- A ((-
( A ((-(
B AB ((-(
+ AB ((-(+
C ABC ((-(+
) ABC+ ((-
) ABC+- (
* ABC+- (*
D ABC+-D (*
) ABC+-D*
$ ABC+-D* $
( ABC+-D* $(
E ABC+-D*E $(
+ ABC+-D*E $(+
F ABC+-D*EF $(+
) ABC+-D*EF+ $
3
Stack Applications ABC+-D*EF+$ 1
Conversion To Prefix Expression (1)
• Example: (A + B) * (B – C)
Stack Applications 32
Conversion To Prefix Expression (2)
• Example: (A+B^C)*D+E^5
Stack Applications 33
Evaluating a Postfix Expression
opndstk = the empty stack
/* scan the input string reading one element */
/* at a time into symb */
while (not end of input) {
symb = next input character;
if (symb is an operand)
push(opndstk, symb) Each operator in postfix
else { string refers to the previous
/* symb is an operator */ two operands in the string.
opnd2 = pop(opndstk);
opnd1 = pop(opndstk);
value = result of applying symb
to opnd1 and opnd2;
push(opndstk, value);
} /* end else */
} /* end while */
return (pop(opndstk));
Stack Applications 34
Evaluating a Postfix Expression
opndstk = the empty stack
Example Postfix Expression:
/* scan the input string reading one element */ 623+-382/+*2$3+
/* at a time into symb */
while (not end of input) { symb opnd1 opnd2 value opndstk
symb = next input character; 6 6
if (symb is an operand) 2 6,2
push(opndstk, symb) 3 6,2,3
else {
+ 2 3 5 6,5
/* symb is an operator */
- 6 5 1 1
opnd2 = pop(opndstk);
opnd1 = pop(opndstk); 3 6 5 1 1,3
Stack Applications 36