Skip to content

Lists

List and its linked variants offer array-like APIs plus iterable helpers.

All list types share the same IList/ILinkedList contracts and support iterators (Iterable) as well as reverseIterator, sorting (ISortable) and functional helpers (IListFunctions).

List

  • Backed by an array.
  • Supports functional helpers (map, filter, reduce, every, some).
  • Provides slicing/splicing helpers:
    • slice(start, end) wraps around when indices exceed bounds.
    • slice2(start, end) uses the sign of end to determine direction (left/right).
    • splice(start, deleteCount) removes elements relative to start.
  • Implements Iterable and reverseIterator() for forward/backward traversal.
ts
const list = new List([1, 2, 3, 4])
list.slice(-1, 2)   // wrap-around slice
const mapped = list.map(n => n * 2)

Linked lists

LinkedList, DoublyLinkedList, and CyclicLinkedList share the ILinkedList API:

  • addFirst, addLast, removeFirst, removeLast
  • getNode(index) for direct node access
  • first/last node references for constant-time head/tail operations

Use linked variants when you need predictable insert/remove costs regardless of position.

Comparator + sorting

Lists implement ISortable and honor list.comparator. Pair them with createComparator or the built-in number/string comparators to sort in place.

ts
const list = new List([{ score: 10 }, { score: 5 }])
list.comparator = createComparator('score', 'desc')
list.sort()

Initialization via Iterable

Every list constructor accepts an Iterable, so you can pass generators, sets, or other collections to bootstrap contents quickly.

Complexity

OperationList (array)Linked variants
add, addLastAmortized O(1)O(1)
addFirstO(n)O(1)
remove by indexO(n)O(n)
slice / spliceO(k)O(k)
map / filterO(n)O(n)
indexOf / containsO(n)O(n)

k denotes the number of elements copied/removed.

Use List (array-backed) for random access and functional helpers, or select LinkedList, DoublyLinkedList, or CyclicDoublyLinkedList when you need O(1) insertion/removal at the ends.

Notes

  • Linked variants expose addFirst/removeFirst/getNode via ILinkedList.
  • Use Queues or Stacks when you only need strict FIFO/LIFO semantics.
  • Pair lists with the Comparator helpers to use value-based equality.

Built with VitePress – Released under the MIT License.