Lists
Posted on August 1st, 2022
Updated on August 2nd, 2022
Part of (Re)Learning CS
Talks aboutComputer Science
In computing, a list is a data collection which storesa linear, ordered collection of values of the same data type.
Array
An array stores its values in adjacent memory locations (contiguous) and has a
fixed length like int[]
. For cases where the program doesn't know the data
size in advance, languages also use dynamic arrays, like vector<T>
, which
resize on the fly.
Indexing
Because array elements are stored together, they can use pointer arithmetic for indexing. For any array of type , where is the size of in bytes and is its starting memory address, an item at index is at address .
This means they have random access — all elements can be accessed in constant time ().
Adding & Removing
For fixed-length arrays, pushing and popping new elements is because it's just indexing. Removing an element from the middle requires all subsequent elements to be shifted a position, making it . Removing the first value is however, as the pointer to the first value can be incremented to the next element.
For dynamic arrays, appending a new value beyond the current array length is :
- The array size is doubled
- New memory is allocated
- Its values are copied across
- The new element is stored at the end
Searching
For an unsorted array, finding an element is as it must be iterated to compare against each value.
Sorted arrays can utilise random access and the binary search algorithm to achieve . The algorithm compares the middle item in the array to determine whether the search value is in the lower or upper half; this is performed iteratively/recursively, reducing the array's length for each run and thus the time complexity.
Linked List
A linked list is composed of nodes which store a pointer to the next node in a
A global head pointer is kept to directly access the start of the chain, and a tail pointer is also typically kept to access the end.
Indexing
Accessing nodes in a linked list is , as the nodes must be traversed until the index is reached. To lessen the blow, we can store the length of the list to determine if an index is closer to the head () or tail ().
Adding & Removing
Again, it's .
To insert a new element at index , the current node at index is updated to point to and is updated to point at node . For a double-linked list, the previous pointer is also updated.
Removal is the same idea but the current value is forgotten about.
Searching
Nothing clever can be done in a linked list, so it's once more.
Circular Array
Arrays are good for random access; linked lists are good for start/end insertions. Circular arrays bridge the two.
A large backing array stores the array values, and its start & end memory addresses are managed with a head & tail pointer.
Indexing
We can still use pointer arithmetic to achieve , as long as we respect the head pointer.
For any array of type with length , where is the size of in bytes, and is the head pointer address, an item at index is at address .
Adding & Removing
To add values from the start/end of a circular array, the appropriate pointer is incremented and the value is stored in memory. To remove, the logic is reversed resulting in as long as the backing array isn't full.
Searching
Same as arrays — .