Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.

Similar presentations


Presentation on theme: "1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N."— Presentation transcript:

1 1 Joe Meehean

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

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

4 Insert in detail shifts all items at positions greater than the insert position to the right 4 A B C D A B Z C D 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

5 Remove in detail removes an item at position Y shifts items after Y left 5 A B C D A C D Remove at 1

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

7 List Implementations internal view (how data is stored and managed) array list linked lists 7

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

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

10 size: 4 4 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); data

11 size: 4 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); data

12 size: 5 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); data

13 size: 5 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); data tmp

14 size: 5 5 capacity: 5 5 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); data tmp

15 size: 5 5 capacity: 5 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); data tmp

16 16 size: 5 6 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); data

17 17 size: 6 6 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); list.removeAtIndex(3); data

18 18 size: 6 5 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); list.removeAtIndex(3); data

19 19 size: 6 5 capacity: 10 ArrayList list; // insert a bunch of stuff... list.add(45); list.add(32); list.removeAtIndex(3); data

20 20

21 Get size O(1) Get the Mth item (e.g., data[M]) O(1) Clear the list O(1) 21

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

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

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

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

26 26


Download ppt "1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N."

Similar presentations


Ads by Google