SlideShare une entreprise Scribd logo
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
What is a Queue?
• Like stacks, queues are lists. With a queue, however,
insertion is done at one end whereas deletion is done
at the other end.
• Queues implement the FIFO (first-in first-out) policy.
E.g., a printer/job queue!
• Two basic operations of queues:
– dequeue: remove an item/element from front
– enqueue: add an item/element at the back
dequeue enqueue
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Queue ADT
• Queues implement the FIFO (first-in first-out) policy
– An example is the printer/job queue!
enqueue(o)
dequeue()
isEmpty()
getFront() createQueue()
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Sample Operation
Queue *Q;
enqueue(Q, “a”);
enqueue(Q, “b”);
enqueue(Q, “c”);
d=getFront(Q);
dequeue(Q);
enqueue(Q, “e”);
dequeue(Q);
q
front back
a b c e
d
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Queue ADT interface
• The main functions in the Queue ADT are (Q is the
queue)
void enqueue(o, Q) // insert o to back of Q
void dequeue(Q); // remove oldest item
Item getFront(Q); // retrieve oldest item
boolean isEmpty(Q); // checks if Q is empty
boolean isFull(Q); // checks if Q is full
void clear(Q); // make Q empty
}
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Implementation of Queue (Array)
• use Array with front and back pointers as implementation of queue
Queue
arr 0 1 7 8 9
2 3 4 5 6
A B C D E F G
front
back
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Circular Array
• To implement queue, it is best to view arrays as circular structure
0 1 7 8 9
2 3 4 5 6
A B C D E F G
front
back
front
back
A
B
C
D
E
F
G
0
1
7
8
9
2
3
4
5
6
Circular view of arrays.
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
How to Advance
• Both front & back pointers should make advancement until they reach end
of the array. Then, they should re-point to beginning of the array
front = adv(front);
back = adv(back);
int adv(int p)
{ return ((p+1) % maxsize);
}
Alternatively, use modular arithmetic:
mod operator
int adv(int p)
{ int r = p+1;
if (r<maxsize) return r;
else return 0;
}
upper bound of the array
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Sample
Queue *Q;
enqueue(Q, “a”);
enqueue(Q, “b”);
enqueue(Q, “c”);
dequeue(Q);
dequeue(Q);
enqueue(Q, “d”);
enqueue(Q, “e”);
dequeue(Q);
a
Q
F=front
B=back
F
B
b c d
F
B B B
F F
B B
e
© Oxford University Press 2014. All rights reserved.
5/7/2023 CS 201
Checking for Full/Empty State
What does (F==B) denote?
F
B
Queue
Empty
State
c d
e
B
F
f Queue
Full
State
size 0 size 4
c d
e
B F
Alternative - Leave a Deliberate Gap!
No need for size field.
Full Case : (adv(B)==F)
© Oxford University Press 2014. All rights reserved.
Infix Notation
• Infix, Postfix and Prefix notations are three different but equivalent
notations of writing algebraic expressions.
• While writing an arithmetic expression using infix notation, the
operator is placed between the operands. For example, A+B; here,
plus operator is placed between the two operands A and B.
• Although it is easy to write expressions using infix notation,
computers find it difficult to parse as they need a lot of information
to evaluate the expression.
• Information is needed about operator precedence, associativity
rules, and brackets which overrides these rules.
• So, computers work more efficiently with expressions written using
prefix and postfix notations.
© Oxford University Press 2014. All rights reserved.
Postfix Notation
• Postfix notation was given by Jan Łukasiewicz who was a Polish
logician, mathematician, and philosopher. His aim was to develop
a parenthesis-free prefix notation (also known as Polish notation)
and a postfix notation which is better known as Reverse Polish
Notation or RPN.
• In postfix notation, the operator is placed after the operands. For
example, if an expression is written as A+B in infix notation, the
same expression can be written as AB+ in postfix notation.
• The order of evaluation of a postfix expression is always from left
to right.
© Oxford University Press 2014. All rights reserved.
Postfix Notation
• The expression (A + B) * C is written as:
AB+C* in the postfix notation.
• A postfix operation does not even follow the rules of operator
precedence. The operator which occurs first in the expression is
operated first on the operands.
• No parenthese
• For example, given a postfix notation AB+C*. While evaluation,
addition will be performed prior to multiplication.
© Oxford University Press 2014. All rights reserved.
Prefix Notation
• In a prefix notation, the operator is placed before the operands.
• For example, if A+B is an expression in infix notation, then the
corresponding expression in prefix notation is given by +AB.
• While evaluating a prefix expression, the operators are applied to
the operands that are present immediately on the right of the
operator.
• Prefix expressions also do not follow the rules of operator
precedence, associativity, and even brackets cannot alter the
order of evaluation.
• The expression (A + B) * C is written as:
*+ABC in the prefix notation
© Oxford University Press 2014. All rights reserved.
Evaluation of an Infix Expression
STEP 1: Convert the infix expression into its equivalent postfix expression
Algorithm to convert an Infix notation into postfix notation
Step 1: Add ‘)” to the end of the infix expression
Step 2: Push “(“ on to the stack
Step 3: Repeat until each character in the infix notation is scanned
IF a “(“ is encountered, push it on the stack
IF an operand (whether a digit or an alphabet) is encountered,
add it to the postfix expression.
IF a “)” is encountered, then;
a. Repeatedly pop from stack and add it to the postfix expression
until a “(” is encountered.
b. Discard the “(“. That is, remove the “(“ from stack and do not
add it to the postfix expression
IF an operator X is encountered, then;
a Repeatedly pop from stack and add each operator (popped from the
stack) to the postfix expression which has the same precedence or a
higher precedence than X
b. Push the operator X to the stack
Step 4: Repeatedly pop from the stack and add it to the postfix expression
until the stack is empty
Step 5: EXIT
© Oxford University Press 2014. All rights reserved.
Evaluation of an Infix Expression
STEP 2: Evaluate the postfix expression
Algorithm to evaluate a postfix expression
Step 1: Add a “)” at the end of the postfix expression
Step 2: Scan every character of the postfix expression and
repeat
steps 3 and 4 until “)”is encountered
Step 3: IF an operand is encountered, push it on the stack
IF an operator X is encountered, then
a. pop the top two elements from the stack as A and B
b. Evaluate B X A, where A was the topmost element
and B was
the element below A.
c. Push the result of evaluation on the stack
[END OF IF]
Step 4: SET RESULT equal to the topmost element of the stack
Step 5: EXIT
© Oxford University Press 2014. All rights reserved.
Evaluation of an Infix Expression
• Example: evaluate “9 - (( 3 * 4) + 8) / 4”.
• Step 1 infix “(9 - (( 3 * 4) + 8) / 4)" => postfix “9 3 4 * 8 + 4 / -“
• Step 2 evaluate “9 3 4 * 8 + 4 / -“
Character
scanned
Stack
9 9
3 9, 3
4 9, 3, 4
* 9, 12
8 9, 12, 8
+ 9, 20
4 9, 20, 4
/ 9, 5
- 4
infix Stack postfix
( (
9 ( 9
- ( - 9
( (-( 9
( (-(( 9
3 (-(( 9 3
* (-((* 9 3
4 (-((* 9 3 4
) (-( 9 3 4 *
+ (-(+ 9 3 4 *
8 (-(+ 9 3 4 * 8
) (- 9 3 4 * 8 +
/ (-/ 9 3 4 * 8 +
4 (-/ 9 3 4 * 8 + 4
) 9 3 4 * 8 + 4 / -
© Oxford University Press 2014. All rights reserved.
Convert Infix Expression into Prefix Expression
Consider an infix expression: (A – B / C) * (A / K – L)
• Step 1: Reverse the infix string. Note that while reversing the string
you must interchange left and right parenthesis.
(L – K / A) * (C / B – A)
• Step 2: Obtain the corresponding postfix expression of the infix
expression obtained as a result of Step 1.
• The expression is: (L – K / A) * (C / B – A)
• Therefore, [L – (K A /)] * [ (C B /) - A ]
= [LKA/-] * [ CB/A-]
= L K A / - C B / A - *
• Step 3: Reverse the postfix expression to get the prefix expression
• Therefore, the prefix expression is * - A / B C - / A K L

Contenu connexe

Similaire à Lect-5 & 6.pptx

Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
ecomputernotes
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
KALPANAC20
 

Similaire à Lect-5 & 6.pptx (20)

DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
Infix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdfInfix to Postfix Conversion.pdf
Infix to Postfix Conversion.pdf
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Polish
PolishPolish
Polish
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 

Dernier

一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
Introduction-to-Cybersecurit57hhfcbbcxxx
Introduction-to-Cybersecurit57hhfcbbcxxxIntroduction-to-Cybersecurit57hhfcbbcxxx
Introduction-to-Cybersecurit57hhfcbbcxxx
zahraomer517
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
MAQIB18
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 

Dernier (20)

一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?
 
Uber Ride Supply Demand Gap Analysis Report
Uber Ride Supply Demand Gap Analysis ReportUber Ride Supply Demand Gap Analysis Report
Uber Ride Supply Demand Gap Analysis Report
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
Introduction-to-Cybersecurit57hhfcbbcxxx
Introduction-to-Cybersecurit57hhfcbbcxxxIntroduction-to-Cybersecurit57hhfcbbcxxx
Introduction-to-Cybersecurit57hhfcbbcxxx
 
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPsWebinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
Using PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBUsing PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDB
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 

Lect-5 & 6.pptx

  • 1. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 What is a Queue? • Like stacks, queues are lists. With a queue, however, insertion is done at one end whereas deletion is done at the other end. • Queues implement the FIFO (first-in first-out) policy. E.g., a printer/job queue! • Two basic operations of queues: – dequeue: remove an item/element from front – enqueue: add an item/element at the back dequeue enqueue
  • 2. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Queue ADT • Queues implement the FIFO (first-in first-out) policy – An example is the printer/job queue! enqueue(o) dequeue() isEmpty() getFront() createQueue()
  • 3. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Sample Operation Queue *Q; enqueue(Q, “a”); enqueue(Q, “b”); enqueue(Q, “c”); d=getFront(Q); dequeue(Q); enqueue(Q, “e”); dequeue(Q); q front back a b c e d
  • 4. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Queue ADT interface • The main functions in the Queue ADT are (Q is the queue) void enqueue(o, Q) // insert o to back of Q void dequeue(Q); // remove oldest item Item getFront(Q); // retrieve oldest item boolean isEmpty(Q); // checks if Q is empty boolean isFull(Q); // checks if Q is full void clear(Q); // make Q empty }
  • 5. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Implementation of Queue (Array) • use Array with front and back pointers as implementation of queue Queue arr 0 1 7 8 9 2 3 4 5 6 A B C D E F G front back
  • 6. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Circular Array • To implement queue, it is best to view arrays as circular structure 0 1 7 8 9 2 3 4 5 6 A B C D E F G front back front back A B C D E F G 0 1 7 8 9 2 3 4 5 6 Circular view of arrays.
  • 7. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 How to Advance • Both front & back pointers should make advancement until they reach end of the array. Then, they should re-point to beginning of the array front = adv(front); back = adv(back); int adv(int p) { return ((p+1) % maxsize); } Alternatively, use modular arithmetic: mod operator int adv(int p) { int r = p+1; if (r<maxsize) return r; else return 0; } upper bound of the array
  • 8. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Sample Queue *Q; enqueue(Q, “a”); enqueue(Q, “b”); enqueue(Q, “c”); dequeue(Q); dequeue(Q); enqueue(Q, “d”); enqueue(Q, “e”); dequeue(Q); a Q F=front B=back F B b c d F B B B F F B B e
  • 9. © Oxford University Press 2014. All rights reserved. 5/7/2023 CS 201 Checking for Full/Empty State What does (F==B) denote? F B Queue Empty State c d e B F f Queue Full State size 0 size 4 c d e B F Alternative - Leave a Deliberate Gap! No need for size field. Full Case : (adv(B)==F)
  • 10. © Oxford University Press 2014. All rights reserved. Infix Notation • Infix, Postfix and Prefix notations are three different but equivalent notations of writing algebraic expressions. • While writing an arithmetic expression using infix notation, the operator is placed between the operands. For example, A+B; here, plus operator is placed between the two operands A and B. • Although it is easy to write expressions using infix notation, computers find it difficult to parse as they need a lot of information to evaluate the expression. • Information is needed about operator precedence, associativity rules, and brackets which overrides these rules. • So, computers work more efficiently with expressions written using prefix and postfix notations.
  • 11. © Oxford University Press 2014. All rights reserved. Postfix Notation • Postfix notation was given by Jan Łukasiewicz who was a Polish logician, mathematician, and philosopher. His aim was to develop a parenthesis-free prefix notation (also known as Polish notation) and a postfix notation which is better known as Reverse Polish Notation or RPN. • In postfix notation, the operator is placed after the operands. For example, if an expression is written as A+B in infix notation, the same expression can be written as AB+ in postfix notation. • The order of evaluation of a postfix expression is always from left to right.
  • 12. © Oxford University Press 2014. All rights reserved. Postfix Notation • The expression (A + B) * C is written as: AB+C* in the postfix notation. • A postfix operation does not even follow the rules of operator precedence. The operator which occurs first in the expression is operated first on the operands. • No parenthese • For example, given a postfix notation AB+C*. While evaluation, addition will be performed prior to multiplication.
  • 13. © Oxford University Press 2014. All rights reserved. Prefix Notation • In a prefix notation, the operator is placed before the operands. • For example, if A+B is an expression in infix notation, then the corresponding expression in prefix notation is given by +AB. • While evaluating a prefix expression, the operators are applied to the operands that are present immediately on the right of the operator. • Prefix expressions also do not follow the rules of operator precedence, associativity, and even brackets cannot alter the order of evaluation. • The expression (A + B) * C is written as: *+ABC in the prefix notation
  • 14. © Oxford University Press 2014. All rights reserved. Evaluation of an Infix Expression STEP 1: Convert the infix expression into its equivalent postfix expression Algorithm to convert an Infix notation into postfix notation Step 1: Add ‘)” to the end of the infix expression Step 2: Push “(“ on to the stack Step 3: Repeat until each character in the infix notation is scanned IF a “(“ is encountered, push it on the stack IF an operand (whether a digit or an alphabet) is encountered, add it to the postfix expression. IF a “)” is encountered, then; a. Repeatedly pop from stack and add it to the postfix expression until a “(” is encountered. b. Discard the “(“. That is, remove the “(“ from stack and do not add it to the postfix expression IF an operator X is encountered, then; a Repeatedly pop from stack and add each operator (popped from the stack) to the postfix expression which has the same precedence or a higher precedence than X b. Push the operator X to the stack Step 4: Repeatedly pop from the stack and add it to the postfix expression until the stack is empty Step 5: EXIT
  • 15. © Oxford University Press 2014. All rights reserved. Evaluation of an Infix Expression STEP 2: Evaluate the postfix expression Algorithm to evaluate a postfix expression Step 1: Add a “)” at the end of the postfix expression Step 2: Scan every character of the postfix expression and repeat steps 3 and 4 until “)”is encountered Step 3: IF an operand is encountered, push it on the stack IF an operator X is encountered, then a. pop the top two elements from the stack as A and B b. Evaluate B X A, where A was the topmost element and B was the element below A. c. Push the result of evaluation on the stack [END OF IF] Step 4: SET RESULT equal to the topmost element of the stack Step 5: EXIT
  • 16. © Oxford University Press 2014. All rights reserved. Evaluation of an Infix Expression • Example: evaluate “9 - (( 3 * 4) + 8) / 4”. • Step 1 infix “(9 - (( 3 * 4) + 8) / 4)" => postfix “9 3 4 * 8 + 4 / -“ • Step 2 evaluate “9 3 4 * 8 + 4 / -“ Character scanned Stack 9 9 3 9, 3 4 9, 3, 4 * 9, 12 8 9, 12, 8 + 9, 20 4 9, 20, 4 / 9, 5 - 4 infix Stack postfix ( ( 9 ( 9 - ( - 9 ( (-( 9 ( (-(( 9 3 (-(( 9 3 * (-((* 9 3 4 (-((* 9 3 4 ) (-( 9 3 4 * + (-(+ 9 3 4 * 8 (-(+ 9 3 4 * 8 ) (- 9 3 4 * 8 + / (-/ 9 3 4 * 8 + 4 (-/ 9 3 4 * 8 + 4 ) 9 3 4 * 8 + 4 / -
  • 17. © Oxford University Press 2014. All rights reserved. Convert Infix Expression into Prefix Expression Consider an infix expression: (A – B / C) * (A / K – L) • Step 1: Reverse the infix string. Note that while reversing the string you must interchange left and right parenthesis. (L – K / A) * (C / B – A) • Step 2: Obtain the corresponding postfix expression of the infix expression obtained as a result of Step 1. • The expression is: (L – K / A) * (C / B – A) • Therefore, [L – (K A /)] * [ (C B /) - A ] = [LKA/-] * [ CB/A-] = L K A / - C B / A - * • Step 3: Reverse the postfix expression to get the prefix expression • Therefore, the prefix expression is * - A / B C - / A K L