项目作者: Floofies

项目描述 :
Singly linked, doubly linked, and circular LinkedList classes from Differentia.js
高级语言: JavaScript
项目地址: git://github.com/Floofies/LinkedList.git
创建时间: 2019-01-06T22:06:21Z
项目社区:https://github.com/Floofies/LinkedList

开源协议:MIT License

下载


LinkedList

Singly linked, doubly linked, and circular LinkedList classes.

NPM

You can import the list constructors like so:

  1. const listlib = require("listlib");
  2. const LinkedList = listlib.LinkedList;
  3. const DoubleLinkedList = listlib.DoubleLinkedList;
  4. const CircularLinkedList = listlib.CircularLinkedList;
  5. const CircularDoubleLinkedList = listlib.CircularDoubleLinkedList;
  6. // Using ES6 Destructuring Assignment
  7. const {
  8. LinkedList,
  9. DoubleLinkedList,
  10. CircularLinkedList,
  11. CircularDoubleLinkedList
  12. } = require("listlib");

LinkedList

Constructor

  1. new LinkedList( [ iterable = null ] );

Uses ListElement to represent the list structure.

Acyclic Singly Linked List.

Constructor Parameters

  • iterable (Optional) Iterable

    The values of the optional Iterable will be used to populate the new LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Append "6" to the end of the list, returns a new ListElement.
  6. var six = list.push(6);
  7. // Prepends "7" to the beginning of the list, returns a new ListElement.
  8. var seven = list.unshift(7);
  9. // Moves "7" to the end of the list.
  10. list.pushBack(seven);
  11. // Logs 1, 2, 3, 4, 5, 6, and 7.
  12. for (const element of list) {
  13. console.log(element.payload);
  14. }

Instance Properties

LinkedList.head

ListElement

  1. LinkedList.head

A ListElement which is considered the head/start of the list, and is used to begin enumeration of subsequent ListElements.

LinkedList.tail

ListElement

  1. LinkedList.tail

A ListElement which is considered the tail/end of the list, and is used to end enumeration of preceding ListElements.

LinkedList.size

Number

  1. LinkedList.size

A number representing the quantity of ListElements within the LinkedList, not counting the head and tail elements.

LinkedList.double

Boolean

  1. LinkedList.double

Indicates if the LinkedList is doubly linked, linking every ListElement in both directions.

LinkedList.circular

Boolean

  1. LinkedList.circular

Indicates if the LinkedList is circular, linking the head and tail elements.


Constructor Properties

LinkedList.ListElement

See the ListElement documentation.


Prototype Methods

LinkedList.prototype.@@iterator

Iterator

  1. LinkedList[Symbol.iterator]( [ ends = false ] );

An iterator which yields each ListElement in the LinkedList, except for the head and tail elements.

Parameters

  • ends Boolean

    If set to true, the iterator will include the head/tail ListElements in iteration. In a circular LinkedList, this will cause the iterator to infinitely loop through the list unless some user-supplied break condition is in place.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Logs 1, 2, 3, 4, and 5.
  6. for (const value of list) console.log(element.payload);

Example 1: ends Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Logs `null`, 1, 2, 3, 4, 5, and `null`.
  6. for (const value of list[Symbol.iterator](true)) console.log(element.payload);

LinkedList.prototype.values

Iterator

  1. LinkedList.values();

An iterator which yields the value of each ListElement in the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Logs 1, 2, 3, 4, and 5.
  6. for (const value of list.values()) console.log(value);

LinkedList.prototype.elements

Iterator

  1. LinkedList.elements();

An iterator which yields each ListElement in the LinkedList, except for the head and tail elements.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Logs 1, 2, 3, 4, and 5.
  6. for (const value of list.values()) console.log(value);

forEach

Higher-Order Function

  1. LinkedList.forEach( callback );

Calls callback with the value of each ListElement.

Parameters

  • callback Function

    The callback function to execute for each ListElement value.

Callback Parameters

  • value

    The value of the current ListElement being enumerated over.

  • index

    The index (Starting at zero) of the current ListElement being enumerated over.

  • list

    The target LinkedList being iterated through.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Logs 1, 2, 3, 4, and 5.
  6. list.forEach(function (value, index, list) {
  7. console.log(value);
  8. });

