1 / 14

Lecture 28 : Array-Based List

CSC 212 – Data Structures. Lecture 28 : Array-Based List. List ≠ array. List at times resembles concept of array, but … … no preset size of a List In List, index where element found may change Could implement an List using: Array Linked list Administrator/Faculty

andren
Télécharger la présentation

Lecture 28 : Array-Based List

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. CSC 212 – Data Structures Lecture 28:Array-Based List

  2. List ≠ array • List at times resembles concept of array, but… • … no preset size of a List • In List, index where element found may change • Could implement an List using: • Array • Linked list • Administrator/Faculty • Other Faculty Members

  3. ArrayListConcept (Usually) • Will also see this called Vector & IndexList • Used interchangeably; depends on where you look • Based upon aconceptual resizable array • Arbitrary sequence of elements stored within list • IndexListkeeps elements indexed from 0 to n-1 • Rank is meaningless; only gives position in list

  4. ArrayListIndices (Usually) • Index used by ArrayListto organize elements • List uses index of 0 for item at front • 2nd item at index 1in the IndexList • IndexListstores 3rd item at index 2 • nth item at index n-1in the IndexList • ArrayListstores elements in sequential indices • Index is absolute position of element in the list • Indices must be sequential & cannot be blank • Cannot be repeated; 1 element per index at a time

  5. 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

  6. What They Have In Common • All methods first search through the List • Searching should differ if List ordered or unordered • Once item found (or not found) differences occur • Couldrewrite code…… but violates laziness goal • Use private method that returns index where found • All (public) methods then rely upon this private one

  7. How To Solve This? • How we go about search will differ by List type • Search all elements in List when data unordered • Only search through smaller elements if ordered • Couldrewrite all public methods in all classes…… still violates laziness goal • Make private method abstractin List superclass • Means that cannot use superclass (lacking add methods) • Override method in subclasses where approach known

  8. What About add*() Methods? • Code similar among these methods, too • All of these methods must make space in array • Identical updates to needed fields by each of these • Would still like to avoid having to duplicate code • Difference is WHERE space will be needed • addFirst() & addRear()work at List’s ends • Middle of List used (usually) by add() & addAfter() • Create protected addAt()method in superclass • Specify element & index as parameters to this method

  9. Insertion • To make room for e, addAt()shifts elements • Move to higher indices in array to make room • Process requires O(n) time • Copyelements at index i & higher for space in array 0 1 2 n-1 i

  10. Deletion • remove() “shifts” elements down to fill hole • Remember: not all array locations contain elements • Copy elements down from indices holding data 0 1 2 e n-1 i

  11. Array-based IndexList • Do not want size limits restricting size of object • That this has no preset size is main advantage • ADT hides complexity of changing array’s size • When calling add()with array full • Create larger array and copy values into it • Larger array aliased by field… • …without revealing secret to anyone

  12. Ways to Grow Array • Two ways to increase array size • Constant value (e.g., 2, 4, 6, 8, 10…) • Constant factor (e.g., 2, 4, 8, 16, 32…) • Both approaches requires O(n) time… • Instantiating & copying array are slow steps • …average (amortized) costs differ, however • Difference in how often slow step needed

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

  14. For Next Lecture • Read section 6.7 for Monday’s lecture • How do LinkedList & a linked list differ? • What is advantage of LinkedListover ArrayList? • Week #10 assignment posted to Angel • As usual, will be due next Tuesday • Enjoy respite from crushing deadlines during lull • Get ready to move on; project #2 available in one week

More Related