Algorithms and data structures for JS/TS
  • Introduction
  • GETTING STARTED
    • TS
    • Node.js
    • ES Modules
    • Browsers
  • PUBLIC API
    • How to read
    • Linked lists
      • SinglyLinkedList
      • DoublyLinkedList
      • CircularlyLinkedList
    • Stacks and queues
      • LinkedStack
      • LinkedQueue
      • LinkedDeque
      • CircularQueue
      • CircularArrayBuffer
      • UnsortedPriorityQueue
      • SortedPriorityQueue
      • AdaptableHeapPriorityQueue
    • Maps
      • SortedMap
      • MaximaSet
      • AVLTreeMap
      • SplayTreeMap
      • RedBlackTreeMap
    • Trees
      • GeneralTree
      • LinkedBinaryTree
      • PreorderTreeTraversal
      • InorderTreeTraversal
      • PostorderTreeTraversal
      • EulerTourTreeTraversal
    • Searches
      • Binary search
      • Quick select
    • Text processing
      • Longest common subsequence
      • Boyer-Moore
      • Knuth-Morris-Pratt
    • Position
    • Locator
    • Comparators
  • CONTRIBUTION NOTES
    • How to contribute
    • Project structure
  • Changelog
Powered by GitBook
On this page

Was this helpful?

  1. PUBLIC API

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' 

Do not use _internal property from your code. It stays public because is used by external code in the package realization. But TS does not yet have package scope limitation level so this seems to be the only option.

PreviousKnuth-Morris-PrattNextLocator

Last updated 5 years ago

Was this helpful?