02 Stacks Queues

download 02 Stacks Queues

of 66

Transcript of 02 Stacks Queues

  • 8/9/2019 02 Stacks Queues

    1/66

    DATA STRUCTURESCOL 106

    AMIT KUMAR, SHWETAAGRAWAL

  • 8/9/2019 02 Stacks Queues

    2/66

    Why are we study!" DataStru#tures $

  • 8/9/2019 02 Stacks Queues

    3/66

    M%der! w%r&d a&& a'%ut (DATA

  • 8/9/2019 02 Stacks Queues

    4/66

  • 8/9/2019 02 Stacks Queues

    5/66

    Dea&!" wth data(

    H%w t% use t $

    H%w t% st%re t $

    H%w t% )r%#ess t $

    H%w t% "a! *+!%w&ed"e

    -r%. t $ H%w t% +ee) t se#ret$

  • 8/9/2019 02 Stacks Queues

    6/66

    Dea&!" wth data(

    H%w t% use t $

    H%w t% st%re t $

    H%w t% )r%#ess t $

    H%w t% "a! *+!%w&ed"e

    -r%. t $ H%w t% +ee) t se#ret$

  • 8/9/2019 02 Stacks Queues

    7/66

    H%w sh%u&d data 'e

    st%red$De)e!ds %! y%urre/ure.e!t

  • 8/9/2019 02 Stacks Queues

    8/66

    ut we hae s%.e 'u&d!"'&%#+s

    Data s derse 22

  • 8/9/2019 02 Stacks Queues

    9/66

    T% st%re %ur '" data

  • 8/9/2019 02 Stacks Queues

    10/66

    10

    E&e.e!tary Data*Stru#tures

    Arrays

    Lists

    Stacks Queues

    Trees

    RF

    1

    2

    3 4

    5 6

    7 8

    I! s%.e &a!"ua"es these are 'as# data ty)es 3 !%thers they !eed t% 'e .)&e.e!ted

    head

  • 8/9/2019 02 Stacks Queues

    11/66

    Stacks

  • 8/9/2019 02 Stacks Queues

    12/66

    Sta#+

    A &st -%r wh#h I!sert a!d De&ete area&&%wed %!&y at %!e e!d %- the &st4the top5 LIO 3 Last !, rst %ut

    Push

    Pop Pop

  • 8/9/2019 02 Stacks Queues

    13/66

    What s ths "%%d -%r $

    7a"e8sted hst%ry ! a We''r%wser

  • 8/9/2019 02 Stacks Queues

    14/66

    What s ths "%%d -%r $

    7a"e8sted hst%ry ! a We''r%wser

    U!d% se/ue!#e ! a te9t edt%r

  • 8/9/2019 02 Stacks Queues

    15/66

    What s ths "%%d -%r $

    7a"e8sted hst%ry ! a We''r%wser

    U!d% se/ue!#e ! a te9t edt%r Sa!" &%#a& ara'&es whe! %!e

    -u!#t%! #a&&s a!%ther, a!d ths

    %!e #a&&s a!%ther

  • 8/9/2019 02 Stacks Queues

    16/66

    H%w sh%u&d we re)rese!tt $

    Wrte #%de ! )yth%! $

  • 8/9/2019 02 Stacks Queues

    17/66

    H%w sh%u&d we re)rese!tt $

    Wrte #%de ! )yth%! $

    Wrte #%de ! C $

  • 8/9/2019 02 Stacks Queues

    18/66

    H%w sh%u&d we re)rese!tt $

    Wrte #%de ! )yth%! $

    Wrte #%de ! C $

    Wrte #%de ! :aa $

    Are!;t we esse!ta&&y d%!"the sa.e th!"$

  • 8/9/2019 02 Stacks Queues

    19/66

    1e#ts,

    wth %)erat%!sde=!ed %! the.

  • 8/9/2019 02 Stacks Queues

    20/66

    ?0

    E9a.)&es

    as# Ty)es !te"er, rea&4@%at!" )%!t5, '%%&ea!

    40,15, #hara#ter Arrays

    A022

  • 8/9/2019 02 Stacks Queues

    21/66

    ?1

    A .a))!" -r%. a! !de9 set, su#h as{0,1,2,,n}, !t% a #e&& ty)e

    O'>e#tsset %-#e&&sO)erat%!s

    create(A,n)

    put(A,v,i) %rA[i] = v vaue(A,i)

    ADT Array

  • 8/9/2019 02 Stacks Queues

    22/66

    22

    A'stra#t Data Ty)es 4ADTs5

    A! a'stra#t data ty)e 4ADT5 sa! a'stra#t%! %- a data

    stru#ture

    A! ADT s)e#=es

    Data st%redO)erat%!s %! the data

    Err%r #%!dt%!s ass%#ated

    wth %)erat%!s

  • 8/9/2019 02 Stacks Queues

    23/66

    23

    ADT -%r st%#+ trade

    The data st%red are 'uyse&& %rders

    The %)erat%!ssu))%rted are

    %rder 'uy 4st%#+, shares5 %rder se&&4st%#+, shares 5

    %d #a!#e&4%rder5

    Err%r #%!dt%!s uyse&& a !%!e9ste!t st%#+

    Ca!#e& a !%!e9ste!t %rder

  • 8/9/2019 02 Stacks Queues

    24/66

    Objects:

    A =!te se/ue!#e %- !%des

    Operations:

    Create

    7ush I!sert e&e.e!t at t%)

    T%) Retur! t%) e&e.e!t 7%) Re.%e a!d retur! t%) e&e.e!t

    IsE.)tytest -%r e.)ty!ess

    Sta#+ ADT

  • 8/9/2019 02 Stacks Queues

    25/66

    25

    E9#e)t%!s

    Atte.)t!" the e9e#ut%! %- a!%)erat%! %- ADT .ay s%.et.es#ause a! err%r #%!dt%!, #a&&ed a!

    e9#e)t%! E9#e)t%!s are sad t% 'e *thr%w! 'y

    a! %)erat%! that #a!!%t 'e e9e#uted

    I! the Sta#+ADT, %)erat%!s )%)a!dt%)#a!!%t 'e )er-%r.ed - the sta#+ se.)ty

    Atte.)t!" the e9e#ut%! %- )%)%r t%)

    %! a! e.)ty sta#+ thr%ws a!

  • 8/9/2019 02 Stacks Queues

    26/66

    26

    E9er#se Sta#+s

    Des#r'e the %ut)ut %- the -%&&%w!" seres %- sta#+%)erat%!s

    7ush45

    7ush4F5

    7%)45

    7ush4?5

    7ush45

    7%)45

    7%)45

    7ush4

  • 8/9/2019 02 Stacks Queues

    27/66

    27

    C Ru!8t.e Sta#+

    The C ru!8t.e syste.+ee)s tra#+ %- the #ha! %-a#te -u!#t%!s wth a sta#+

    Whe! a -u!#t%! s #a&&ed, the

    ru!8t.e syste. )ushes %! thesta#+ a -ra.e #%!ta!!" L%#a& ara'&es a!d retur! a&ue

    7r%"ra. #%u!ter, +ee)!" tra#+%- the state.e!t 'e!" e9e#uted

    Whe! a -u!#t%! retur!s, ts-ra.e s )%))ed -r%. thesta#+ a!d #%!tr%& s )assed t%the .eth%d %! t%) %- the sta#+

    main() {

    int i;

    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 02 Stacks Queues

    28/66

    Sta#+s ?

    7are!theses Mat#h!"

    Ea#h *4, *, %r * .ust 'e )aredwth a .at#h!" *5, *J, %r * correct: 4 544 5544 5B5J

    correct: 444 544 5544 5B5J

    incorrect: 544 5544 5B5J

    incorrect: 4 B5J

    incorrect: 4

  • 8/9/2019 02 Stacks Queues

    29/66

    Sta#+s ?e#t %5 !serts

    e&e.e!t % at the e!d %-the /ueue

    de/ueue45 re.%es a!dretur!s the e&e.e!t at the-r%!t %- the /ueue

    Au9&ary /ueue%)erat%!s -r%!t45 retur!s the

    e&e.e!t at the -r%!twth%ut re.%!" t

    se45 retur!s the!u.'er %- e&e.e!tsst%red

    sE.)ty45 retur!s a%%&ea! a&ue !d#at!"whether !% e&e.e!ts are

    st%red E9#e)t%!s

    Atte.)t!" the e9e#ut%!%- de/ueue %r -r%!t %! a!e.)ty /ueue thr%ws a!E.)tyQueueE9#e)t%!

  • 8/9/2019 02 Stacks Queues

    47/66

    47

    E9er#se Queues

    Des#r'e the %ut)ut %- the -%&&%w!" seres %- /ueue%)erat%!s

    e!/ueue45

    e!/ueue4F5

    de/ueue45

    e!/ueue4?5

    e!/ueue45

    de/ueue45

    de/ueue45

    e!/ueue4

  • 8/9/2019 02 Stacks Queues

    48/66

    48

    A))at%!s %- Queues

    Dre#t a))at%!s Wat!" &!es

    A##ess t% shared res%ur#es 4e2"2,

    )r!ter5

    I!dre#t a))at%!s

    Au9&ary data stru#ture -%r a&"%rth.s C%.)%!e!t %- %ther data stru#tures

  • 8/9/2019 02 Stacks Queues

    49/66

    49

    Array8'ased Queue

    Use a! array %- se N! a #r#u&ar -ash%!

    Tw% ara'&es +ee) tra#+ %- the -r%!t a!d rear f !de9 %- the -r%!t e&e.e!t

    r !de9 ..edate&y )ast the rear e&e.e!t

    Array &%#at%! rs +e)t e.)ty

    Q

    0 1 ! rf

    norma" config/ration

    Q

    0 1 ! fr

    ra++e#aro/n# config/ration

  • 8/9/2019 02 Stacks Queues

    50/66

    5

    Queue O)erat%!s

    We use the.%du&%%)erat%r

    4re.a!der %-ds%!5

    Algorithmsize()

    return(N + r f) mod N

    AlgorithmisEmpty()

    return(f = r)

    Q

    0 1 ! rf

    Q

    0 1 ! fr

  • 8/9/2019 02 Stacks Queues

    51/66

    51

    Queue O)erat%!s 4#%!t25

    Algorithmenqueue(o)

    ifsize()=N - 1then

    throw

    FullQueueException

    else

    Q[r]=o

    r=(r + 1) mod N

    O)erat%! e!/ueuethr%ws a! e9#e)t%! -the array s -u&&

    Ths e9#e)t%! s.)&e.e!tat%!8

    de)e!de!t

    Q

    0 1 ! rf

    Q

    0 1 ! fr

  • 8/9/2019 02 Stacks Queues

    52/66

    52

    Queue O)erat%!s 4#%!t25

    O)erat%! de/ueuethr%ws a! e9#e)t%!- the /ueue s e.)ty

    Ths e9#e)t%! ss)e#=ed ! the/ueue ADT

    Algorithmdequeue()

    ifisEmpty()then

    throw

    EmptyQueueException

    else

    o=Q[f]

    f=(f + 1) mod N

    returno

    Q

    0 1 ! rf

    Q

    0 1 ! fr

  • 8/9/2019 02 Stacks Queues

    53/66

    7er-%r.a!#e a!d L.tat%!s8 array8'ased .)&e.e!tat%! %- /ueue ADT

    7er-%r.a!#e Let n'e the !u.'er %- e&e.e!ts ! the

    /ueue

    The s)a#e used s O(n Ea#h %)erat%! ru!s ! t.e O(1

    L.tat%!sThe .a9.u. se %- the /ueue .ust 'e

    de=!ed a priori, a!d #a!!%t 'e #ha!"edTry!" t% e!/ueue a! e&e.e!t !t% a -u&&

    /ueue #auses a! .)&e.e!tat%!8s)e#=#e9#e)t%!

    Gr%wa'&e Array 'ased

  • 8/9/2019 02 Stacks Queues

    54/66

    54

    Gr%wa'&e Array8'asedQueue

    I! a! e!/ueue %)erat%!, whe! thearray s -u&&, !stead %- thr%w!" a!e9#e)t%!, we #a! re)&a#e the array

    wth a &ar"er %!e S.&ar t% what we dd -%r a! array8

    'ased sta#+

    The e!/ueue %)erat%! has a.%rtedru!!!" t.e O(nwth the !#re.e!ta& strate"y

    O(1wth the d%u'&!" strate"y

  • 8/9/2019 02 Stacks Queues

    55/66

    *ectors

    E9er#se

    Des#r'e h%w t% .)&e.e!t a/ueue us!" a s!"&y8&!+ed &st Queue %)erat%!s e!/ueue495,

    de/ueue45, se45, sE.)ty45

    %r ea#h %)erat%!, "e the ru!!!"t.e

    Queue wth a S!"&y L!+ed

  • 8/9/2019 02 Stacks Queues

    56/66

    56

    Queue wth a S!"&y L!+edLst

    We #a! .)&e.e!t a /ueue wth a s!"&y &!+ed &st The -r%!t e&e.e!t s st%red at the head %- the &st

    The rear e&e.e!t s st%red at the ta& %- the &st

    The s)a#e used s O(na!d ea#h %)erat%! %- the Queue ADTta+es O(1 t.e

    NOTE we d% !%t hae the &.tat%! %- the array 'ased.)&e.e!tat%! %! the se %- the sta#+ '# the se %- the &!+ed

    &st s !%t =9ed, I2e2, the /ueue s NEER -u&&2

    f

    r

    no#es

    e"ements

    frontrear

  • 8/9/2019 02 Stacks Queues

    57/66

    57

    I!-%r.a& C Queue I!ter-a#e

    I!-%r.a& C!ter-a#e -%r %urQueue ADT

    Re/ures thede=!t%! %-#&assEmpty3ueueException

    N%#%rres)%!d!"'u&t8! STL #&ass

    template

    class3ueue{

    public:

    int size();

    bool isEmpty();

    Objectront()t!ro"(Empty3ueueException);

    voi#en4ueue(Object o);

    Object #e4ueue()

    t!ro"(Empty3ueueException);$;

  • 8/9/2019 02 Stacks Queues

    58/66

    Queue Su..ary

    Queue O)erat%! C%.)&e9ty -%rDPere!t I.)&e.e!tat%!s

    1'29'15 4(54(5 $)

    *ectors 58

    ArrayFixed-Size

    ArrayExpandable (doublin!"ra"ey#

    $i!"Sinly-$in%ed

    #e:/e/e,- .,1- .,1- .,1-

    en:/e/e,o- .,1- .,n- orst Case.,1- %est Case.,1- $erage Case

    .,1-

    front,- .,1- .,1- .,1-

    Sie,- ism+t,- .,1- .,1- .,1-

    Th D '& E d d Q ADT

  • 8/9/2019 02 Stacks Queues

    59/66

    59

    The D%u'&e8E!ded Queue ADT42F5

    The D%u'&e8E!ded Queue, %rDe/ue,ADT st%res ar'trary%'>e#ts2 47r%!%u!#ed de#+;5

    R#her tha! sta#+ %r /ueueADTs2 Su))%rts !sert%!s a!dde&et%!s at '%th the -r%!t

    a!d the e!d2 Ma! de/ue %)erat%!s

    !sertrst4%'>e#t %5 !sertse&e.e!t % at the 'e"!!!" %-the de/ue

    !sertLast4%'>e#t %5 !sertse&e.e!t % at the e!d %- thede/ue

    Re.%erst45 re.%es a!dretur!s the e&e.e!t at the-r%!t %- the /ueue

    Re.%eLast45 re.%es a!dretur!s the e&e.e!t at thee!d %- the /ueue

    Au9&ary /ueue %)erat%!s =rst45 retur!s the e&e.e!t

    at the -r%!t wth%utre.%!" t

    &ast45 retur!s the e&e.e!t at

    the -r%!t wth%ut re.%!" t se45 retur!s the !u.'er %-

    e&e.e!ts st%red

    sE.)ty45 retur!s a %%&ea!a&ue !d#at!" whether !%e&e.e!ts are st%red

    E9#e)t%!s Atte.)t!" the e9e#ut%! %-

    de/ueue %r -r%!t %! a!e.)ty /ueue thr%ws a!E.)tyDe/ueE9#e)t%!

  • 8/9/2019 02 Stacks Queues

    60/66

    6

    D%u'&y L!+ed Lst

    A d%u'&y &!+ed &st )r%des a!atura& .)&e.e!tat%! %- theDe/ue ADT

    N%des .)&e.e!t 7%st%! a!dst%re

    e&e.e!t

    &!+ t% the )re%us !%de

    &!+ t% the !e9t !%de

    S)e#a& tra&er a!d header !%des

    +re ne!t

    e"em

    trai"er0ea#er no#es'+ositions

    e"ements

    no#e

    De/ue wth a D%u'&y L!+ed

  • 8/9/2019 02 Stacks Queues

    61/66

    61

    De/ue wth a D%u'&y L!+edLst

    We #a! .)&e.e!t a de/ue wth a d%u'&y &!+ed &stThe -r%!t e&e.e!t s st%red at the =rst !%de

    The rear e&e.e!t s st%red at the &ast !%de

    The s)a#e used s O(na!d ea#h %)erat%! %- the

    De/ue ADT ta+es O(1 t.e

    "astfirst

    e"ements

    first

  • 8/9/2019 02 Stacks Queues

    62/66

    6?

    I.)&e.e!t!" De/ues wthD%u'&y L!+ed Lsts

    Here;s asua&at%! %- the#%de -%rre.%eLast452

    7 - d L

  • 8/9/2019 02 Stacks Queues

    63/66

    7er-%r.a!#e a!d L.tat%!s8 d%u'&y &!+ed &st .)&e.e!tat%! %- de/ue ADT

    7er-%r.a!#e Let n'e the !u.'er %- e&e.e!ts ! the

    sta#+

    The s)a#e used s O(n Ea#h %)erat%! ru!s ! t.e O(1

    L.tat%!s NOTE we d% !%t hae the &.tat%! %- the

    array 'ased .)&e.e!tat%! %! the se %-the sta#+ '# the se %- the &!+ed &st s !%t=9ed, I2e2, the de/ue s NEER -u&&2

  • 8/9/2019 02 Stacks Queues

    64/66

    De/ue Su..ary

    De/ue O)erat%! C%.)&e9ty -%rDPere!t I.)&e.e!tat%!s

    1'29'15 4(54(5 $)

    *ectors 64

    ArrayFixed-Size

    ArrayExpandable(doublin !"ra"ey#

    $i!"Sinly-$in%ed

    $i!"&oubly-$in%ed

    remoe;irst,-remoe

  • 8/9/2019 02 Stacks Queues

    65/66

    6

    I.)&e.e!t!" Sta#+s a!d Queueswth De/ues

    Stacks with Deques:

    Queues with Deques:

  • 8/9/2019 02 Stacks Queues

    66/66

    The Ada)t%r 7atter!

    Us!" a de/ue t% .)&e.e!t a sta#+ %r /ueue s a!e9a.)&e %- the ada)t%r )atter!2 Ada)t%r )atter!s.)&e.e!t a #&ass 'y us!" .eth%ds %- a!%ther #&ass

    I! "e!era&, ada)t%r #&asses s)e#a&e "e!era& #&asses

    Tw% su#h a))at%!s88 S)e#a&e a "e!era& #&ass 'y #ha!"!" s%.e.eth%ds2

    Ex: implementing a stack with a deque.88 S)e#a&e the ty)es %- %'>e#ts used 'y a "e!era&ass2

    Ex: Defning an IntegerArrayStack class