Yallist : Yet another linked list

更新时间:
2024-05-13
下载文档

Yallist : Yet another linked list

This module is a doubly-linked list implementation (Yet Another Linked List), for when an array would be too big, and a Map can't be iterated in reverse order.

User can use the following code to import the Yallist module.

var List = require('yallist');

Support

The following shows Yallist module APIs available for each permissions.

 User ModePrivilege Mode
List
list.head
list.tail
list.length
list.pop
list.push
list.splice
list.reverse
list.shift
list.unshift
list.toArray
list.toArrayReverse
list.forEach
list.forEachReverse
list.get
list.getReverse
list.map
list.mapReverse
list.reduce
list.reduceReverse
list.slice
list.sliceReverse
list.pushNode
list.removeNode
list.unshiftNode

List Class

new List([initData])

  • initData {Array} | {List} Initial data.
  • Returns: {List} list object.

Create a linked list object, equivalent to List.create().

Example

var list = new List([1, 2, 3]);

List Object

list.head

  • {Node}

The first node in the list.

list.tail

  • {Node}

The last node in the list.

list.length

  • {Integer}

The number of nodes in the list. Forbid to change this variable.

list.pop()

  • Returns: {Any} list tail node value.

Get the data from the list tail, and remove the tail from the list.

list.push(...args)

  • ...args {Any} Any number of any type of parameters that need to push to list.

Insert one or more items to the tail of the list.

Example

var list = new List();
list.push(1, 2, 3);
console.log(list.pop()); // 3

list.splice(start, deleteCount, ...items)

  • start {Integer} Where to add / remove items.
  • deleteCount {Integer} How many items should be removed, can be 0.
  • ...items {Any} The new items to be added to the list.
  • Returns: {Array} Array of deleted items.

Like array.splice().

Example

var list = new List([1, 2, 3]);
list.splice(1, 1, 5, 6, 7);
list.forEach(function(item, index, list) {
  console.log('index:', index, 'value:', item);
}); // 1 5 6 7 3

list.reverse()

  • Returns: {List} This list.

Reverse the list in place.

Example

var list = new List([1, 2, 3]);
list.reverse(); // 3 2 1

list.shift()

  • Returns: {Any} list head node value.

Get the data from the list head, and remove the head from the list.

list.unshift(...items)

  • ...items {Any} The new items to be added to the head of list.
  • Returns: {Integer} The number of nodes in the list.

Insert one or more items to the head of the list.

Example

var list = new List([1, 2, 3]);
list.unshift(4, 5, 6); // 6 5 4 1 2 3

list.toArray()

  • Returns: {Array} An array holding all the elements of the linked list.

Create an array representation of the list.

list.toArrayReverse()

  • Returns: {Array} An array holding all the elements of the linked list.

Create a reversed array representation of the list.

list.forEach(fn, [thisp])

  • fn {Function} Visit callback function.
  • thisp {Object} The this object when function is called. default: list object.

Call a function on each item in the list.

Example

var list = new List([1, 2, 3]);
list.forEach(function(item, index, list) {
  console.log('index:', index, 'value:', item);
}); // 1 2 3

list.forEachReverse(fn, [thisp])

  • fn {Function} Visit callback function.
  • thisp {Object} The this object when function is called. default: list object.

Call a function on each item in the list, in reverse order.

list.get(n)

  • n {Integer} Forward position.
  • Returns: {Any} Node value on specified position.

Get the data at position n in the list. Return undefined if the position does not exist. If you use this a lot, probably better off just using an Array.

Example

var list = new List([1, 2, 3]);
console.log(list.get(1)); // 2

list.getReverse(n)

  • n {Integer} Reverse position.
  • Returns: {Any} Node value on specified reverse position.

Get the data at position n, counting from the tail.

list.map(fn, [thisp])

  • fn {Function} Visit callback function.
  • thisp {Object} The this object when function is called. default: list object.

Create a new list with the result of calling the function on each item.

Example

var list1 = new List([1, 2, 3]);
var list2 = list1.map(function(item, list) {
  return item + 1;
});
list2.forEach(function(item, index, list) {
  console.log('index:', index, 'value:', item);
}); // 2 3 4

list.mapReverse()

  • fn {Function} Visit callback function.
  • thisp {Object} The this object when function is called. default: list object.

Same as list.map(), but in reverse.

list.reduce(fn[, initial])

  • fn {Function} Visit callback function.
  • initial {Any} The initial value passed to the function.
  • Returns: {Any} Function calculation results.

Like array.reduce().

Example

var list = new List([1, 2, 3]);
var sum  = list.reduce(function(total, item, index) {
  return total + item;
});
console.log(sum); // 6

list.reduceReverse(fn[, initial])

  • fn {Function} Visit callback function.
  • initial {Any} The initial value passed to the function.
  • Returns: {Any} Function calculation results.

Like array.reduce(), but in reverse.

list.slice([from], [to])

  • from {Integer} Where to start selection.
  • to {Integer} Where to end the selection (not include).
  • Returns: {List} New list.

Just like array.slice(), but returns a new list.

Example

var list  = new List([1, 2, 3]);
var nlist = list.slice(1, 2); // 2

list.sliceReverse([from], [to])

  • from {Integer} Where to start selection.
  • to {Integer} Where to end the selection (not include).
  • Returns: {List} New list.

Just like list.slice(), but the result is returned in reverse.

Node Object

node.next

  • {Node}

The next node in the list.

node.prev

  • {Node}

The previous node in the list.

node.value

  • {Any}

The data the node contains.

node.list

  • {List}

The list to which this node belongs. (null if it does not belong to any list.)

list.pushNode(node)

  • node {Node} The node.

Move node object to the end of the list. (That is, pull it out of wherever it lives, and make it the new tail.)

If the node belongs to a list already, then that list will remove it first.

list.removeNode(node)

  • node {Node} The node.

Remove node from the list, preserving referential integrity of head and tail and other nodes.

Will throw an error if you try to have a list remove a node that doesn't belong to it.

list.unshiftNode(node)

  • node {Node} The node.

Move node object to the front of the list. (That is, pull it out of wherever it lives, and make it the new head.)

If the node belongs to a different list, then that list will remove it first.

文档内容是否对您有所帮助?
有帮助
没帮助