1 joe meehean. ordered collection of items not necessarily sorted 0-index (first item is item 0)...

26
The List ADT & Array Lists 1 Joe Meehean

Upload: jorge-grimshaw

Post on 30-Mar-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1

1 Joe Meehean Slide 2 Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 Item N Slide 3 Expected operations add to end (append) insert at position X membership (does it contain) size is empty? get item at position X remove item at position X 3 Slide 4 Insert in detail shifts all items at positions greater than the insert position to the right 4 A B C D 0 1 2 3 A B Z C D 0 1 2 3 4 Insert Z at 2 Cannot insert past end of list e.g., insert X at 6 Can insert at the end e.g., insert X at 5 Slide 5 Remove in detail removes an item at position Y shifts items after Y left 5 A B C D 0 1 2 3 A C D 0 1 2 Remove at 1 Slide 6 Similarities ordered collections of objects indexed at 0 Differences lists can grow or shrink dont need to know data size in advance size is always number of items, not capacity lists cant add elements at arbitrary position no inserts past end can insert anywhere in array 6 Slide 7 List Implementations internal view (how data is stored and managed) array list linked lists 7 Slide 8 Array List list implementation using an array implementation hidden in a C++ class data stored in an array stores a counter of number of elements Wrap list functionality around an array Special Cases out of bounds: insert, add, get, remove array is full: insert and add 8 Slide 9 Out of bounds index outside of range of list throw an exception OR return garbage (very C++) Array is full allocate a bigger array copy old data to new array insert new item 9 Slide 10 10 0234 size: 4 4 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); 17 5 5 4 4 11 1 data Slide 11 11 0234 size: 4 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); 17 5 5 4 4 11 45 1 data Slide 12 12 0234 size: 5 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); 17 5 5 4 4 11 45 1 data Slide 13 13 0234 size: 5 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); 17 5 5 4 4 11 45 1 data 02341 tmp 56 789 Slide 14 14 0234 size: 5 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); 17 5 5 4 4 11 45 1 data 0234 17 5 5 4 4 11 45 1 tmp 56 789 Slide 15 15 0234 size: 5 5 capacity: 5 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); 17 5 5 4 4 11 45 1 data 0234 17 5 5 4 4 11 45 1 tmp 56 789 Slide 16 16 size: 5 6 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); data 0234 17 5 5 4 4 11 45 1 32 56 789 Slide 17 17 size: 6 6 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); list.removeAtIndex(3); data 0234 17 5 5 4 4 11 45 1 32 56 789 Slide 18 18 size: 6 5 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); list.removeAtIndex(3); data 0234 17 5 5 4 4 11 45 1 32 56 789 Slide 19 19 size: 6 5 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); list.removeAtIndex(3); data 0234 17 5 5 4 4 45 32 156 789 Slide 20 20 Slide 21 Get size O(1) Get the Mth item (e.g., data[M]) O(1) Clear the list O(1) 21 Slide 22 Add at the end avg case: O(1) worst case: O(N) Add to the front O(N) Remove at the end O(1) Remove at the front O(N) 22 Slide 23 Standard Template Library (STL) provides many data structures for C++ including lists Array List is implemented by STLs vector vector is a template class vector e.g., vector intList; 23 Slide 24 int size() const returns the number of elements stored void clear() removes all elements from vector void push_back(const T& t) adds t to the end of the list void pop_back() removes the last item from the list 24 Slide 25 T& operator[](int idx) returns a reference to the object at index gives mutable access to this object e.g., given vector v v[5] = 15; // 6 th element is now 15 v[5]++; // 6 th element is now 16 provides no bound checking Can also remove items using Iterators coming soon 25 Slide 26 26