CircularlyLinkedList

Positional linked list with elements linked in one direction from head to tail and tail linked to head.

Generic types (only for TS):

T - Type of elements stored in the list.

new CircularlyLinkedList()

/**
 * Creates an instance of CircularlyLinkedList.
 *
 * @param elements Array of elements to create the new linked list with.
 */
constructor(elements: T[] = [])

Creating a CircularlyLinkedList with elements 1->2->3:

new CircularlyLinkedList([1, 2, 3]);

length

/**
 * Number of elements in the list.
 *
 * @readonly
 */
get length(): number

Adding element increases list length:

addAfter()

Adding element after the specified position:

circle-info

Circular list structure implies that tail pointer stays the same whenever you add new elements after or before it. But as current is always the element after the last, this means that when you add a new element after the last element of the list, getCurrent() call will now return position of the newly added element. So addAfter() at the last element has literally the same effect as addCurrent(). This approach by default allows to fill circular list in a way of a stack. To fill list in the same order that the elements were supplied use the combination of addCurrent() and rotate() methods.

addCurrent()

Adding element as current:

clear()

Deep clearing:

Fast clearing:

getAfter()

Getting position after the specified:

getCurrent()

Getting position of the current element:

getLast()

Getting position of the first element:

circle-info

getLast() and getCurrent() return positions of the same element if list has only one element. These positions won't be the same object however.

isEmpty()

Adding element makes list not empty:

removeCurrent()

Removing element deprecates its position:

replace()

Replacing element takes effect for all positions pointing to it:

rotate()

Rotating list to the element next to the current:

[Symbol.iterator]()

Building an array from list:

See also

Position

Last updated

Was this helpful?