1 / 26

The List ADT & Array Lists

The List ADT & Array Lists. Joe Meehean. List ADT. Item 0 Item 1 Item 2 … Item N. Ordered collection of items n ot necessarily sorted 0-index (first item is item 0) Abstraction. List ADT. Expected operations a dd to end (append) i nsert at position X membership (does it contain…)

mohawk
Download Presentation

The List ADT & Array Lists

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The List ADT & Array Lists Joe Meehean

  2. List ADT Item 0 Item 1 Item 2 … Item N • Ordered collection of items • not necessarily sorted • 0-index (first item is item 0) • Abstraction

  3. List ADT • 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

  4. List ADT 0 1 2 3 0 1 2 3 4 Insert Zat 2 A B C D A B Z C D • Cannot insert past end of list • e.g., insert X at 6 • Can insert at the end • e.g., insert X at 5 • Insert in detail • shifts all items at positions greater than the insert position to the right

  5. List ADT 0 1 2 3 0 1 2 Remove at 1 A B C D A C D • Remove in detail • removes an item at position Y • shifts items after Y left

  6. Lists vs Arrays • Similarities • ordered collections of objects • indexed at 0 • Differences • lists can grow or shrink • don’t need to know data size in advance • size is always number of items, not capacity • lists can’t add elements at arbitrary position • no inserts past end • can insert anywhere in array

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

  8. Array List • 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

  9. Array List • 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

  10. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); 0 1 2 3 4 data 5 11 17 4 size: 4 capacity: 5

  11. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); 0 1 2 3 4 data 5 11 17 4 45 size: 4 5 capacity: 5

  12. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); 0 1 2 3 4 data 5 11 17 4 45 size: 5 capacity: 5

  13. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); 9 8 7 6 0 1 2 3 4 5 tmp 0 1 2 3 4 data 5 11 17 4 45 size: 5 capacity: 5

  14. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); 9 8 7 6 0 1 2 3 4 5 tmp 5 11 17 4 45 0 1 2 3 4 data 5 11 17 4 45 size: 5 capacity: 5

  15. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); 9 8 7 6 0 1 2 3 4 5 tmp 5 11 17 4 45 0 1 2 3 4 data 5 11 17 4 45 size: 5 capacity: 5 10

  16. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); 9 8 7 6 0 1 2 3 4 5 5 11 32 17 4 45 data size: 5 6 capacity: 10

  17. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); list.removeAtIndex(3); 9 8 7 6 0 1 2 3 4 5 5 11 32 17 4 45 data size: 6 capacity: 10

  18. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); list.removeAtIndex(3); 9 8 7 6 0 1 2 3 4 5 5 11 32 17 4 45 data size: 6 5 capacity: 10

  19. Array List ArrayList list; // insert a bunch of stuff ... list.add(45); list.add(32); list.removeAtIndex(3); 9 8 7 6 0 1 2 3 4 5 5 45 17 4 32 data size: 6 5 capacity: 10

  20. Questions?

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

  22. Array List Algorithm Analysis • 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)

  23. Lists in C++ • Standard Template Library (STL) • provides many data structures for C++ • including lists • Array List is implemented by STL’s vector • vector is a template class • vector<T> • e.g., vector<int> intList;

  24. vector<T> • 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

  25. vector<T> • T& operator[](intidx) • returns a reference to the object at index • gives mutable access to this object • e.g., given vector vv[5] = 15; // 6th element is now 15v[5]++; // 6th element is now 16 • provides no bound checking • Can also remove items using Iterators • coming soon

  26. Questions?

More Related