LinkedList.prototype.fromIterable

Function

  1. LinkedList.fromIterable( iterable );

Inserts the values of the Iterable into the LinkedList.

Parameters

  • iterable Iterable

    The iterable to copy values from.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New empty LinkedList is created.
  4. var list = new LinkedList();
  5. // Contents of `arr` are inserted into the list.
  6. list.fromIterable(arr);

LinkedList.prototype.coerceElement

Function

  1. LinkedLst.coerceElement( value );

Creates a new ListElement using value. If value is already a ListElement, it is returned.

Parameters

  • value Any

    A ListElement, or a value to create a new ListElement with.

Examples

Example 1: Basic Usage:

  1. // Arbitrary number.
  2. var numbers = 123;
  3. // Returns a new ListElement with `numbers` assigned to `element.payload`.
  4. var element = LinkedList.coerceElement(numbers);

LinkedList.prototype.item

Function

  1. LinkedList.item( index );

Returns the ListElement at the specified 0-indexed offset from the head element, or null if it was not found.

Parameters

  • index Number

    An offset from the head element, starting at zero, to look for a ListElement at.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns the ListElement at index 4, the last element in the list, which contains "5".
  6. var foundElement = list.item(4);

LinkedList.prototype.find

Function

  1. LinkedList.find( value );

Returns the first ListElement encountered that contains a payload matching value, or null if one was not found.

Parameters

  • value Any

    A value to search for in the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns the ListElement containing "3".
  6. var foundElement = list.find(3);

LinkedList.prototype.includes

Function

  1. LinkedList.includes( value );

Returns true if a ListElement is found which contains a payload matching value, or false if one was not found.

Parameters

  • value Any

    A value to search for in the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns `true`.
  6. var wasFound = list.includes(3);

LinkedList.prototype.getPrev

Function

  1. LinkedList.getPrev( element );

Returns the ListElement located before element, or null if it was not found.

Parameters

  • element ListElement

    A ListElement to search for the preceding ListElement of in the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns the element containing "2".
  6. var two = list.find(2);
  7. // Returns the ListElement containing "1".
  8. var one = list.getPrev(two);

LinkedList.prototype.first

Function

  1. LinkedList.first();

Returns the element at the beginning of the LinkedList, or null if the list is empty.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns the element containing "1".
  6. var one = list.first();

LinkedList.prototype.last

Function

  1. LinkedList.last();

Returns the element at the end of the LinkedList, or null if the list is empty.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns the element containing "5".
  6. var five = list.last();

LinkedList.prototype.clear

Function

  1. LinkedList.clear();

Removes all elements from the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // List becomes empty.
  6. list.clear();

LinkedList.prototype.concat

Function

  1. LinkedList.concat( list1 [, list2, ..., listN ] );

Concatenates multiple LinkedLists into the callee LinkedList.

Parameters

  • list1listN ListElement

    An argument list of LinkedLists to concatenate.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedLists are created using the contents of `arr`.
  4. var list1 = new LinkedList(arr);
  5. var list2 = new LinkedList(arr);
  6. var list3 = new LinkedList(arr);
  7. // Appends `list2` and `list3` to the end of `list1`.
  8. list1.concat(list2, list3);
  9. // Logs 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.
  10. for (const element of list) {
  11. console.log(element.payload);
  12. }

LinkedList.prototype.remove

Function

  1. LinkedList.remove( element );

Removes and returns an element from the LinkedList.

Parameters

  • element ListElement

    A ListElement object to remove from the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Get the element which contains "3".
  6. var three = list.find(3);
  7. // Removes the element from the list.
  8. list.remove(three);

LinkedList.prototype.insertBefore

Function

  1. LinkedList.insertBefore( element, newElement );

Inserts a ListElement before element

Parameters

  • element ListElement

    A ListElement object to prepend with newElement.

  • newElement Any

    A ListElement or arbitrary value to add to the LinkedList before element.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Get the element which contains "3".
  6. var three = list.find(3);
  7. // Inserts "2.5" before "3".
  8. var twoPointFive = list.insertBefore(three, 2.5);

