0% found this document useful (0 votes)
29 views13 pages

Example of Stacks

Uploaded by

emc.javaid2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

Example of Stacks

Uploaded by

emc.javaid2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Example of Stacks

INFIX, POSTFIX and PREFIX


● Infix: A+B-C
● Postfix: AB+C-
● Prefix: -+ABC

Order of Precedence of Operators


● Exponentiation
● Multiplication/Division
● Addition/Subtraction
Infix: ( (A+B)*C-(D-E) ) $ (F+G)
Conversion to Postfix Expression

● ( (AB+)*C-(DE-) ) $ (FG+)
● ( (AB+C*)-(DE-) ) $ (FG+)
● (AB+C*DE--) $ (FG+)
● AB+C*DE- -FG+$

Exercise: Convert the following to Postfix


● ( A + B ) * ( C – D)
● A $ B * C – D + E / F / (G + H)
Conversion to Prefix Expression
The precedence rules for converting an expression from infix to
prefix are identical. The only change from postfix is that the operator
is placed before the operands rather than after them.

Evaluating a Postfix Expression


Each operator in a postfix string refers to the previous two operands
in the string.
Algorithm to Evaluate a Postfix
opndstk = the empty stack Example:
Expression
/* scan the input string reading one
Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +

element */
/* at a time into symb */ symb opnd1 opnd2 value opndstk
while (not end of input) { 6 6

symb = next input character;


2 6,2
if (symb is an operand)
push(opndstk, symb) 3 6,2,3
else {
+ 2 3 5 6,5
/* symb is an operator */
- 6 5 1 1
op2 = pop(opndstk);
3 6 5 1 1,3
op1 = pop(opndstk); 8 6 5 1 1,3,8
value = result of applying symb to op1 2 6 5 1 1,3,8,2
& op2 / 8 2 4 1,3,4
push(opndstk, value); + 3 4 7 1,7
} /* end else */ * 1 7 7 7

} /* end while */ 2 1 7 7 7,2


symb opnd1 opnd2 value opndstk
6 Example: 6
Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
Conversion of Infix Expression to
postfix
A+B*C = ABC*+
(A+B)*C = AB+C*
There must be a precedence function.
prcd(op1, op2), where op1 and op2 are characters representing
operators.
This function returns TRUE if op1 has precedence over op2
when op1 appears to the left of op2 in an infix expression
without parenthesis. prcd(op1,op2) returns FALSE otherwise.
For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas
prcd(‘+’,’*’) is FALSE.

prcd(‘$’,’$’) = FALSE

prcd( ‘(‘ , op) = FALSE for any operator op


Algorithm to Convert Infix to Postfix
opstk = the empty stack;
Example-1: A+B*C
while (not end of input) {
symb = next input character;
if (symb is an operand) symb Postfix string opstk
add symb to the postfix string
A A
else {
while (!empty(opstk) && + A +
prcd(stacktop(opstk),symb) )
{ B AB +
topsymb = pop(opstk); * AB +*
add topsymb to the postfix string; C ABC +*
} /* end while */ ABC* +

push(opstk, symb); ABC*+


} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
Algorithm to Convert Infix to Postfix
Example-2: (A+B)*C
opstk = the empty stack;
while (not end of input) {
symb = next input character;
symb Postfix string opstk
if (symb is an operand)
add symb to the postfix string
else { ( (
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) A A (
{ + A (+
topsymb = pop(opstk); B AB (+
add topsymb to the postfix string;
) AB+
} /* end while */

* AB+ *
push(opstk, symb);
} /* end else */ C AB+C *
AB+C*
} /* end while */
Algorithm to Convert Infix to Postfix
Example-2: (A+B)*C
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand) symb Postfix opstk
add symb to the postfix string string
else { ( (
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) A A (
{
+ A (+
topsymb = pop(opstk);
add topsymb to the postfix string; B AB (+
} /* end while */ ) AB+

If (empty(opstk) || symb != ‘)’ ) * AB+ *


push(opstk, symb);
C AB+C *
else
topsymb = pop(opstk); AB+C*
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) )
{ Example-3: ( (A-(B+C) ) *D ) $ (E+F)
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */

If (empty(opstk) || symb != ‘)’ ) prcd(‘$’,’$’) = FALSE


push(opstk, symb);
prcd( ‘(‘ , op) = FALSE for any operator op
else
topsymb = pop(opstk); prcd( op, ‘(‘ ) = FALSE for any op other than
‘)’
} /* end else */
prcd( op, ‘)‘ ) = TRUE for any op other than ‘(‘
Algorithm to Convert Infix to Postfix
opstk = the empty stack; Example-3: ( (A-(B+C) ) *D ) $ (E+F)
while (not end of input) { symb Postfix string opstk
symb = next input character; ( (

if (symb is an operand)
add symb to the postfix string ( ((

else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) ) A A ((

{ - A ((-

topsymb = pop(opstk); ( A ((-(

add topsymb to the postfix string; B AB ((-(


+ AB ((-(+
} /* end while */
C ABC ((-(+
) ABC+ ((-
If (empty(opstk) || symb != ‘)’ )
) ABC+- (
push(opstk, symb);
* ABC+- (*
else
D ABC+-D (*
topsymb = pop(opstk);
) ABC+-D*

} /* end else */
$ ABC+-D* $
( ABC+-D* $(
} /* end while */
E ABC+-D*E $(
Algorithm to Convert Infix to Postfix
Example-4: ( A + B ) * ( C – D)
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand) symb Postfix string opstk
add symb to the postfix string
else {
while (!empty(opstk) && prcd(stacktop(opstk),symb) )
{
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */

If (empty(opstk) || symb != ‘)’ )


push(opstk, symb);
else
topsymb = pop(opstk);

} /* end else */

} /* end while */
Factorial of a number
● Write function to calculate the factorial of a
number.
○ Iterative definition
○ Recursive

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy