CH 7 LIST AND ITERATOR ADTS ACKNOWLEDGEMENT THESE

  • Slides: 23
Download presentation
CH 7. LIST AND ITERATOR ADTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED

CH 7. LIST AND ITERATOR ADTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016)

LIST ADT

LIST ADT

EXAMPLE • A sequence of List operations:

EXAMPLE • A sequence of List operations:

ARRAY LISTS • 0 0 1 2 i n

ARRAY LISTS • 0 0 1 2 i n

INSERTION • A 0 1 2 i n 0 1 2 o i A

INSERTION • A 0 1 2 i n 0 1 2 o i A A n

ELEMENT REMOVAL • A 0 1 2 o i n 0 1 2 r

ELEMENT REMOVAL • A 0 1 2 o i n 0 1 2 r A A n

PERFORMANCE •

PERFORMANCE •

EXERCISE: • Implement the Deque ADT update functions using List functions • Deque update

EXERCISE: • Implement the Deque ADT update functions using List functions • Deque update functions: • first(), last(), add. First(e), add. Last(e), remove. First(), remove. Last(), size(), is. Empty() • List functions: • get(i), set(i, e), add(i, e), remove(i), size(), is. Empty()

LIST SUMMARY Array Fixed-Size or Expandable add(i, e), remove(i) get(i), set(i, e) size(), is.

LIST SUMMARY Array Fixed-Size or Expandable add(i, e), remove(i) get(i), set(i, e) size(), is. Empty() List Singly or Doubly Linked

POSITIONAL LISTS •

POSITIONAL LISTS •

POSITIONAL LIST ADT • Accessor methods:

POSITIONAL LIST ADT • Accessor methods:

POSITIONAL LIST ADT, 2 • Update methods:

POSITIONAL LIST ADT, 2 • Update methods:

EXAMPLE • A sequence of Positional List operations:

EXAMPLE • A sequence of Positional List operations:

POSITIONAL LIST IMPLEMENTATION prev next • The most natural way to implement a positional

POSITIONAL LIST IMPLEMENTATION prev next • The most natural way to implement a positional list is with a doubly-linked list. header element nodes/positions elements node trailer

INSERTION, E. G. , ADDAFTER(P, E) p A B C p A q B

INSERTION, E. G. , ADDAFTER(P, E) p A B C p A q B C X p A q B X C

REMOVE(P) p A B C D p D A B C

REMOVE(P) p A B C D p D A B C

PERFORMANCE •

PERFORMANCE •

POSITIONAL LIST SUMMARY List Singly-Linked first(), last(), add. First(), add. Last(), add. After() add.

POSITIONAL LIST SUMMARY List Singly-Linked first(), last(), add. First(), add. Last(), add. After() add. Before(p, e), erase() size( ), is. Empty() List Doubly- Linked

ITERATORS • An iterator is a software design pattern that abstracts the process of

ITERATORS • An iterator is a software design pattern that abstracts the process of scanning through a sequence of elements, one element at a time.

THE ITERABLE INTERFACE • Java defines a parameterized interface, named Iterable, that includes the

THE ITERABLE INTERFACE • Java defines a parameterized interface, named Iterable, that includes the following single method: • iterator(): Returns an iterator of the elements in the collection. • An instance of a typical collection class in Java, such as an Array. List, is Iterable (but not itself an iterator); it produces an iterator for its collection as the return value of the iterator() method. • Each call to iterator() returns a new iterator instance, thereby allowing multiple (even simultaneous) traversals of a collection.

THE FOR-EACH LOOP • Java’s Iterable class also plays a fundamental role in support

THE FOR-EACH LOOP • Java’s Iterable class also plays a fundamental role in support of the “for-each” loop syntax: • is equivalent to:

INTERVIEW QUESTION 1 • Write code to partition a list around a value x,

INTERVIEW QUESTION 1 • Write code to partition a list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. GAYLE LAAKMANN MCDOWELL, "CRACKING THE CODE INTERVIEW: 150 PROGRAMMING QUESTIONS AND SOLUTIONS", 5 TH EDITION, CAREERCUP PUBLISHING, 2011.

INTERVIEW QUESTION 2 • Implement a function to check if a list is a

INTERVIEW QUESTION 2 • Implement a function to check if a list is a palindrome. GAYLE LAAKMANN MCDOWELL, "CRACKING THE CODE INTERVIEW: 150 PROGRAMMING QUESTIONS AND SOLUTIONS", 5 TH EDITION, CAREERCUP PUBLISHING, 2011.