Circular-linked-list
Circular singly linked list implementation, heavily based on Singlie by Klaus Sinani.
Installation
$ npm install --save circular-linked-list
Usage
$ npm run lint
$ npm run test
$ npm run test:coverage
$ npm run dist
$ npm run deploy
const CircularLinkedList = require("circular-linked-list");
const list = new CircularLinkedList();
list.append("B").prepend("A");
console.log(list.node(0));
console.log(list.node(0).next);
console.log(list.node(0).next.next);
console.log(
list
.map(x => `[${x}]`)
.reverse()
.toArray()
);
API
list.append(value[, value])
Appends one of more nodes to the list.
value
Can be one or more comma delimited values. Each value corresponds to a single node.
list.append("A", "B", "C", "D");
list.prepend(value[, value])
Prepends one of more nodes to the list.
value
Can be one or more comma delimited values. Each value corresponds to a single node.
list.append("C", "D");
list.prepend("B", "A");
list.head
Returns the value of the first node / head on the list.
list.append("A", "B");
console.log(list.head);
list.last
Returns the value of the last node on the list.
list.append("A", "B");
console.log(list.last);
list.length
Returns the length of the list.
list.append("A", "B");
console.log(list.length);
list.isEmpty()
Checks whether or not the list is empty.
list.append("A", "B");
console.log(list.isEmpty());
list.insert({value[, value], index})
Inserts one or more nodes to the given index.
value
Can be one or more comma delimited values. Each value corresponds to a single node.
index
Can be an integer corresponding to a list index.
list.append("A", "B", "E");
list.insert({ value: ["C", "D"], index: 1 });
list.node(index)
Return the node corresponding to the given index.
index
Can be an integer corresponding to a list index.
list.append("A", "B", "C", "D");
const node = list.node(0);
console.log(node);
console.log(node.value);
console.log(node.next);
list.get(index)
Return the value of node corresponding to the given index.
index
Can be an integer corresponding to a list index.
list.append("A", "B");
console.log(list.get(0));
console.log(list.get(0));
list.remove(index)
Removes from the list the node located to the given index.
index
- Type:
Integer
- Default:
list.length - 1
Can be an integer corresponding to a list index.
If not provided, the last node of the list will be removed.
list.append("A", "B", "C", "D");
list.remove(0);
list.remove(0);
list.toArray()
Converts the list into an array.
list.append("A", "B", "C");
const array = list.toArray();
console.log(array);
list.clear()
- Return Type:
Empty Linked List
Removes all nodes from the list.
list.append("A", "B", "C");
list.clear();
list.join([separator])
Joins the values of all nodes on the list into a string and returns the string.
separator
- Type:
String
- Default:
Comma ','
Specifies a string to separate each pair of adjacent node values of the array.
If omitted, the node values are separated with a comma ','
.
list.append("A", "B", "C");
console.log(list.join());
console.log(list.join(""));
console.log(list.join(" "));
list.forEach(function)
Executes a provided function once for each node value.
function
Function to execute for each node value.
const array = [];
list.append("A", "B", "C");
list.forEach(x => array.push(x));
console.log(array);
list.map(function)
Executes a provided function once for each node value.
function
Function that produces a new node value for the new list.
list.append("A", "B", "C");
const mapped = list.map(x => `[${x}]`);
console.log(array.join(" "));
Licence
My work is released under the MIT licence. One can consider this project to be a fork of Singlie by Klaus Sinani.