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

Stack 1

This document discusses stacks and their applications. It provides examples of converting infix notation arithmetic expressions to postfix notation using a stack-based algorithm. It also demonstrates evaluating a postfix expression using a stack by pushing operands and popping to apply operators. The document includes pseudocode and step-by-step examples for infix to postfix conversion and postfix evaluation. It proposes exercises for converting infix to postfix, evaluating infix and postfix notations using stacks, and an algorithm for multi-digit number arithmetic.

Uploaded by

Mustafa Adil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Stack 1

This document discusses stacks and their applications. It provides examples of converting infix notation arithmetic expressions to postfix notation using a stack-based algorithm. It also demonstrates evaluating a postfix expression using a stack by pushing operands and popping to apply operators. The document includes pseudocode and step-by-step examples for infix to postfix conversion and postfix evaluation. It proposes exercises for converting infix to postfix, evaluating infix and postfix notations using stacks, and an algorithm for multi-digit number arithmetic.

Uploaded by

Mustafa Adil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ata Structures

Introduction to Stacks
Introduction to Stacks
2

 What is a Stack?

 Stack implementation using array.

 Stack implementation using structure.

 Applications of Stacks.
3
Applications of Stacks
 Some direct applications:
 Conversion of tail-recursive algorithms to iterative ones
 Evaluation of arithmetic expressions by compilers [infix
to postfix conversion, infix to prefix conversion,
evaluation of postfix expressions]

 Some indirect applications


 Auxiliary data structure for some algorithms
 Example: Converting a decimal number to another base
 Component of other data structures
 Example: In this course we will use a stack to implement a
Tree iterate
Application of Stacks - Evaluating Postfix Expressions
4

(5+9)*2+6*5
 An ordinary arithmetical expression like the above
is called infix-expression - binary operators appear
in between their operands.
 The order of operations evaluation is determined
by the precedence rules and parentheses.
 When an evaluation order is desired that is
different from that provided by the precedence,
parentheses are used to override precedence rules.
Application of Stacks - Evaluating Postfix Expressions (Cont’d)
5

 Expressions can also be represented using postfix


notation - where an operator comes after its two
operands or prefix notation – where an operator
comes before its two operands.

 The advantage of postfix and prefix notations is that


the order of operation evaluation is unique without
the need for precedence rules or parentheses.

Infix Notation Postfix (Reverse Polish) Notation Prefix (Polish) Notation


16 / 2 16 2 / / 16 2
(2 + 14)* 5 2 14 + 5 * * + 2 14 5
2 + 14 * 5 2 14 5 * + + * 2 14 5
(6 – 2) * (5 + 4) 6 2 - 5 4 +* * - 6 2 + 5 4
Infix to Postfix conversion (manual)
6
 An Infix to Postfix manual conversion algorithm is:
1. Completely parenthesize the infix expression according to order of
priority you want.
2. Move each operator to its corresponding right parenthesis.
3. Remove all parentheses.

 Examples:
3+4*5 (3 + (4 * 5) ) 345*+

a/b^c–d*e–a*c^3^4 abc^/de*ac34^^*--

((a / (b ^ c)) – ((d * e) – (a * (c ^ (3 ^ 4) ) ) ) )


Infix to Prefix conversion (manual)
7
 An Infix to Prefix manual conversion algorithm is:
1. Completely parenthesize the infix expression according to order of
priority you want.
2. Move each operator to its corresponding left parenthesis.
3. Remove all parentheses.
 Examples:
3+4*5 (3 + (4 * 5) ) +3*4 5

a/b^c–d*e–a*c^3^4 -/a^b c-* d e*a^c^3 4

( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) )
 The following
Application algorithmPostfix
of Stacks - Evaluating usesExpression
a stack to evaluate
(Cont’d) a
postfix expressions.

Start with an empty stack


for (each item in the expression) {
if (the item is an operand)
Push the operand onto the stack
else if (the item is an operator operator X){
Pop operand1 from the stack
Pop operand2 from the stack
result = operand2 operator X operand1
Push the result onto the stack
}
}
Pop the only operand from the stack: this is the result of the evaluation
Application of Stacks - Evaluating Postfix Expression (Cont’d)
9

 Example: Consider the postfix expression, 2 10 + 9 6 - /, which is


(2 + 10) / (9 - 6) in infix, the result of which is 12 / 3 = 4.
 The following is a trace of the postfix evaluation algorithm for the
postfix expression:
//Application of Stacks – Infix to Postfix Conversion

10
postfixString = “”;
while(infixString has tokens){
Get next token x;
if(x is operand)
Append x to postfixString;
else if(x is ‘(’ )
stack.push(x);
else if(x is ‘)’ ){
y = stack.pop();
while(y is not ‘(’ ){
Append y to postfixString;
y = stack.pop();
}
discard both ‘(‘ and ‘)’;
}
//Application of Stacks – Infix to Postfix Conversion

11
else if(x is operator){
while(stack is not empty){
y = stack.getTop(); // top value is not removed from stack
if(y is ‘(‘ ) break;
if(y has low precedence than x) break;
if(y is right associative with equal precedence to x) break;
y = stack.pop();
Append y to postfixString;
}
push x;
}
while(stack is not empty){
y = stack.pop( );
Append y to postfixString;
}
Application of Stacks – Infix to Postfix Conversion (cont’d)
12
step1: step2:

step3: step4:
Application of Stacks – Infix to Postfix Conversion (cont’d)
13
step5: step6:

step7: step8:
Application of Stacks – Infix to Postfix Conversion (cont’d)

14
step9: step10:

step11: step12:
Application of Stacks – Infix to Postfix Conversion (cont’d)
15
step13: step14:

step15: step16:
Application of Stacks – Infix to Postfix Conversion (cont’d)
16

step17: step18:
Application of Stacks – Infix to Prefix Conversion
17
An infix to prefix conversion algorithm:
1. Reverse the infix string
2. Perform the infix to postfix algorithm on the reversed string
3. Reverse the output postfix string
Example: (A + B) * (B – C)

reverse

(C – B) * (B + A)

Infix to postfix algorithm

C B - B A + *

reverse

* + A B - B C
Exercise:-
18
1– Convert the following infix expressions into postfix notations using two
stacks:
I. a+b^2/4-(c*5/8-f) ^3
II. m^3 or n-b/2 and (m+n)
III. a+b+c*(-d/3^4)and not f
IV. a^(b/2)or(x+y/3-w)and (c^2)^3
V. x^n+(m-p*4)^f and (-b)^2/c+6
VI. a+b-c or (x*2^n)-m/p and f^2
VII. a^(b/2)and (x-y/3+w)or(c^3)^2
VIII. n^x-(p*4+m)/f or –c/b^2
IX. b-a+c and n^x-(p/m or f^2)

2- Execute the following postfix notation using the stack ab*cde^/+


when a=5, b=6,c=8,d=2,e=2
3- Execute the following infix notation using the stack
a/(b*c/2)^4+m if a=10,b=8,c=4,m=20
Lab Exercise:-
19
1- Write a program in C++ to convert infix expressions
into postfix notations using two stacks:

2- Write a program in C++ to execute infix notation


using the stack.

3- Write a program in C++ to execute postfix notation


using the stack.

Homework:-
1- write an algorithm to execute infix notation (Number consists of
more than one rank) using the stack.
Hint:- For example (12+28=40).

You might also like