polish notations

Upload: muhammad-faraz-ahmed

Post on 06-Jul-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Polish Notations

    1/24

    Linear Data Structures

    Stack

  • 8/17/2019 Polish Notations

    2/24

    Topics Covered in This Lecture

    Applications of stack

     – Reversing the string

     –Balancing symbols

     – Postfix expression evaluation

     – Translating infix expression to postfix expression

  • 8/17/2019 Polish Notations

    3/24

    Applications of Stack

    Reversing the string

     – push each character on to a stack as it is read.

     – When the line is finished, we then pop characters off

    the stack, and they will come off in the reverse order.

  • 8/17/2019 Polish Notations

    4/24

    Applications of Stack

    Balancing SymbolsCompilers use a program that checks hether every symbol !like braces" parenthesis"brackets" etc# in a program is balanced$

    The simple algorithm for this purpose is%

    1. Make an empty stack.

    2. Read the characters until end of file.

    3. If the character is an open any thing, push it onto the stack.

    . If it is a close any thing, then

    if the stack is empty report an error.!ther"ise #op the Stack.

    If the popped sym$ol is not the corresponding opening sym$ol, thenreport an error.

    %. &t the end of the file, if the stack is not empty report an error.

  • 8/17/2019 Polish Notations

    5/24

    #olish 'otation 

    a – b / c + d * e

     

    Precedence?

     

    1. b/c

    2. d*e3. a – a1 /a1 = b/c /

    4. t2 + a2 / t2 = a – b/c / /a2 = d*e/

  • 8/17/2019 Polish Notations

    6/24

    Infi(, Suffi(, #refi(

    Infix = a * b + c

    ((a*b) +c)

    Priority:

    1. a * b

    2. a1 + c / a1 = a * b /

    Prefix =

    * a b , +a1 c

    +*abc

    Sffix =

    ab* , a1c+

    ab*c+

  • 8/17/2019 Polish Notations

    7/24

    Infi(, Suffi(, #refi(

    infix = a- b * c / d + e / f

    !ffix =a – bc* / d + e / f 

      a – bc*d/ + e / f 

      a – bc*d/ + ef/

      abc*d/" + ef/

      abc*d/"ef/+

    #refix =a " *bc / d + e / f 

      a " /*bcd + e / f 

      a " /*bcd + /ef 

      "a/*bcd + /ef 

      +"a/*bcd/ef

  • 8/17/2019 Polish Notations

    8/24

    Infi(, Suffi(, #refi(

    $nfix:

    a+b*c–d/f+e

    Sffix:

    abc*+df/"e+

    Prefix:

    +"+a*bc/dfe

  • 8/17/2019 Polish Notations

    9/24

    Applications of Stack

    Postfix &xpression &valuation

     – 'hen a number is seen" it is pushed onto the stack

     – 'hen an operator is seen" then pop to elements fromstack and push the result onto the stack$

    (o e evaluate the folloing postfix expression$

    ) * + , - . / - , - /0$ The first four are placed on the stack$ The resulting stack is

    3

    2

    5

    6

    stack 

  • 8/17/2019 Polish Notations

    10/24

    Applications of Stack

    evaluating the folloing postfix expression$

    ) * + , - . / - , - /3

    2

    5

    6

    stack +$ (ext a - is read" so , and + are popped from the stack and their sum * ispushed$ 5

    5

    6

    stack 

  • 8/17/2019 Polish Notations

    11/24

    Applications of Stack

    evaluating the folloing postfix expression$

    ) * + , - . / - , - /

    5

    5

    6

    stack 

    ,$ (ext . is read and pushed$ 85

    5

    6

    stack 

  • 8/17/2019 Polish Notations

    12/24

    Applications of Stack

    evaluating the folloing postfix expression$

    ) * + , - . / - , - /

    1$ (ext a / is seen so . and * are popped as . / * 2 13 is pushed40

    5

    6

    stack 

    8

    5

    5

    6

    stack 

  • 8/17/2019 Polish Notations

    13/24

    Applications of Stack

    evaluating the folloing postfix expression$

    ) * + , - . / - , - /

    *$ (ext a - is read so 13 and * are popped and 13 - * 2 1* is pushed$

    40

    5

    6

    stack 

    45

    6

    stack 

  • 8/17/2019 Polish Notations

    14/24

    Applications of Stack

    evaluating the folloing postfix expression$

    ) * + , - . / - , - /

    )$ (o , is pushed

    45

    6

    stack 

    3

    45

    6

    stack 

  • 8/17/2019 Polish Notations

    15/24

    Applications of Stack

    evaluating the folloing postfix expression$

    ) * + , - . / - , - /

    4$ (ext symbol is - so pops , and 1* and pushes 1* - , 2 1." so push 1. in stack$

    345

    6

    stack 

    48

    6

    stack 

  • 8/17/2019 Polish Notations

    16/24

  • 8/17/2019 Polish Notations

    17/24

    Applications of StackTranslating infix expressions to postfix expression

     – 'hen an operand is read" it is immediately placed onto the output$

     – 'hen an operator or left parenthesis comes then save it in the stack

    initially stack is empty$

     – 6f e see a right parenthesis" then e pop the stack" riting symbols until e encounter a !corresponding# left parenthesis" hich is popped but notoutput$

     –

    6f e see any other symbol !7-8" 7/8" 7!7" etc# then e pop entries form thestack until e find an entry of loer priority$ 9ne exception is that enever remove a 7!7 from the stack except hen processing a 7#8$

     – 'hen the popping is done" e push the operand onto the stack$

     – 5inally" if e read the end of input" e pop the stack until it is empty" riting symbols onto the output$

  • 8/17/2019 Polish Notations

    18/24

    Applications of Stack

    Translating infix expressions to postfix expression

    Convert the folloing infix expression to postfix expression$

    a-b/c-!d/e-f#/g

    0$ 5irst the symbol a is read" so it is passed through to the output aoutput+$ Then - is read and pushed onto the stack.

    stack 

    4. !e"t a # is read. $he top entry on the operator stack has lower

     precedence than #, so nothin% is output and # is put on the .

    ,$ (ext b is read and passed through to the output$ a&

    output #

    stack 

  • 8/17/2019 Polish Notations

    19/24

    Applications of Stack

    Converting the folloing infix expression to postfix expression$

    a-b/c-!d/e-f#/g

    *$ (ext" c is read and output$

    )$ The next symbol is a -$ Checking the stack" e find that priority of stack top

    symbol / is higher than - $ So e pop a / and place it on the output" Pop the

    other -" hich is not of loer but e:ual priority" and then push -$

    stack 

    a&c#output

    #

    stack 

    a&c

    output

  • 8/17/2019 Polish Notations

    20/24

    Applications of Stack

    Converting the folloing infix expression to postfix expression$

    a-b/c-!d/e-f#/g

    4$ The next symbol read is an 7!7" hich" being of highest precedence" is placed on

    the stack$

    '

    stack 

    .$ Then d is read and output$ a&c#d

    output

  • 8/17/2019 Polish Notations

    21/24

    Applications of Stack

    Converting the folloing infix expression to postfix expression$

    a-b/c-!d/e-f#/g

    ;$ 'e continue by reading a /$ Since open parenthesis do not get removed except

     hen a closed parenthesis is being processed" there is no output and e push /

    in stack

    #

    '

    stack 

    03$ (ext" e is read and output$a&c#de

    output

  • 8/17/2019 Polish Notations

    22/24

  • 8/17/2019 Polish Notations

    23/24

    Applications of Stack

    Converting the folloing infix expression to postfix expression$

    a-b/c-!d/e-f#/g

    0,$ (o e read a 7#8" so the stack is emptied back to the 7!7" e output a -$

    01$ 'e read a / next< it is pushed onto the stack$

    a&c#de#f

    output

    stack 

    #

    stack 

    0*$ (o" g is read and output$ a&c#de#f%

    output

  • 8/17/2019 Polish Notations

    24/24

    Applications of Stack

    Converting the folloing infix expression to postfix expression$

    a-b/c-!d/e-f#/g

    0)$ The input is no empty" so pop output symbols from the stack until it is empty$

    a&c#de#f%#

    outputstack 

    #

    stack