LinkedList.prototype.insertAfter

Function

  1. LinkedList.insertAfter( element, newElement );

Inserts a ListElement after element

Parameters

  • element ListElement

    A ListElement object to prepend with newElement.

  • newElement Any

    A ListElement or arbitrary value to add to the LinkedList after element.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Get the element which contains "3".
  6. var three = list.find(3);
  7. // Inserts "3.5" after "3".
  8. var threePointFive = list.insertAfter(three, 3.5);

LinkedList.prototype.prepend

Function

  1. LinkedList.prepend( element );

Alias: unshift

Inserts a ListElement at the beginning of the LinkedList.

Parameters

  • element Any

    A ListElement or arbitrary value to prepend the LinkedList with.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Inserts "0" at the beginning of the list.
  6. var zero = list.prepend(0);

LinkedList.prototype.unshift

Function

  1. LinkedList.unshift( element );

An alias of LinkedList.prepend.


LinkedList.prototype.append

Function

  1. LinkedList.append( element );

Alias: push

Inserts a ListElement at the end of the LinkedList.

Parameters

  • element Any

    A ListElement or arbitrary value to append the LinkedList with.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Inserts "6" at the end of the list.
  6. var six = list.append(6);

LinkedList.prototype.push

Function

  1. LinkedList.push( element );

An alias of LinkedList.prototype.append.


LinkedList.prototype.shift

Function

  1. LinkedList.shift();

Removes an element from the beginning of the LinkedList and returns it.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Removes and returns the first element in the list.
  6. var one = list.shift();

LinkedList.prototype.pushBack

Function

  1. LinkedList.pushBack( element );

Moves a ListElement to the end of the LinkedList.

Parameters

  • element Any

    A ListElement object to move to the end of the LinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Returns the element containing "2".
  6. var two = list.find(2);
  7. // Moves "2" to the end of the list.
  8. list.pushBack(two);

LinkedList.prototype.copyWithin

Function

  1. LinkedList.copyWithin( target [, start = 0 [, end = LinkedList.size ]] );

Shallow copies ListElements to another location in the same LinkedList and returns it, without modifying its size.

Parameters

  • target Number

    Zero based index at which to copy the sequence to. If negative, target will be counted from the end. If target is at or greater than LinkedList.size, nothing will be copied. If target is positioned after start, the copied sequence will be trimmed to fit LinkedList.size.

  • start (Optional) Number

    Zero based index at which to start copying elements from. If negative, start will be counted from the end. If start is omitted, copyWithin will copy from the start of the LinkedList (defaults to 0).

  • end (Optional) Number

    Zero based index at which to end copying elements from. Copies up to but not including end. If negative, end will be counted from the end.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New LinkedList is created using the contents of `arr`.
  4. var list = new LinkedList(arr);
  5. // Copy to index 0 the element at index 3.
  6. list.copyWithin(0, 3, 4);
  7. // Logs 4, 2, 3, 4, 5
  8. for (const element of list) {
  9. console.log(element.payload);
  10. }
  11. // Copy to index 1 all elements from index 3 to the end.
  12. list.copyWithin(1, 3);
  13. // Logs 4, 4, 5, 4, 5
  14. for (const element of list) {
  15. console.log(element.payload);
  16. }

DoubleLinkedList

Constructor

  1. new DoubleLinkedList( [ iterable = null ] );

Inherits from LinkedList

Uses ListElement to represent the list structure.

Refer to the LinkedList documentation for member methods & properties.

Circular Doubly Linked List. Elements have references to previous elements, making some operations faster.

Constructor Parameters

  • iterable (Optional) Iterable

    The values of the optional Iterable will be used to populate the new CircularDoubleLinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New DoubleLinkedList is created using the contents of `arr`.
  4. var list = new DoubleLinkedList(arr);
  5. // Append "6" to the end of the list, returns a new ListElement.
  6. var six = list.push(6);
  7. // Prepends "7" to the beginning of the list, returns a new ListElement.
  8. var seven = list.unshift(7);
  9. // Moves "7" to the end of the list.
  10. list.pushBack(seven);
  11. // Logs 1, 2, 3, 4, 5, 6, and 7.
  12. for (const element of list) {
  13. console.log(element.payload);
  14. }

