Stack Applications - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Stack Applications

Description:

Example: arithmetic expression a & b ... Algorithm is same as for arithmetic expressions: ... List represents postfix notation of an arithmetic expression. ... – PowerPoint PPT presentation

Number of Views:151
Avg rating:3.0/5.0
Slides: 24
Provided by: PEV4
Category:

less

Transcript and Presenter's Notes

Title: Stack Applications


1
Stack Applications
  • CS501
  • Irena Pevac

2
Arithmetic Expressions
  • Arithmetic expressions have
  • operands (variables or numeric constants).
  • Operators
  • Binary , -, , / ,
  • Unary -
  • Priority convention
  • - Unary minus (sign for negative numbers) has
    highest priority
  • , /, have medium priority
  • , - have lowest priority

3
Infix, Prefix, Postfix
  • Example arithmetic expression a b consists of
    operands a, b and operator .
  • Infix notation is format where operator is
    specified in between the two operands. ab
  • Prefix notation is format where operator is
    specified before the two operands. a b
  • Postfix notation is format where operator is
    specified after the two operands. Postfix
    notation is also called RPN or Reverse Polish
    Notation. a b

4
Arithmetic Expressions
5
Boolean Expressions
  • Boolean expressions have
  • Operands (variables of boolean type or boolean
    constants TRUE, FALSE).
  • Operators Binary , V, gt, ?
  • Unary
  • Convention for operator priorities
  • has highest priority, has next priority
    level, v has next priority level, gt has next
    priority level, ? has lowest priority.

6
Infix, Prefix, Postfix for Boolean Expressions
  • Example Boolean expression a b consists of
    operands a, b and operator .
  • Infix notation is format where operator is
    specified in between the two operands. ab
  • Prefix notation is format where operator is
    specified before the two operands. a b
  • Postfix notation is format where operator is
    specified after the two operands. Postfix
    notation is also called RPN or Reverse Polish
    Notation. a b

7
Boolean Expressions
8
Evaluating Arithmetic Expressions in Postfix
Notation
  • Algorithm
  • INPUT list of tokens that are either numbers or
    binary arithmetic operators , -,, /, and .
    List represents postfix notation of an arithmetic
    expression
  • OUTPUT value of expression.

9
  • Create an empty stack that will contain operands.
  • Take one by one token from the left to right.
  • If token is an operand push it to the stack.
  • If token is an operator op
  • Pop the top item from the stack as operand2.
  • Pop again the top item from the stack as
    operand1.
  • Perform operation operand1 op operand2.
  • Push result back to stack.
  • When all tokens in input expression are processed
    stack should contain a single item, which is the
    value of expression.

10
Evaluate 3 2 - 4
11
Evaluating Boolean Expressions in Postfix Notation
  • Algorithm is same as for arithmetic expressions
  • INPUT list of tokens that are either TRUE or
    FALSE or operators , V, gt, ?. List represents
    postfix notation of an arithmetic expression.
  • OUTPUT value of expression that is either TRUE
    or FALSE.

12
  • Create an empty stack that will contain boolean
    operands.
  • Take one by one token from the left to right.
  • If token is an operand push it to the stack.
  • If token is an operator op
  • Pop the top item from the stack as operand2.
  • Pop again the top item from the stack as
    operand1.
  • Perform operation operand1 op operand2.
  • Push result back to stack.
  • When all tokens in input expression are processed
    stack should contain a single item, which is the
    boolean value of expression.

13
Evaluate True True False gt
14
ALGORITHM TO CONVERT AN INFIX EXPRESSION TO RPN
  • Accepts An infix expression
  • Convert the expression to RPN
  • Uses a stack to store operations
  • Output The RPN expression

15
INFIX EXPRESSION TO RPN
Initialize an empty stack of operators. While no
error has occurred and the end of the infix
expression has not been reached, do the
followinga. Get the next input Token (constant,
variable, arithmetic operator, left
and right parenthesis) in the infix
expression.
16
INFIX EXPRESSION TO RPN
  • b. If Token is(i) a left parenthesis Push it
    onto the stack(ii) a right parenthesis Pop and
    display stack elements until a left
    parenthesis is popped, but do not display it.
    (It is an error if the stack becomes empty
    with no left parenthesis found.)(iii) an
    operator If the stack is empty or
  • Token
    has a higher priority than the top stack,
    push Token onto the stack. Otherwise, pop and
    display the top stack

  • element then repeat the
    comparison of Token

  • with the new top stack item. A left
    parenthesis in the stack is assumed to have a
    lower priority than that of operators.(iv) an
    operand Display it.

17
INFIX EXPRESSION TO RPN
  • c. When the end of the infix expression is
    reached, pop and display stack items until the
    stack is empty.

18
Example
  • Convert infix expression into postfix
  • (71)(6(3-2)) gtgt RPN

19
(No Transcript)
20
(71)(6(3-2)) gtgt RPN
21
(71)(6(3-2)) gtgt RPN
  • 7 1 6 3 - displayed
  • Stack content
  • Add that to display
  • 7 1 6 3 - is RPN

22
Checking Parenteses
  • Create an empty stack
  • Until a special symbol appears (indicating the
    end of expression) doRead one by one token from
    the input expressionIf token is
  • If the stack is empty push token to the
    stack
  • If the stack is non empty pop a top stack
    element
  • If the popped element is ( or type an
    error message such as Rule 2 is not satisfied
    at positionOtherwise push it back and push the
    token to the stack.
  • (

23
Checking Parenteses
  • If the stack is empty rule 1 is not satisfied.
    Type a message If the stack is not empty pop
    the top stack element and If it is then read
    next token Otherwise type Rule 1 is not
    satisfied at position
  • )
  • Other Skip to the next token
  • If the stack is non empty after the end of
    expression has been encountered then print an
    error message such as Rule 1 is violated at the
    end of expressionelse Check control
    is O.K.
Write a Comment
User Comments (0)
About PowerShow.com