Lecture 28 : Array-Based List

1 / 14

# Lecture 28 : Array-Based List - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Lecture 28 : Array-Based List' - andren

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
• Other Faculty Members
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
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
• 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 should differ if List ordered or unordered
• Couldrewrite code…… but violates laziness goal
• Use private method that returns index where found
• All (public) methods then rely upon this private one
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
• 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
• Create protected addAt()method in superclass
• Specify element & index as parameters to this method
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

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

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