0% found this document useful (0 votes)
14 views10 pages

Stack Presentation

1) The document discusses stacks and their applications such as syntax parsing, expression evaluation, backtracking, and inventory systems. 2) It explains infix, postfix, and prefix notation for algebraic expressions and how stacks can be used to convert between these notations. 3) As an example, it shows the steps to convert an infix expression to postfix notation using a stack and the operator precedence rules.

Uploaded by

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

Stack Presentation

1) The document discusses stacks and their applications such as syntax parsing, expression evaluation, backtracking, and inventory systems. 2) It explains infix, postfix, and prefix notation for algebraic expressions and how stacks can be used to convert between these notations. 3) As an example, it shows the steps to convert an infix expression to postfix notation using a stack and the operator precedence rules.

Uploaded by

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

CSC 2105::DATA STRUCTURE

STACK APPLICATION
Dipta Gomes
Lecturer, Department of Computer Science
Faculty of Science & Information Technology
American International University-Bangladesh (AIUB)
[email protected]
APPLICATIONS OF STACK
 Syntax parsing, Expression evaluation and Expression conversion.
 Banking Transaction View
 You view the last transaction first.
 Backtracking and implementation of recursive function, calling function.
 Towers of Hanoi
 Inventory Systems like Issuing students the Multi-meters… you will be issued
the most recently returned item likely

Fahad Ahmed CSC 2015: Data Structures Stack Application  2


ALGEBRAIC EXPRESSION
 An algebraic expression is a legal combination of operands and the operators.
 Operand is the quantity (unit of data) on which a mathematical operation is
performed.
 Operand may be a variable like x,y,z or a constant like 5,4,0,9,1 etc.
 Operator is a symbol which signifies a mathematical or logical operation
between the operands. Example of familiar operators include +,-,*,/,^,%
 Considering these definitions of operands and operators now we can write an
example of expression as: x + y * z

Fahad Ahmed CSC 2015: Data Structures Stack Application  3


INFIX, POSTFIX AND PREFIX
EXPRESSIONS
 INFIX: The expressions in which operands surround the operator, i.e. operator
is in between the operands. e.g. x+y, 6*3 etc. The infix notation is the
general way we write an expression.
 POSTFIX: Also Known as Reverse Polish Notation (RPN). The operator comes
after the operands, i.e. operator comes post of the operands, so the name
postfix. e.g. xy+, xyz+* etc.
 PREFIX: Also Known as Polish notation. The operator comes before the
operands, i.e. operator comes pre of the operands, so the name prefix. e.g.
+xy, *+xyz etc.

Fahad Ahmed CSC 2015: Data Structures Stack Application  4


INFIX
 To our surprise INFIX notations are not as simple as they seem specially while
evaluating them. To evaluate an infix expression we need to consider
Operators’ Precedence and Associative property
 For example expression 3+5*4 evaluate to
32 = (3+5)*4 or
23 = 3+(5*4)
 Operator precedence and associativity governs the evaluation order of an
expression.
 An operator with higher precedence is applied before an operator with lower precedence.
 Same precedence order operator is evaluated according to their associativity order.

Fahad Ahmed CSC 2015: Data Structures Stack Application  5


OPERATOR PRECEDENCE AND
ASSOCIATIVITY
Precedence Operator Associativity Precedence Operator Associativity
1 :: Left-to-right 9 < > <= >= Left-to-right
2 () [] . -> ++ -- Left-to-right 10 == != Left-to-right
++ -- ~ ! sizeof new
delete 11 & Left-to-right
3 Right-to-left 12 ^ Left-to-right
*&
+- 13 | Left-to-right
4 (type) Right-to-left 14 && Left-to-right
5 .* ->* Left-to-right
15 || Left-to-right
6 */% Left-to-right
7 +- Left-to-right 16 ?: Right-to-left
8 << >> Left-to-right = *= /= %= += -=
17 Right-to-left
>>= <<= &= ^= |=
18 , Left-to-right

Fahad Ahmed CSC 2015: Data Structures Stack Application  6


INFIX EXPRESSION IS HARD TO PARSE
 Need operator priorities, tie breaker, and delimiters.
 This makes the evaluation of expression more difficult than is necessary for the
processor.
 Both prefix and postfix notations have an advantage over infix that while
evaluating an expression in prefix or postfix form we need not consider the
Precedence and Associative property.
 The expression is scanned from user in infix form; it is converted into prefix or
postfix form and then evaluated without considering the parenthesis and
priority of the operators.
 So, it is easier (complexity wise) for the processor to evaluate expressions that
are in these forms.
Fahad Ahmed CSC 2015: Data Structures Stack Application  7
EXAMPLES OF INFIX TO PREFIX AND
POSTFIX
Infix PostFix Prefix
A+B AB+ +AB
(A+B) * (C + D) AB+CD+* *+AB+CD
A-B/(C*D^E) ABCDE^*/- -A/B*C^DE
A- B/ ( C* D^ E ) ABCDE ^ * / - - A/ B* C^ DE
A- B/ ( C* F ) ABCF * / - D^ E - A/ B* CF D^ E
A- B/ G ABG/ - C* F - A/ BG C* F
A- H
AH- B/ G - AH B/ G
I
I A- H I A- H

Fahad Ahmed CSC 2015: Data Structures Stack Application  8


Infix Expression: 2*6/(4-1)+5*3
Add ')' to the end of Infix; Push( '(' );
CONVERTING INFIX TO POSTFIX
do{
OP = next symbol from left of Infix;
Infix 2 * 6 / ( 4 - 1 ) + 5 * 3 )
if OP is OPERAND then EnQueue( OP );
else if OP is OPERATOR then{
if OP = '(' then Push( OP ); Postfix 2 6 * 4 1 - / 5 3 * +
else if OP = ')' then{
Pop( StackOperator );
while StackOperator != '(' do{ Stack ( +
*
/ (
* -
Enqueue( Stackoperator );
Pop( StackOperator );
}`
}else{
while Precedence( OP ) <= Precedence( TopElement( ) ) do{ End of
OPERATOR
OPERAND
Expression
Pop( StackOperator );
Enqueue( StackOperator );
/
+  StackTop(
* <
- = )
( +
(
* )
/
}
Push( OP );
}
Fahad Ahmed
}while !IsEmpty( ); CSC 2015: Data Structures Stack Application  9
EVALUATING POSTFIX
Postfix 2 6 * 4 1 - / 5 3 * + )
EXPRESSION
Postfix Expression: 26*41-/53*+
Stack 2
19
12 6
4 15
4
3
5 1
3
EnQueue( ')' );
while ( FrontElement( ) != ')' ) do{
DeQueue( OP ); OPERATOR
OPERAND
if OP is OPERAND then Push( OP );
else if OP is OPERATOR then{
Evaluate(
Evaluate(
Evaluate(
Expression
End of
4,
2,
5,
12,
4,
Expression
‘)‘
'+',
Result
'*',
'-',
'/',15
613=
3 )))19
===15
12
19
3
4
Pop( OperandRight );
Pop( OperandLeft );
Push( Evaluate( OperandLeft, OP, OperandRight ) );
}
}
Pop( Result );
cout << Result;
}

Fahad Ahmed CSC 2015: Data Structures Stack Application  10

You might also like