stacks infix postfix

Upload: beeashh

Post on 01-Jun-2018

259 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Stacks Infix Postfix

    1/90

    Stacks

    Briana B. Morrison

     Adapted from Alan Eugenio

  • 8/9/2019 Stacks Infix Postfix

    2/90

    Stacks 2

    Topics Define Stack

     APIs

     Applications Create Hex Numer 

    !ecursion Con"erting from Infix to Postfix

    E"aluating Postfix

    Con"erting from Infix to Prefix

    Implementation  Arra# ased

    $inked list ased

  • 8/9/2019 Stacks Infix Postfix

    3/90

    Stacks 3

    Stacks  A stack is a se%uence of items t&at are

    accessile at onl# one end of t&e se%uence.

  • 8/9/2019 Stacks Infix Postfix

    4/90

    Stacks 4

    Pushing/Popping a Stack 

    Because a pop remo"es t&e item last added tot&e stack' (e sa# t&at a stack &as $I)* +last,

    in-first,out ordering.

  • 8/9/2019 Stacks Infix Postfix

    5/90

    Stacks 5

    TOP  –  THE MOST RECENTLY INSERTED

    ITEM

     PUSH  - TO INSERT ONTO THE TOP OF A

    STACK 

     POP  - TO REMOVE THE TOP ITEM IN A

    STACK 

  • 8/9/2019 Stacks Infix Postfix

    6/90

    Stacks 6

    THE stack CLASS IS TEMPLATED:

    template /class 0, class Container  1 de%ue/02 2

    0 IS THE TEMPLATE PARAMETER 

    FOR THE ITEM TYPE. Container  IS THE

    TEMPLATE PARAMETER FOR THE

    CLASS THAT WILL HOLD THE ITEMS.

  • 8/9/2019 Stacks Infix Postfix

    7/90

    Stacks 7

      METHOD INTERFACES

      FOR THE

      stack CLASS

  • 8/9/2019 Stacks Infix Postfix

    8/90

    Stacks 8

    CLASS stack  Constructor stack!

    stack+3Create an empt# stack

    CLASS stack  O"#rat$ons stack!

    ool empty+3 const

    C&eck (&et&er t&e stack is empt#. !eturn true if it isempt# and false ot&er(ise.

  • 8/9/2019 Stacks Infix Postfix

    9/90

    Stacks 9

    CLASS stack  O"#rat$ons stack!

    "oid pop+3!emo"e t&e item from t&e top of t&e stack.Precondition4 0&e stack is not empt#.

    Postcondition4 Eit&er t&e stack is empt# ort&e stack &as a ne( topmost

    item from a pre"ious pus&."oid push+const 05 item3

    Insert t&e argument item at t&e top of t&e stack.Postcondition4 0&e stack &as a ne( item at

    t&e top.

  • 8/9/2019 Stacks Infix Postfix

    10/90

    Stacks 10

    CLASS stack  O"#rat$ons stack!

    int size+ const3!eturn t&e numer of items on t&e stack.

    05 top+ const3

    !eturn a reference to t&e "alue of t&e item at t&e

    top of t&e stack.Precondition4 0&e stack is not empt#.

    const 05 top+ const3

    Constant "ersion of top+.

  • 8/9/2019 Stacks Infix Postfix

    11/90

    Stacks 11

    THERE ARE NO ITERATORS%

    THE ONLY ACCESSI&LE ITEM IS THE

    FRONT ITEM.

  • 8/9/2019 Stacks Infix Postfix

    12/90

    Stacks 12

    DETERMINE THE O'TP'T FROM

    THE FOLLOWIN(:

    stack/int2 m#6stack3

    for  +int i 1 73 i / 873 i99  m#6stack.pus& +i : i3

    while +;m#6stack.empt#+

    <  cout // m#6stack.top+ // endl3

      m#6stack.pop+3

    = -- (&ile

  • 8/9/2019 Stacks Infix Postfix

    13/90

  • 8/9/2019 Stacks Infix Postfix

    14/90

  • 8/9/2019 Stacks Infix Postfix

    15/90

    Stacks 15

    SO THE "ector ) de%ue) AND list CLASSES

    CAN SERVE AS THE 'NDERLYIN(

    CONTAINER CLASS.

    NOTE: THE TOP  ITEM ON THE

    STACK IS AT THE BACK  OF THE

    'NDERLYIN( CONTAINER%

  • 8/9/2019 Stacks Infix Postfix

    16/90

  • 8/9/2019 Stacks Infix Postfix

    17/90

    Stacks 17

    HERE IS PART OF THE DEFINITION OF

    THE stack CLASS:

    template /class 0' class Container 1 de%ue/02 2class stack<  protected4

      Container c3

      public4

      @

      void pus& +const "alue6t#pe5 x  <  c.pus&6ack +x3  ==

  • 8/9/2019 Stacks Infix Postfix

    18/90

    Stacks 18

    stack %oe

    template  void stack::push(const Item_Type& item) {

      container.push_back(item)

      !

    template  Item_Type& stack::top() {

      return container.back()

      !

    template

      const Item_Type& stack::top() const {

      return container.back()

      !

  • 8/9/2019 Stacks Infix Postfix

    19/90

  • 8/9/2019 Stacks Infix Postfix

    20/90

    Stacks 20

    STACK APPLICATIONS

  • 8/9/2019 Stacks Infix Postfix

    21/90

  • 8/9/2019 Stacks Infix Postfix

    22/90

  • 8/9/2019 Stacks Infix Postfix

    23/90

  • 8/9/2019 Stacks Infix Postfix

    24/90

  • 8/9/2019 Stacks Infix Postfix

    25/90

    Stacks 25

     

    EACH ACTIVATION RECORD CONTAINS:

    *. A VARIA&LE THAT CONTAINS THE RET'RN ADDRESS

    IN THE CALLIN( METHOD+

    ,. FOR EACH VAL'E FORMAL PARAMETER) A

    VARIA&LE THAT CONTAINS A COPY OF THE

    AR('MENT+

    -. FOR EACH REFERENCE FORMAL PARAMETER) A

    VARIA&LE THAT CONTAINS THE ADDRESS OF THE

    AR('MENT+

    . FOR EACH VARIA&LE DEFINED IN THE METHOD/S

    &LOCK) A VARIA&LE THAT CONTAINS A COPY OF

    THAT DEFINED VARIA&LE.

  • 8/9/2019 Stacks Infix Postfix

    26/90

    Stacks 26

     THERE IS A R'N0TIME STACK TO

    HANDLE THESE ACTIVATIONRECORDS.

    P'SH: WHEN F'NCTION IS CALLED

    POP: WHEN E1EC'TION OFF'NCTION IS COMPLETED 

  • 8/9/2019 Stacks Infix Postfix

    27/90

    Stacks 27

    AN ACTIVATION RECORD IS SIMILAR 

    TO AN E1EC'TION FRAME) E1CEPT

    THAT AN ACTIVATION RECORD HAS

    VARIA&LES ONLY) NO CODE.

    YO' CAN REPLACE REC'RSIONWITH ITERATION &Y CREATIN(

    YO'R OWN STACK.

  • 8/9/2019 Stacks Infix Postfix

    28/90

    Stacks 28

    %.. untime Stack 

    0&e C99 run,time s#stem keepstrack of t&e c&ain of acti"e functions(it& a stack

    ?&en a function is called' t&e run,

    time s#stem pus&es on t&e stack aframe containing $ocal "ariales and return "alue Program counter' keeping track of

    t&e statement eing executed

    ?&en a function returns' its frame ispopped from t&e stack and controlis passed to t&e met&od on top oft&e stack

    main() {

    int i = 5;

    foo(i);

    }

    foo(int j) {

    int k;

    k = j+1;

    bar(k);

    }

    bar(int m) {

    }

    bar  PC = 1  m = 6

    foo  PC = 3  j = 5  k = 6

    main  PC = 2  i = 5

  • 8/9/2019 Stacks Infix Postfix

    29/90

    Stacks 29

     A r g u m e n t s

    i n t n

     R e t u r n A d d r e s s

     R e t L o c o r R e t L o c 2

    A c t $ 2 a t $ o n R # c o r 3

    " n m a i n # $ %

      c a & & a c t # 4 $  A r g u m e n t   4   R e t u r n ( e t ) o c 1

    S 4 s t # 5 S t a c k

    " n a c t # 4 $

    c a & & a c t # 3 $

     A r g u m e n t  3

     A r g u m e n t  4

     R e t u r n ( e t ) o c 2

     R e t u r n ( e t ) o c 1

  • 8/9/2019 Stacks Infix Postfix

    30/90

    Stacks 30

  • 8/9/2019 Stacks Infix Postfix

    31/90

    Stacks 31

  • 8/9/2019 Stacks Infix Postfix

    32/90

    Stacks 32

     STACK APPLICATION

    CONVERTIN( FROM

    INFI1 TO POSTFI1

  • 8/9/2019 Stacks Infix Postfix

    33/90

    Stacks 33

    IN INFIX  NOTATION) AN OPERATOR

    IS PLACED &ETWEEN ITSOPERANDS.

    a 6 7

    c 8 3 6 9# ; 8 < => ? $

  • 8/9/2019 Stacks Infix Postfix

    34/90

    Stacks 34

    OLD COMPILERS:

    INFI1 MACHINE LAN('A(E

    THIS (ETS MESSY &ECA'SE OF

    PARENTHESES.

    NEWER COMPILERS:

    INFI1 POSTFI1 MACHINE LAN(.

  • 8/9/2019 Stacks Infix Postfix

    35/90

    Stacks 35

    In POSTFIX  Notat$on) An OPERATOR $s

    "@ac#3 IMMEDIATELY AFTER $ts

    OPERANDS.

    INFI1

    POSTFI1

    a 6 7

  • 8/9/2019 Stacks Infix Postfix

    36/90

  • 8/9/2019 Stacks Infix Postfix

    37/90

    Stacks 37

    LET/S CONVERT AN INFI1 STRIN( TO

    A POSTFI1 STRIN(.

    8 4 B 

  • 8/9/2019 Stacks Infix Postfix

    38/90

  • 8/9/2019 Stacks Infix Postfix

    39/90

    Stacks 39

    INFI1 POSTFI1

    8 4 B

  • 8/9/2019 Stacks Infix Postfix

    40/90

    Stacks 40

    INFI1 POSTFI1

    8 4 B

    THE OPERANDS FOR 0/ ARE NOT YET

    IN POSTFI1) SO 0/ M'ST &E

    TEMPORARILY SAVED SOMEWHERE.9STACK>

  • 8/9/2019 Stacks Infix Postfix

    41/90

    Stacks 41

    INFI1 POSTFI1

    8 4 B 4

  • 8/9/2019 Stacks Infix Postfix

    42/90

    Stacks 42

    INFI1 POSTFI1

    8 4 B 4

    THE OPERANDS FOR / ARE NOT YET

    IN POSTFI1) SO / M'ST &E

    TEMPORARILY SAVED SOMEWHERE)AND RESTORED BEFORE ‘-’ .

  • 8/9/2019 Stacks Infix Postfix

    43/90

    Stacks 43

    INFI1 POSTFI1

    8 4 B 4B

  • 8/9/2019 Stacks Infix Postfix

    44/90

  • 8/9/2019 Stacks Infix Postfix

    45/90

    Stacks 45

    Su""os#) $nst#a3) # start#3 $t=

    40B

    A;t#r 5o2$n< / to "ost;$) / $s

    t#5"orar$@4 sa2#3) an3 t=#n 4/ $s

    a""#n3#3 to "ost;$. W=at =a""#ns =#n

    0/ $s acc#ss#3

    INFI1 POSTFI1

    4 8 B 4

  • 8/9/2019 Stacks Infix Postfix

    46/90

  • 8/9/2019 Stacks Infix Postfix

    47/90

  • 8/9/2019 Stacks Infix Postfix

    48/90

    Stacks 48

    %one!ting In$ to Post$ Anal#sis4 Operands are in same order in infix and postfix

    Operators occur later in postfix

    Strateg#4 Send operands straig&t to output

    Send &ig&er precedence operators first

    If same precedence' send in left to rig&t order  Hold pending operators on a stack

  • 8/9/2019 Stacks Infix Postfix

    49/90

    Stacks 49

    FOR EACH OPERATOR IN INFI1:

      LOOP 'NTIL OPERATOR P'SHED:

      IF OPERATOR STACK IS EMPTY)P'SH

      ELSE IF INFIX  OPERATOR HAS 

    GREATER  PRECEDENCE THAN TOP OPERATOR 

    ON STACK)

     PUSHELSE

    POP AND APPEND TO POSTFI1

  • 8/9/2019 Stacks Infix Postfix

    50/90

    Stacks 50

       INFIX GREATER, PUSH 

  • 8/9/2019 Stacks Infix Postfix

    51/90

    Stacks 51

    CONVERT FROM INFI1 TO POSTFI1:

    INFI1 POSTFI1

    a 6 7 c ? 3 0 #  

  • 8/9/2019 Stacks Infix Postfix

    52/90

    Stacks 52

    INFI1 POSTFI1

    a 6 7 c ? 3 8 # a7c3?6# 8

    0

    ?

     

      6

      OPERATOR STACK 

  • 8/9/2019 Stacks Infix Postfix

    53/90

    Stacks 53

    WHAT A&O'T PARENTHESES

    LEFT PARENTHESIS: P'SH) &'T

    WITH LOWEST PRECEDENCE.

    RI(HT PARENTHESIS:KEEP POPPIN(

    AND APPENDIN( TO POSTFI1 'NTIL

    9 POPPED+ PITCH 9 AND PROCEED.

  • 8/9/2019 Stacks Infix Postfix

    54/90

    Stacks 54

    CONVERT TO POSTFI1:

    94 6 B>  

  • 8/9/2019 Stacks Infix Postfix

    55/90

    Stacks 55

    INFI1 POSTFI1

    94 6 B> 4B6

      6  9

     

    OPERATOR STACK 

  • 8/9/2019 Stacks Infix Postfix

    56/90

    Stacks 56

    # i

  • 8/9/2019 Stacks Infix Postfix

    57/90

    Stacks 57

    #$e!cise

    Con"ert t&e follo(ing expression to postfix4

    a 9 , + c 9 d : e - + f g : + & 9 i

    # l i P

  • 8/9/2019 Stacks Infix Postfix

    58/90

    #aluating a Post$#$p!ession ?e descrie &o( to parse and e"aluate a

    postfix expression.

    ?e read t&e tokens in one at a time.

    If it is an integer' pus& it on t&e stack

    If it is a inar# operator' pop t&e top t(o

    elements from t&e stack' appl# t&e operator'

    and pus& t&e result ack on t&e stack.

    Stacks 58

    5 9 3 . 4 2 7

  • 8/9/2019 Stacks Infix Postfix

    59/90

    5 9 3 . 4 2 7. Stack *perations *utput

    pus&+3 pus&+3

    pus&+3

    pus&+pop+ 9 pop+ 8F

    pus&+G3 8F Gpus&+F3 8F G F

    pus&+pop+ : pop+ 8F

    pus&+pop+ : pop+ pus&+J Jpus&+pop+ 9 pop+ 87

    pus&+pop+ : pop+ 8

    Stacks 59

  • 8/9/2019 Stacks Infix Postfix

    60/90

  • 8/9/2019 Stacks Infix Postfix

    61/90

    Stacks 61

    THE FOLLOWIN( TRANSITION

    MATRI1 SPECIFIES THE TRANSITION

    FROM INFI1 NOTATION TO POSTFI1

    NOTATION:

      TOP CHARACTER ON STACK 

  • 8/9/2019 Stacks Infix Postfix

    62/90

    Stacks 62

      9 6)0 )? #5"t4I

    N $3#nt$;$#rF

    I >1  9

      6)0C

    H  )?

    AR   #5"t4

     APPEND APPEND APPEND APPEND

      TO TO TO TOPOSTFI1 POSTFI1 POSTFI1 POSTFI1

    POP+ POP TO POP TO ERROR 

    PITCH 9 POSTFI1 POSTFI1

    P'SH P'SH P'SH P'SH

    P'SH POP TO POP TO P'SH

      POSTFI1 POSTFI1

      P'SH P'SH POP TO P'SH

      POSTFI1

    ERROR POP TO POP TO DONE

      POSTFI1 POSTFI1

  • 8/9/2019 Stacks Infix Postfix

    63/90

    Stacks 63

  • 8/9/2019 Stacks Infix Postfix

    64/90

    Stacks 64

     STACK APPLICATION

    EVAL'ATIN( A

    % , ith 1 t k

  • 8/9/2019 Stacks Infix Postfix

    65/90

    Stacks 65

    %an ,e one ith 1 stack 

    ?&ene"er an operand is encountered' pus&

    onto stack

    ?&ene"er operator is encountered' pop

    re%uired numer of arguments from operandstack and e"aluate

    Pus& result ack onto stack

    (l ith " l

  • 8/9/2019 Stacks Infix Postfix

    66/90

    Stacks 66

     (lgo!ithm "o! eal

    8. Empt# t&e operand stack

    #. $hile t&ere are more tokens

    . Ket t&e next token

    G. i% t&e first c&aracter of t&e token is a digit

    .

    Pus& t&e integer onto t&e stack. else i% t&e token is an operator 

    J. Pop t&e rig&t operand off t&e stack

    . Pop t&e left operand off t&e stack

    . E"aluate t&e operation87. Pus& t&e result onto t&e stack

    88. Pop t&e stack and return t&e result

    R P N 9 R # 2 # r s # P o @ $ s = N o t a t $ o n > # " r # s s $ o n , - 6

  • 8/9/2019 Stacks Infix Postfix

    67/90

    Stacks 67

    R P N 9 R # 2 # r s # P o @ $ s = N o t a t $ o n > # " r # s s $ o n , - 6

    3

    2

    o p e r a n * t a c + e m p t ,

    3 - " * e n t i , . a s a n o p e r a t o r  / e g i n t h e p r o c e s s o e 0 a & u a t i n g . -

    4 - g e t p e r a n * s # $ p o p s s t a c +  t i c e a n * a s s i g n s 3 t o  r i g h t a n * 2 t o & e t -

    5 - c o m p u t e # $ e 0 a & u a t e s & e t . r i g h t  a n * r e t u r n s t h e 0 a & u e 5 - ( e t u r n  0 a & u e i s p u s h e * o n t h e s t a c + -

    5

    1 - " * e n t i , 2 a s a n o p e r a n * -

      P u s h i n t e g e r 2 o n t h e s t a c + -

    C u r r e n t o p e r a n * t a c +

    2

    3

    2

    2 - " * e n t i , 3 a s a n o p e r a n * -  P u s h i n t e g e r 3 o n t h e s t a c + -

    c a n o p r e s s i o n a n * A c t i o n

    ( th # l

  • 8/9/2019 Stacks Infix Postfix

    68/90

    Stacks 68

     (nothe! #$ampleExpression4 , 9 F - 8 F 9 , :

    F

    Execute , Execute F 9

    F

    Execute - F

    G

    8

    G

    F

    8

    G

    Execute

    8 9 F

    G

    Execute

    G ,

    8

    Execute

    : 8

     

     F

  • 8/9/2019 Stacks Infix Postfix

    69/90

    Stacks 69

    IN PREFIX  NOTATION) AN OPERATOR 

    IMMEDIATELY PRECEDES ITSOPERANDS.

    INFI1 PREFI1

  • 8/9/2019 Stacks Infix Postfix

    70/90

    Stacks 70

    INFI1 PREFI1

    a 6 7 6a7

    a 97 6 c> a67c

    a 7 6 c 6a7c

    IN PREFI1 NOTATION) AS IN POSTFI1)THERE ARE NO PARENTHESES.

  • 8/9/2019 Stacks Infix Postfix

    71/90

    Stacks 71

    TWO STACKS ARE 'SED:

    OPERATOR STACK: SAME R'LES AS

      FOR POSTFI1 STACK 

    OPERAND STACK: TO HOLD THE

      OPERANDS

    WHENEVER o"t IS POPPED FROM

  • 8/9/2019 Stacks Infix Postfix

    72/90

    Stacks 72

    WHENEVER o"t IS POPPED FROM

    OPERATOR STACK) o"3* AND THEN

    o"3, ARE POPPED FROM OPERAND

    STACK. THE STRIN( o"t 6 o"3, 6 o"3*

    IS P'SHED ONTO OPERAND STACK.

    NOTE: o"3, WAS P'SHED &EFORE o"3*.

  • 8/9/2019 Stacks Infix Postfix

    73/90

    Stacks 73

    CONVERT FROM INFI1 TO PREFI1:

    INFI1

    a 6 97 c 8 3> ? #

    INFI1 PREFI1

  • 8/9/2019 Stacks Infix Postfix

    74/90

    Stacks 74

    a 6 97 c 8 3> ? # 6a?8 7c3#

      6a?8 7c3#

      ?8 7c3#

      #

      87c3  3 ?

      7c 8 

      c

      7 9  a 6

      OPERAND OPERATOR  

      STACK STACK 

  • 8/9/2019 Stacks Infix Postfix

    75/90

    #$e!cise

  • 8/9/2019 Stacks Infix Postfix

    76/90

    Stacks 76

    #$e!cise

    Con"ert t&e follo(ing expression to prefix4

    a 9 , + c 9 d : e - + f g : + & 9 i

    Sel" Test

  • 8/9/2019 Stacks Infix Postfix

    77/90

    Stacks 77

    Sel"Test

    ?rite t&e code to re"erse an input string

    using a stack.

     Assuming t&at #ou &a"e a stack t&at contains

    colors4 stack/color2(rite t&e code to remo"e all t&e "alues of a

    specific color (&ile lea"ing t&e remaining

    "alues in t&e same order.

    Stack Implementations

  • 8/9/2019 Stacks Infix Postfix

    78/90

    Stacks 78

    Stack Implementations

     Arra# ased ?&ere is topL

    Ho( are elements added' remo"edL

    $inked $ist ased ?&ere is topL

    Ho( are elements added' remo"edL

    Efficienc# of operations

    (!!a ase Stack

  • 8/9/2019 Stacks Infix Postfix

    79/90

    Stacks 79

     (!!a ase StackImplementation?&ere s&ould top eL

      8F

      8

     

      F

     

      G

    stack

    7 arra#

    ?&at are arra# "aluesL

    s.pus& +F7

    s.pop+

    !oa,le (!!a ,ase Stack

  • 8/9/2019 Stacks Infix Postfix

    80/90

    Stacks 80

    !oa,le (!!a,ase Stack 

    In a pus& operation' (&ent&e arra# is full' instead oft&ro(ing an exception' (ecan replace t&e arra# (it& a

    larger one Ho( large s&ould t&e ne(

    arra# eL incremental strateg#4 increase

    t&e si>e # a constant c douling strateg#4 doule t&e

    si>e

    A@

  • 8/9/2019 Stacks Infix Postfix

    81/90

    Stacks 81

    %ompa!ison o" the St!ategies

    ?e compare t&e incremental strateg# and t&edouling strateg# # anal#>ing t&e total time T #%$ 

    needed to perform a series of % pus& operations

    ?e assume t&at (e start (it& an empt# stack

    represented # an arra# of si>e 1

    ?e call amorti>ed time of a pus& operation t&e

    a"erage time taken # a pus& o"er t&e series of

    operations' i.e.' T #%$/%

    Inc!emental St!ateg

  • 8/9/2019 Stacks Infix Postfix

    82/90

    Stacks 82

    Inc!emental St!ateg (nalsis

    ?e replace t&e arra# ( = %-c times 0&e total time T #%$ of a series of % pus&

    operations is proportional to

    % + c + 2c . 3c . 4c . ) . (c =

    % + c#1 . 2 . 3 . 6 . ( $ =

    % + c( #( . 1$/2 Since c is a constant' T #%$ is O#% + ( 2$' i.e.' O#%2$ 0&e amorti>ed time of a pus& operation is O#%$

    ou,ling St!ateg (nalsis

  • 8/9/2019 Stacks Infix Postfix

    83/90

    Stacks 83

    ou,ling St!ateg (nalsis

    ?e replace t&e arra# ( = &og2 %times

    0&e total time T #%$ of a series of % pus& operations is proportional to

    % + 1 . 2 . 4 . 9 . 6. 2(  =

    % + 2( . 1 −1 = 2% −1 T #%$ is O#%$

    0&e amorti>ed time of a pus&operation is O#1$

    geometric series

    1

    2

    14

    9

    :inke :ist Implementation

  • 8/9/2019 Stacks Infix Postfix

    84/90

    Stacks 84

    :inke :ist Implementation

    D

    C

    t o p

    A

    t a c +

    D AC

    ) i n + e * ) i s t

    r o n t

    s.pus& +O)

    s.pop+

    Stack ith a Singl :inke

  • 8/9/2019 Stacks Infix Postfix

    85/90

    Stacks 85

    Stack ith a Singl :inke:ist ?e can implement a stack (it& a singl# linked list

    0&e top element is stored at t&e first node of t&e list

    0&e space used is O#%$ and eac& operation of t&e Stack AD0

    takes O#1$ time

    nodes

    elements

    %ompa!ison o" stack

  • 8/9/2019 Stacks Infix Postfix

    86/90

    Stacks 86

    %ompa!ison o" stack Implementations

    0&e code for t&e "ector "ersion is "er# similar to t&e

    implementation in t&e C99 standard lirar#. B# using delegation' (e a"oid &a"ing to implement t&e

    operations oursel"es and are assured of *+8performance.

    sing t&e standard containers &as a space penalt#. "ector allocates additional space to pro"ide ammorti>ed constant

    insertion time. list uses a doule linked list (&ic& &as unneeded pointers.

    sing a single linked list allocates space for t&e links' utt&ese are necessar#.

    sing a single linked list also pro"ides *+8 performance.

    Summa! Slie 1

  • 8/9/2019 Stacks Infix Postfix

    87/90

    Stacks 87 87

    Summa! Slie 1Q, Stack 

    , Storage Structure (it& insert +pus& and erase +popoperations occur at one end' called t&e top of t&estack.

    , 0&e last element in is t&e first element out of t&estack' so a stack is a $I)* structure.

    Summa! Slie 2

  • 8/9/2019 Stacks Infix Postfix

    88/90

    Stacks 88 88

    Summa! Slie 2Q, !ecursion

    , 0&e s#stem maintains a stack of acti"ation recordst&at specif#4

    8 t&e function arguments

    F t&e local "ariales-oRects t&e return address

    , 0&e s#stem pus&es an acti"ation record (&encalling a function and pops it (&en returning.

  • 8/9/2019 Stacks Infix Postfix

    89/90

    Summa! Slie 4

  • 8/9/2019 Stacks Infix Postfix

    90/90

    Summa! Slie 4Q, Infix notation

    , A inar# operator appears et(een its operands.

    , More complex t&an postfix' ecause it re%uires t&euse of operator precedence and parent&eses.

    , In addition' some operators are left,associati"e' anda fe( are rig&t,associati"e.