Position

Represents the location of a single element in linked data structure.

Position represents a location of a single element in linked data structure. It allows access to list element in O(1) time instead of O(n) when searching through entire list by value.

Client code is not allowed to create an instance of Position class directly but can often obtain it from methods called on linked-based structures. This class brings difference between working with inner realization of a data structure and getting access only to data viable for developer. So one purpose of it is to be an adapter for safety.

Another objective of position is to point directly on certain node of the structure to grant access to it in constant time.

element

element: T

Element at this position. This field is read only.

Generic types (only for TS):

T - Type of elements stored in the data structure this position points on.

Examples:

import { SinglyLinkedList } from 'ads-js/lists';

const list = new SinglyLinkedList([1, 2, 3]);
const position = list.getFirst();

position.element === 1; // true
list.getAfter(position); // Position of 2
list.clear();
position.element === 1; // true, because position keeps reference to the element
// though it was deprecated and can not be used with the list anymore
list.getAfter(position); // throws 'ADSError: Position is deprecated' 

Last updated

Was this helpful?