Abstract Data Types: Lists

What is a list?

A list is a sequential abstract data type where following rules have to be obeyed

How values are added to list tail:

 

What are the operations a list requires?

Although, as an abstract data type, list comes with no implementation, it requires from data structures that implement it to provide support for a minimal set of operations:

Operation Arguments Returns Description
addToHead VALUE void Add value on top of list.
This means inserting another entry at position 0, shifting all existing entries one step (position) right
addToTail VALUE void Add value on bottom of list.
This means inserting another entry after last existing entry in list
clear   void Clears list of all values.
containsIndex POSITION BOOLEAN Checks if position exists in list.
containsValue VALUE, COMPARATOR BOOLEAN Checks if value exists in list by comparator.
This potentially requires iterating the whole list and applying comparator to see if value matches. AVOID unless list is small!
emplace POSITION, VALUE void Inserts value at position
This means inserting an entry at given position, shifting remaining entries one step (position) right
get POSITION VALUE Gets value by position.
set POSITION, VALUE void Sets value by position.
removeIndex POSITION void Removes value by position.
This means removing entry at given position, shifting remaining entries one step (position) left
removeValue VALUE, COMPARATOR void Removes all values that match value by comparator.
This potentially requires iterating the whole list, applying comparator to see if value matches and, every time it matches, shifting remaining entries one step (position) left. AVOID unless list is very small!
isEmpty   BOOLEAN Checks if list is empty
size   ULONG Gets number of elements in list

Where:

What are the data structures that implement list?

Most common implementations of lists are:

This time complexity table using Big O Notation shows advantages and disadvantages of each data structure chosen for each list operation:

Method Dynamic Array Linked List Doubly Linked List
addToHead O(N*2) O(1) O(1)
addToTail O(1) O(1) O(1)
clear O(2) O(N) O(N)
containsIndex O(1) O(1) O(1)
containsValue O(N) O(N) O(N)
emplace O(N*2) O(N) O(N)
get O(1) O(N) O(N)
set O(1) O(N) O(N)
removeIndex O(N*2) O(N) O(N)
removeValue O(N*2) O(N) O(N)
isEmpty O(1) O(1) O(1)
size O(1) O(1) O(1)

Conclusion: whenever random position deletes/inserts are not needed, for memory & speed efficiency reasons, Dynamic Array should be solution of choice!