1 / 26

Problem of the Day

Problem of the Day. What do you get when you cross a mountain climber and a grape?. Problem of the Day. What do you get when you cross a mountain climber and a grape? Nothing, you cannot cross a scalar. CSC 212 – Data Structures. Lecture 29: linked list-Based List. List Interface.

Télécharger la présentation

Problem of the Day

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. Problem of the Day • What do you get when you cross a mountain climber and a grape?

  2. Problem of the Day • What do you get when you cross a mountain climber and a grape? • Nothing, you cannot cross a scalar.

  3. CSC 212 – Data Structures Lecture 29:linked list-Based List

  4. List Interface public interface List<E>extends Collection {public EremoveFirst() throws EmptyCollectionException;public EremoveLast() throws EmptyCollectionException;public Eremove(E elem) throws ElementNotFoundException;public Efirst() throws EmptyCollectionException;public Elast() throws EmptyCollectionException;public booleancontains(E elem);public get(inti)throwsEmptyCollectionException; }

  5. List Subinterfaces public interface OrderedList<E>extends List<E>{publicvoidadd(E elem); } public interface UnorderedList<E>extends List<E>{public voidaddToFront(E elem);public voidaddToRear(E elem);public voidaddAfter(E elem, Etgt)throws ElementNotFoundException; }

  6. Array-based List.addAt() • addAt(i, e)“shifts” elements to make space • Needs to make hole in array to place element • Can take O(n) time for this shifting • When adding list’s end may take O(1) time, but… • Constant time amortizes cost of growing array 0 1 2 e n-1 i

  7. Array-based List.remove() • remove(e)“shifts” elements down to fill hole • Not only way, but other options are difficult & slow • O(n)time required in the general case • But for specific situations could take only O(1) time • But must consider worst case when computing big-Oh 0 1 2 e n-1 i

  8. Something About List’s Methods • contains()checks if elementalready included • Requires comparing with elements currently in List • remove()removes element IF in List already • Requires comparing with elements currently in List • add()keeps order by placing element correctly OR • addAfter()places element after finding target • Requires comparing with elements currently in List

  9. What They Have In Common • All methods first search through the List • Searching differs if List ordered or unordered • Each method has different action with found element • Couldrewrite code, but want to be VERY lazy • Use private method that returns index where found • All (public) methods then rely upon this private one

  10. What They Have In Common • All methods first search through the List • Searching differs if List ordered or unordered • Each method has different action with found element • Couldrewrite code, but want to be VERY lazy • Use private method that returns index where found • All (public) methods then rely upon this private one

  11. What They Have In Common • All methods first search through the List • Searching differs if List ordered or unordered • Each method has different action with found element • Couldrewrite code, but want to be VERY lazy • Use private method that returns index where found • All (public) methods then rely upon this private one WAIT!

  12. What They Have In Common • All methods first search through the List • Searching differs if List ordered or unordered • Each method hasdifferent action with found element • Couldrewrite code, but want to be VERY lazy • Use private method that returns indexwhere found • All (public) methods thenrely upon this private one WAIT!

  13. What Does find() Do? • find() returns location element found • Array organized via indices, so returning int logical • Linked list has no indices & not centrally organized • Must we write separate find()methods?

  14. What Does find() Do? • find() returns location element found • Array organized via indices, so returning int logical • Linked list has no indices & not centrally organized • Must we write separate find()methods? • The different Listimplementations not related • All of the code rewritten between types of Lists • This duplication is not & should not be a surprise

  15. What Does find() Do? • find() returns location element found • Array organized via indices, so returning int logical • Linked list has no indices & not centrally organized • Nodes organize data held within a linked list • In linked list-based List, find() returns Node • Can move forward & backward if doubly-linked • If element not in List, method can return null • Bigger generalization from this discussion • Node in linked list equivalent to index in array

  16. Key Concept For Rest of Week Nodein linked list equivalent to index in array

  17. What About add*() Methods? • Work similar among these methods, too • All of these methods must create new Node • Update size field in each of these • Would still like to avoid having to duplicate code • Difference is WHERE node will be needed • addFirst() & addRear()work at List’s ends • Middle of List used (usually) by add() & addAfter()

  18. What About add*() Methods? • Work similar among these methods, too • All of these methods must create new Node • Update size field in each of these • Would still like to avoid having to duplicate code • Difference is WHERE node will be needed • addFirst() & addRear()work at List’s ends • Middle of List used (usually) by add() & addAfter() • Must write each; little overlapping code here • prev & next set differently;bulk of code is there

  19. ArrayList v. LinkedList

  20. ArrayList v. LinkedList

  21. ArrayList v. LinkedList

  22. List ADT • Collection which we can access all elements • Add element after an existing one • Collection can remove any element it contains • Loop over all elements without removing them • List ADTs differ in how they provide access • ArrayList’s indices give quick access to specific position • Good at working at relative location with LinkedList

  23. Does Absolute Position Matter?

  24. Relativity Can Be Important

  25. Your Turn • Get into your groups and complete activity

  26. For Next Lecture • Read section 7.1 – 7.2 for Wednesday’s lecture • What is an Iteratorand what does it do? • How do we connect ideas of Iterable & List? • What are for-each loops & why do they make life easy? • Week #10 assignment posted to Angel • As usual, will be due tomorrow

More Related