1 / 12

a list = a Linear Recursive Structure (LRS or LRStruct)

a list = a Linear Recursive Structure (LRS or LRStruct). What is a list? the empty list is a list a pair whose tail is a list is itself a list This is a recursive definition! (1) is called the base case, and (2) is called the recursive case.

media
Télécharger la présentation

a list = a Linear Recursive Structure (LRS or LRStruct)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. a list = a Linear Recursive Structure(LRS or LRStruct) • What is a list? • the empty list is a list • a pair whose tail is a list is itself a list • This is a recursive definition! (1) is called the base case, and (2) is called the recursive case. • Note that traditional implementations do not follow this precise definition of what a list is: • many have no explicit representation of an empty list • none are recursive on the list; instead they recurse on a list node • This has implications for how the structure can support extension (see Visitor support, in later slides)

  2. States • A list can therefore be in one of two states: • empty (corresponding to the base case) • non-empty (corresponding to the recursive case) • The state-based implementation we will study makes this distinction explicit in the representation

  3. Empty vs. NonEmpty state(look Ma, no NullPointerException!) • An LRS object delegates all calls to its LRS State object – which can respond to all messages. • Empty and NonEmpty states respond differently. • There is never a null pointer in the structure! (Think about the implications of this for a while.)

  4. What is basic (invariant) list functionality? • insert new item at front • remove item from front • set/get first item (head) • set/get rest (tail) • plus (in Java) methods inherited from Object (toString, equals, etc)

  5. How are these methods defined? public LRStruct<E> insertFront(E item) { return _state.insertFront(this, item); } public E removeFront() { return _state.removeFront(this); } public LRStruct<E> setDatum(E item) { return _state.setDatum(this, item); } public E getDatum() { return _state.getDatum(this); } public LRStruct<E> setRest(LRStruct<E> rest) { return _state.setRest(this, rest); } public LRStruct<E> getRest() { return _state.getRest(this); }

  6. LRStruct delegates to State! • All of these methods delegate to the state of the LRStruct. • States polymorphically determine what happens.

  7. a An empty LRS • LRS a = new LRS(); LRS Empty _state

  8. a Inserting an item into an empty LRS • a.insertFront(“fred”); LRS Empty _state

  9. Empty a Inserting an item into an empty LRS • a.insertFront(“fred”); LRS _state

  10. Empty a _state Inserting an item into an empty LRS • a.insertFront(“fred”); LRS _dat=“fred” _tail NonEmpty

  11. Empty a _state _state Inserting an item into an empty LRS • a.insertFront(“fred”); LRS _dat=“fred” _tail NonEmpty LRS

  12. Empty a _state _state Inserting an item into an empty LRS • a.insertFront(“fred”); LRS _dat=“fred” _tail NonEmpty LRS

More Related