csc 212 data structures n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 28 : Array-Based List PowerPoint Presentation
Download Presentation
Lecture 28 : Array-Based List

Loading in 2 Seconds...

play fullscreen
1 / 14

Lecture 28 : Array-Based List - PowerPoint PPT Presentation


  • 166 Views
  • Uploaded on

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

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

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


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.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
list array
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
arraylist concept usually
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
arraylist indices usually
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
something about list s methods
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
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
how to solve this
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
what about add methods
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
insertion
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
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
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
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
your turn
Your Turn
  • Get into your groups and complete activity
for next lecture
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