Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList."— Presentation transcript:

1 CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList

2 Question of the Day What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side. A A B B 4 4 7 7

3 Deque ADT Another type of Collection class Stands for Double Ended QUEue  Combines ideas from Stack and Queue ADT Manipulate elements at front & rear  addFront(), addLast()  removeFront(), removeLast()  getFront(), getLast() Can be implemented with array or linked list  If using linked list, should be doubly-linked list

4 List ADT Sometime want use of entire Collection  Add new element before existing one  Get the 3 rd element in the Collection  Loop over all elements without removing them  Cannot be done with Stack, Queue & Deque Lists can access all of its elements  But provide different means for this access  Will discuss over next several lectures

5 IndexList ADT Also called a Vector  Names are used interchangeably First example of a List Extends idea of an array  Stores arbitrary sequence of elements  Elements may appear multiple times  Access elements using integer rank  Like array indexes, does not imply ordering

6 Ranks ArrayList organizes collection using rank  Item at front of list has rank of 0  2 nd item has rank of 1  3 rd item has rank of 2  n th item has rank of n – 1 Ranks increase sequentially  Merely discusses position of element  Cannot skip over a rank  Cannot repeat a rank

7 IndexList Interface public interface IndexList extends Collection { public void add(int rank, E e) throws IndexOutOfBoundsException; public E get(int rank) throws IndexOutOfBoundsException; public E remove(int rank) throws IndexOutOfBoundsException; public E set(int rank, E newValue) throws IndexOutOfBoundsException; }

8 IndexList != array Extends the idea of an array, but…  IndexList does not have constant size  Elements’ ranks may change over time Can implement using:  Array,  Singly-linked list,  Doubly-linked list,  Specially trained monkeys,  College students

9 Insertion add( r, e ) “shifts” existing elements down to make room for e  For linked-list, insert node at proper location  For array, must shift elements down Can take O(n) time S 012n r S 012n r S 012n e r

10 Deletion remove( r ) “shifts” remaining elements up to fill hole created by removal  For linked-list, happens automatically  For array, must shift elements up Can also take O(n) time S 012n r S 012n r S 012n r

11 IndexList’s Operations add, get, & remove similar to past operations  add ≈ addFront, addLast, enqueue, push  get ≈ getFront, getLast, front, top  remove ≈ removeFront,removeLast,dequeue,pop But, set is a brand new operation  Stores the new element at the given rank  Removes (& returns) element already there  Does not change rank of other elements

12 IndexList’s Exceptions Any of these methods may throw IndexOutOfBoundsException Thrown when rank is illegal for operation add accepts ranks 0 - size()  add(0, e)  e added to front of List  add(size(), e)  e added at end of List get, set, & remove use ranks 0 - size()-1  Ranks start at 0, so no element at size() rank

13 Implement IndexList with array Can allocating & copy into larger array  Increase array length by constant, c -or-  Double length of the array each time Both approaches have O( n ) complexity  Cost of copying entire array But have different amortized complexities  Consider cost of growth due to n calls to add()

14 Constant Growth Need to grow k = n / c times  Copy entire array with each growth, so total copies is: 1 + ( c +1) + (2 c +1) + + ((( k -1) *c )+1) + ( k*c +1) = (( k * c ) + 2) + (( k * c ) + 2) + + (( k * c ) + 2) = k / 2 * (( k * c ) + 2) = O( c*k 2 )= O( c * ( n / c ) 2 ) = O( n 2 * 1 / c ) = O( n 2 )  Average cost: O( n 2 ) / n = O( n )

15 Doubling Growth Array grows k = log n times  Still copy entire array, for this many copies: 1 + 2 + 4 + + 2 k -1 + 2 k = (2 k - 1) + 2 k = 2 k +1 - 1 = (2 * 2 k ) - 1 = (2 * 2 log n ) - 1 = O(2 n - 1) = O( n )  Average cost: O( n ) / n = O(1) But, moving elements in add() still costs O( n )!

16 Your Turn Get back into groups and do activity

17 Before Next Lecture… Keep up with your reading! Continue Week #9 Assignment Work on Programming Assignment #2


Download ppt "CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList."

Similar presentations


Ads by Google