0% found this document useful (0 votes)
33 views

Stack & Queue Applications: Dept. of Computer Science Faculty of Science and Technology

The document discusses applications of stacks and queues including syntax parsing, expression evaluation, banking transactions, backtracking, and printing jobs. It covers infix, postfix, and prefix notation for algebraic expressions and how operator precedence and associativity are used to evaluate infix expressions. Examples are given for converting infix expressions to postfix and prefix forms using stacks. Methods for checking parentheses matching and evaluating postfix expressions using stacks and queues are also described.

Uploaded by

Rehaan Razib
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Stack & Queue Applications: Dept. of Computer Science Faculty of Science and Technology

The document discusses applications of stacks and queues including syntax parsing, expression evaluation, banking transactions, backtracking, and printing jobs. It covers infix, postfix, and prefix notation for algebraic expressions and how operator precedence and associativity are used to evaluate infix expressions. Examples are given for converting infix expressions to postfix and prefix forms using stacks. Methods for checking parentheses matching and evaluating postfix expressions using stacks and queues are also described.

Uploaded by

Rehaan Razib
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Stack & Queue Applications

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 4.2 Week No: 4 Semester: Fall 20-21


Lecturer: Kaniz Fatema, Assistant Professor, [email protected]
Lecture Outline

1. Applications of Stack & Queue


2. Algebraic Expression
3. Infix, Postfix, Prefix
4. Infix
5. Operator Precedence and Associativity
6. Infix Expression is Hard to Parse
7. Examples of Infix to Postfix & Prefix
8. Parentheses Check Using Stack
9. Converting Postfix Expression Using Stack & Queue
10. Evaluating Postfix Expression Using Stack & Queue
11. Books
2. References
Applications of Stack & Queue
 Syntax parsing, Parenthesis check [Stack]

 Expression evaluation and Expression conversion. [Stack & Queue] [Can also be
achieved using only Stack]

 Banking Transaction View [Stack]


 You view the last transaction first.

 Backtracking and implementation of recursive function, calling function. [Stack]

 Towers of Hanoi [Stack]

 Keeping Track of Printing Jobs [Queue]


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
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.
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.
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 - Wrong
or
23 = 3+(5*4) - Correct

 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.
Operator Precedence and Associativity
Operator Precedence and Associativity
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.
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
A-B/(C*D^E)
(A+B) * (C + D)
=A-B/(C*[DE^])
=(AB+)*(CD+)
=AB+CD+*
=A-B/(CDE^*)
=A-[BCDE^*/]
=ABCDE^*/-
Parentheses Check Using Stack
Using Stack, we can check whether an expression has its parenthesis properly placed; i.e.,
whether its opening and closing parentheses match with each other. For example, let’s
take the expression (x{x[]}x)

We will read the expression as a string and for each character we will do the following
three things:
1. Whenever we get an opening parenthesis, we will push it into the stack.
2. When we get a closing parenthesis we will check that with the top of the stack. If the
top of the stack has the same type of opening parenthesis, we will pop it.
3. We skip the character in the string which is not a parenthesis.

Finally if you have reached the end of the expression and the stack is also empty, that
means the expression is “well formed”. In any other case, the expression is “not well
formed”.
Parentheses Check Using Stack
(x{x[]}x)

We will read the expression as a string and for each character we will do the following
three things:
1. Whenever we get an opening parenthesis, we will push it into the stack.
2. When we get a closing parenthesis we will check that with the top of the stack. If
the top of the stack has the same type of parenthesis but an opening one, we will
pop it.
3. We skip the character in the string which is not a parenthesis.

This expression is
“well formed”
Converting Infix to Postfix
Using Stack & Queue
Infix Expression: 2*6/(4-1)+5*3
Add ')' to the end of Infix; Push( '(' ); Infix 2 * 6 / ( 4 - 1 ) + 5 * 3
do{
OP = next symbol from left of Infix;
if OP is OPERAND then EnQueue( OP );
else if OP is OPERATOR then{ Postfix 2 6 * 4 1 - / 5 3 * +
if OP = '(' then Push( OP );
else if OP = ')' then{
while TopElement() != '(' do{ Stack ( + *
/ ( * -
Enqueue(TopElement());
Pop();
}
Pop();
}else{
while Precedence( OP ) <= Precedence( TopElement() ) do{
Enqueue(TopElement());
Pop(); End ofOPERATOR
OPERAND
Expression
}
Push( OP );
} /
+
* <
-  StackTop(
= )
( +
(
* )
/
}while !IsEmpty();
Evaluating Postfix Expression
Using Stack & Queue
Postfix Expression: 26*41-/53*+
EnQueue( ')' ); Postfix 2 6 * 4 1 - / 5 3 * + )
while ( FrontElement() != ')' ) do{
OP = FrontElement();
DeQueue(); Stack 2
12
19
4 6
15
4
3
5 1
3
if OP is OPERAND then Push( OP );
else if OP is OPERATOR then{
OperandRight = TopElement();
Pop();
OperandLeft = TopElement();
Pop();
x = Evaluate(OperandLeft, OP, OperandRight);
Push(x);
} OPERATOR
OPERAND
}
Result = TopElement();
Pop();
Evaluate(
Evaluate(
Evaluate(
Expression
End of
4,
2,
5,
12,
4,
Expression
‘)‘
'+',
Result
'*',
'-',
'/',15
613=
3 )))=
19
==15
12
3
19
4
cout << Result;
Books

 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard


 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,
References

1. http://www.cs.uregina.ca/Links/class-info/210/Stack/
2. http://www.cs.csi.cuny.edu/~zelikovi/csc326/data/assignment5.htm

You might also like