Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  What do you get when you cross a mountain climber and a grape?

Similar presentations


Presentation on theme: "Problem of the Day  What do you get when you cross a mountain climber and a grape?"— 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

4 public interface List extends Collection { public E removeFirst() throws EmptyCollectionException; public E removeLast() throws EmptyCollectionException; public E remove(E elem) throws ElementNotFoundException; public E first() throws EmptyCollectionException; public E last() throws EmptyCollectionException; public boolean contains(E elem); public get(int i) throws EmptyCollectionException; } List Interface

5 public interface OrderedList extends List { public void add(E elem); } public interface UnorderedList extends List { public void addToFront(E elem); public void addToRear(E elem); public void addAfter(E elem, E tgt) throws ElementNotFoundException; } List Subinterfaces

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 012 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 012 e n-1 i

8 Something About L IST ’s Methods

9 What They Have In Common  All methods first search through the L IST  Searching differs if L IST ordered or unordered  Each method has different action with found element  Could  Could rewrite 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 L IST  Searching differs if L IST ordered or unordered  Each method has different action with found element  Could  Could rewrite 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

12

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 L IST implementations not related  All of the code rewritten between types of L IST s  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  Node s organize data held within a linked list  In linked list-based L IST, find() returns Node  Can move forward & backward if doubly-linked  If element not in L IST, 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 Node in 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 L IST ’s ends  Middle of L IST 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 L IST ’s ends  Middle of L IST 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 MethodArrayListLinkedList removeFirst() O(n)O(n)O(1) removeLast() O(1) addToFront() O(n)O(n)O(1) addToRear() O(1) (amortized)O(1) addAfter() O(n)O(n)O(n)O(n) first(), last() O(1) add(), contains(), remove() O(n)O(n)O(n)O(n)

20 ArrayList v. LinkedList MethodArrayListLinkedList removeFirst() O(n)O(n) removeLast() addToFront() O(n)O(n) addToRear() O(1) (amortized) addAfter() O(n)O(n)O(n)O(n) first(), last() O(1) add(), contains(), remove() O(n)O(n)O(n)O(n)

21 ArrayList v. LinkedList MethodArrayListLinkedList removeFirst() O(n)O(n) removeLast() addToFront() O(n)O(n) addToRear() O(1) (amortized) addAfter() O(n)O(n)O(n)O(n) first(), last() O(1) add(), contains(), remove() O(n)O(n)O(n)O(n) get(), addAt() O(n)O(n)

22  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  L IST ADTs differ in how they provide access  A RRAY L IST ’s indices give quick access to specific position  Good at working at relative location with LinkedList L IST ADT

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 Iterator and what does it do?  How do we connect ideas of Iterable & L IST ?  What are for-each loops & why do they make life easy?  Week #10 assignment  Week #10 assignment posted to Angel  As usual, will be due tomorrow


Download ppt "Problem of the Day  What do you get when you cross a mountain climber and a grape?"

Similar presentations


Ads by Google