Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS 240 35.
CHAPTER 3 : STACK
description
Transcript of CHAPTER 3 : STACK
![Page 1: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/1.jpg)
CHAPTER 3 : STACKPART II
STACK APPLICATIONS
![Page 2: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/2.jpg)
CONVERTING DECIMAL TO BINARY
![Page 3: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/3.jpg)
STACK APPLICATIONS1) Converting Decimal to Binary: Consider the following pseudo code
1) Read (number)2) Loop (number > 0)
1 )digit = number modulo 22 )print (digit)
3 )number = number / 2o The problem with this code is that it will print the binary number
backwards. (ex: 19 becomes 11001000 instead of 00010011. )o To remedy this problem, instead of printing the digit right away,
we can push it onto the stack. Then after the number is done being converted, we pop the digit out of the stack and print it.
![Page 4: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/4.jpg)
INFIX TO POSTFIX CONVERSION
![Page 5: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/5.jpg)
INFIX NOTATION
Infix notation is the common arithmetic and logical
formula notation, in which operators are written infix-
style between the operands they act on
E.g. A + B
![Page 6: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/6.jpg)
POSTFIX NOTATION
In Postfix notation, the operator comes after the
Operand.
For example, the Infix expression A+B will be written as
AB+ in its Postfix Notation.
Postfix is also called ‘Reverse Polish Notation’
![Page 7: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/7.jpg)
PREFIX NOTATION
In Prefix notation, the operator comes before the
operand.
The Infix expression A+B will be written as +AB in its
Prefix Notation.
Prefix is also called ‘Polish Notation’
![Page 8: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/8.jpg)
CONVERSION FROM INFIX TO POSTFIX ALGORITHM
Step1
Scan the Infix expression from left to right for
tokens (Operators, Operands & Parentheses) and
perform the steps 2 to 5 for each token in the
Expression
![Page 9: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/9.jpg)
ALGORITHM
Step2
If token is operand, Append it in postfix expression
Step3
If token is a left parentheses “(“, push it in stack.
![Page 10: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/10.jpg)
ALGORITHM
Step4
If token is an operator,
Pop all the operators which are of higher or equal
precedence then the incoming token and append
them (in the same order) to the output Expression.
After popping out all such operators, push the new
token on stack.
![Page 11: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/11.jpg)
ALGORITHMStep5
If “)” right parentheses is found,
Pop all the operators from the Stack and append
them to Output String, till you encounter the
Opening Parenthesis “(“.
Pop the left parenthesis but don’t append it to
the output string (Postfix notation does not have
brackets).
![Page 12: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/12.jpg)
ALGORITHM
Step6
When all tokens of Infix expression have been scanned.
Pop all the elements from the stack and append
them to the Output String.
The Output string is the Corresponding Postfix
Notation.
![Page 13: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/13.jpg)
EXAMPLELet the incoming the Infix expression be :
A * (B + C) – D / E
Stage 1: Stack is empty and we only have the Infix
Expression .
![Page 14: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/14.jpg)
EXAMPLE
Stage 2
The first token is Operand A Operands are Appended
to the Output as it is.
![Page 15: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/15.jpg)
EXAMPLE
Stage 3
Next token is * Since Stack is empty (top==NULL) it
is pushed into the Stack
![Page 16: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/16.jpg)
EXAMPLEStage 4
Next token is ( the precedence of open-parenthesis, when it
is to go inside, is maximum.
But when another operator is to come on the top of ‘(‘ then
its precedence is least.
![Page 17: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/17.jpg)
EXAMPLE
Stage 5
Next token, B is an operand which will go to the Output
expression as it is
![Page 18: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/18.jpg)
EXAMPLEStage 6
Next token, + is operator, We consider the precedence of top element
in the Stack, ‘(‘. The outgoing precedence of open parenthesis is the
least (refer point 4. Above). So + gets pushed into the Stack
![Page 19: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/19.jpg)
EXAMPLE
Stage 7
Next token, C, is appended to the output
![Page 20: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/20.jpg)
EXAMPLE
Stage 8
Next token ), means that pop all the elements from Stack and
append them to the output expression till we read an opening
parenthesis.
![Page 21: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/21.jpg)
EXAMPLEStage 9
Next token, -, is an operator. The precedence of operator on the
top of Stack ‘*‘ is more than that of Minus. So we pop multiply
and append it to output expression. Then push minus in the
Stack.
![Page 22: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/22.jpg)
EXAMPLE
Stage 10
Next, Operand ‘D‘ gets appended to the output.
![Page 23: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/23.jpg)
EXAMPLE
Stage 11
Next, we will insert the division operator into the Stack
because its precedence is more than that of minus.
![Page 24: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/24.jpg)
EXAMPLE
Stage 12
The last token, E, is an operand, so we insert it to the
output Expression as it is.
![Page 25: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/25.jpg)
EXAMPLE
Stage 13
The input Expression is complete now. So we pop the
Stack and Append it to the Output Expression as we
pop it.
![Page 26: CHAPTER 3 : STACK](https://reader036.fdocuments.net/reader036/viewer/2022081502/5681671b550346895ddb946d/html5/thumbnails/26.jpg)
Postfix EvaluationOperand: pushOperator: pop 2 operands, do the math, pop result back onto stack
1 2 3 + *
Postfix Stack( bot -> top )a) 1 2 3 + *b) 2 3 + * 1c) 3 + * 1 2d) + * 1 2 3e) * 1 5 // 5 from 2 + 3f) 5 // 5 from 1 * 5
STACK APPLICATIONS