260 likes | 386 Views
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.
E N D
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 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; }
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; }
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
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
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
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
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
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!
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!
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?
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
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
Key Concept For Rest of Week Nodein linked list equivalent to index in array
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()
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
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
Your Turn • Get into your groups and complete activity
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