CircularLinkedList

Constructor

  1. new CircularLinkedList( [ iterable = null ] );

Inherits from LinkedList

Uses ListElement to represent the list structure.

Refer to the LinkedList documentation for member methods & properties.

Circular Singly Linked List. The tail and head elements are connected to create a cycle. Iterators will infinitely loop through this list unless they are either interrupted or the list structure becomes broken.

Constructor Parameters

  • iterable (Optional) Iterable

    The values of the optional Iterable will be used to populate the new CircularDoubleLinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New CircularLinkedList is created using the contents of `arr`.
  4. var list = new CircularLinkedList(arr);
  5. // Append "6" to the end of the list, returns a new ListElement.
  6. var six = list.push(6);
  7. // Prepends "7" to the beginning of the list, returns a new ListElement.
  8. var seven = list.unshift(7);
  9. // Moves "7" to the end of the list.
  10. list.pushBack(seven);
  11. // Repeatedly logs 1, 2, 3, 4, 5, 6, and 7, in an infinite loop.
  12. for (const element of list) {
  13. console.log(element.payload);
  14. }

CircularDoubleLinkedList

Constructor

  1. new CircularDoubleLinkedList( [ iterable = null ] );

Inherits from LinkedList

Uses ListElement to represent the list structure.

Refer to the LinkedList documentation for member methods & properties.

Circular Doubly Linked List. Elements have references to previous elements, making some operations faster. The tail and head elements are connected to create a cycle. Iterators will infinitely loop through this list unless they are either interrupted or the list structure becomes broken.

Constructor Parameters

  • iterable (Optional) Iterable

    The values of the optional Iterable will be used to populate the new CircularDoubleLinkedList.

Examples

Example 1: Basic Usage:

  1. // Array of arbitrary numbers.
  2. var arr = [1,2,3,4,5];
  3. // New CircularDoubleLinkedList is created using the contents of `arr`.
  4. var list = new CircularDoubleLinkedList(arr);
  5. // Append "6" to the end of the list, returns a new ListElement.
  6. var six = list.push(6);
  7. // Prepends "7" to the beginning of the list, returns a new ListElement.
  8. var seven = list.unshift(7);
  9. // Moves "7" to the end of the list.
  10. list.pushBack(seven);
  11. // Logs 1, 2, 3, 4, 5, 6, and 7.
  12. for (const element of list) {
  13. console.log(element.payload);
  14. }

ListElement

Constructor

  1. new ListElement( [ payload = null ], [ next = null ], [ prev = null ] );

The class used internally by LinkedList to represent an element of the Linked List.

Parameters

  • payload (Optional) Any

    Arbitrary data which is assigned to ListElement.payload.

  • next (Optional) ListElement

    A ListElement to refer to as being next the next element.

  • prev (Optional) ListElement

    A ListElement to refer to as being the previous element.

Examples

Example 1: Basic Usage

  1. // A Number
  2. var num = 123;
  3. // Creates a new empty LinkedList.
  4. var list = new LinkedList();
  5. // Creates a new ListElement with `num` assigned to `element.payload`
  6. var element = new list.ListElement(num);
  7. // Appends `element` to the end of the list.
  8. list.push(element);
  9. // Removes `element` from the list.
  10. list.remove(element);

Prototype Methods

ListElement.prototype.fromElement

Function

  1. ListElement.fromElement( element );

Copies the payload of a ListElement into the callee ListElement.

Parameters

  • element ListElement

    A ListElement to copy the payload from.

Examples

Example 1: Basic Usage:

  1. // A source ListElement
  2. var element1 = new LinkedList.ListElement(123);
  3. // A destination ListElement
  4. var element2 = new LinkedList.ListElement();
  5. // Copies the payload of `element1` into `element2`.
  6. element2.fromElement(element1);