lists stacks

Upload: kamau-gabriel

Post on 06-Jul-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 Lists Stacks

    1/25

    Chapter 3:Chapter 3:Abstract DataAbstract Data

    TypesTypes

    Lists, Stacks

    Lydia Sinapova, Simpson College

    Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

  • 8/16/2019 Lists Stacks

    2/25

    2

     An ADT  

    • a set of objects

    • a set of operations 

    Same set of objects ,dierent sets of operations =>

    diferent ADTs 

    ADTs are implemented using classes,hiding implementation details:

      encapsulation

  • 8/16/2019 Lists Stacks

    3/25

    3

    LIST ABSTRACTION 

    Denitin:

    A linear congurationof elements, called

    nodes

  • 8/16/2019 Lists Stacks

    4/25

    4

    Characteristics

    !nsert and delete nodes in an" order

     The nodes are cnnected

    #ach node has t! components!nformation $data%

    &in' to the ne(t node

     The nodes are accessed through the

    lin's bet)een them

  • 8/16/2019 Lists Stacks

    5/25

    5

    "ead #redecessr $ %

    Nde%

    S&ccess'r $ %

    tai(

    *or each node the node that isin front of it is calledpredecessr)

     The node that is after it iscalled s&ccessr

  • 8/16/2019 Lists Stacks

    6/25

    6

    Ter*in(+y

    "ead $rnt, rst nde-: 

     The node )ithout predecessor, the node thatstarts the lists

    Tai( end, (ast nde-:  The node that has no successor, the last

    node in the list

    C&rrent nde:  The node being processed) *rom the current node )e can access the ne(t node

    .*pty (ist:  +o nodes e(ist

  • 8/16/2019 Lists Stacks

    7/25

    7

    Basic peratins

     To createdestro" a list

     To e(pandshrin' the list

    -ead.rite operations

    /hanging the current node $mo0ingalong the list%

     To report current position in the list

     To report status of the list

  • 8/16/2019 Lists Stacks

    8/25

    8

    ADT List Ntatin

    L ' list

    e ' item of the same t"pe asthe

    information part of an

    element$a node% in the list

    b ' boolean 0alue

  • 8/16/2019 Lists Stacks

    9/25

    9

    Operatins in ADT

    NtatinInsertL,e-!nserts a node )ith information e beforethe current position 

    De(eteL-

    Deletes the current node in & , the

    current position indicates the ne(t node

    Retrie/eIn$L- e

    -eturns the information in the current

  • 8/16/2019 Lists Stacks

    10/25

    10

    Insertin and De(etin

    A) Insertin

     To insert a node % bet)een thenodes A and B:

    /reate a lin' from % to B

    /reate a lin' from A to %, 

  • 8/16/2019 Lists Stacks

    11/25

    11

    Insertin

      X

      A B

  • 8/16/2019 Lists Stacks

    12/25

    12

    Insertin and De(etin

    B) De(etin

     To delete a node % bet)een A and B:

    • /reate a lin' from A to B,• -emo0e node %

  • 8/16/2019 Lists Stacks

    13/25

    13

    De(etin

      A X B

  • 8/16/2019 Lists Stacks

    14/25

    14

    Nde Linkin+

    0) Sin+(e (inked (ists : 

    #ach node contains t)o lin's 1 to thepre0ious and to the ne(t node

    1) D&b(e (inked (ists : 

    #ach node contains a lin' onl" to the ne(t

    node

    3) Circ&(ar (ists: The tail is lin'ed to the head 

  • 8/16/2019 Lists Stacks

    15/25

    15

    List I*p(e*entatin

    Static 2 using an arra"

    Dyna*ic 2 using linearnodes

  • 8/16/2019 Lists Stacks

    16/25

    16

    Array I*p(e*entatin

     T)o para((e( arrays are used:

    Inde2 array 1 the numberstored in the i1th element sho)sthe inde( of the 3ne(t3 node ,

    ie node that follo)s the i1thnode

    •Data array 1 used to store the

  • 8/16/2019 Lists Stacks

    17/25

    17

  • 8/16/2019 Lists Stacks

    18/25

    18

    STACS

    Denitin: 

    The (ast stred e(e*ent

    is the rst t be accessedLI4O: (ast in ' rst

    &t-

  • 8/16/2019 Lists Stacks

    19/25

    19

    Basic peratins

    #&sh: store a data item

    at the top of the stac'

    #p: retrie0e a dataitem from the top of thestac'

  • 8/16/2019 Lists Stacks

    20/25

    20

    ADT Denitin $ STAC 

    Ntatin:

    S stac'

    e item of same t"pe as

    theelements of S

    b boolean 0alue

  • 8/16/2019 Lists Stacks

    21/25

    21

    Operatins

    Init5StackS-4rocedure to initiali5e S to an

    empt" stac'

    Destry5StackS-4rocedure to delete all elements in

  • 8/16/2019 Lists Stacks

    22/25

    22

    Operatins

    Stack5.*ptyS- b6oolean function that returns

     T-7# if S is empt"

    Stack54&((S- b6oolean function that returns

     T-7# if S is full

  • 8/16/2019 Lists Stacks

    23/25

  • 8/16/2019 Lists Stacks

    24/25

  • 8/16/2019 Lists Stacks

    25/25

    25

    A(+rith*

    0) e8 ← 4op$S%1) !hi(e stack is nt e*pty

    repeat98 e9 ←  pop$S%

    99 push$S, e8e9%

      9; e8 ←pop $S%

    3 push$S e8%