260 likes | 396 Vues
This guide provides a comprehensive overview of the List Abstract Data Type (ADT), detailing its structure, expected operations, and the differences between lists and arrays. It covers fundamental operations like adding, inserting, removing, and retrieving items, along with an explanation of the impact of these operations on performance. Additionally, the document discusses the implementation of lists using arrays and linked lists in C++, highlighting the Standard Template Library's vector class. Ideal for developers looking to deepen their understanding of list structures in programming.
E N D
The List ADT & Array Lists Joe Meehean
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
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
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
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
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
Array List • List Implementations • internal view (how data is stored and managed) • array list • linked lists
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
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
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
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
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
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
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
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
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
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
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
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
Array List Algorithm Analysis • Get size • O(1) • Get the Mth item (e.g., data[M]) • O(1) • Clear the list • O(1)
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)
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;
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
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