README
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));
// => Node { value: 'A', next: Node { value: 'B', next: [CircularLinkedList] } }
console.log(list.node(0).next);
// => Node { value: 'B', next: Node { value: 'A', next: [CircularLinkedList] } }
console.log(list.node(0).next.next);
// => Node { value: 'A', next: Node { value: 'B', next: [CircularLinkedList] } }
console.log(
list
.map(x => `[${x}]`)
.reverse()
.toArray()
);
// => [ '[B]', '[A]' ]
API
append(value[, value])
list.- Return Type:
Linked List
Appends one of more nodes to the list.
value
- Type:
any
Can be one or more comma delimited values. Each value corresponds to a single node.
list.append("A", "B", "C", "D");
// => { value: 'A', next: { value: 'B', next: [List] } }
prepend(value[, value])
list.- Return Type:
Linked List
Prepends one of more nodes to the list.
value
- Type:
any
Can be one or more comma delimited values. Each value corresponds to a single node.
list.append("C", "D");
// => { value: 'C', next: [List] }
list.prepend("B", "A");
// => { value: 'A', next: { value: 'B', next: { value: 'C', next: [List] } } }
head
list.- Return Type:
any
Returns the value of the first node / head on the list.
list.append("A", "B");
console.log(list.head);
// => A
last
list.- Return Type:
any
Returns the value of the last node on the list.
list.append("A", "B");
console.log(list.last);
// => B
length
list.- Return Type:
Integer
Returns the length of the list.
list.append("A", "B");
console.log(list.length);
// => 2
isEmpty()
list.- Return Type:
Boolean
Checks whether or not the list is empty.
list.append("A", "B");
console.log(list.isEmpty());
// => false
insert({value[, value], index})
list.- Return Type:
Linked List
Inserts one or more nodes to the given index.
value
- Type:
any
Can be one or more comma delimited values. Each value corresponds to a single node.
index
- Type:
Integer
Can be an integer corresponding to a list index.
list.append("A", "B", "E");
list.insert({ value: ["C", "D"], index: 1 });
// => { value: 'A', next: { value: 'D', next: { value: 'C', next: { value: 'B', next: [List] } } } }
node(index)
list.- Return Type:
Node
Return the node corresponding to the given index.
index
- Type:
Integer
Can be an integer corresponding to a list index.
list.append("A", "B", "C", "D");
const node = list.node(0);
console.log(node);
// => { value: 'A', next: { value: 'B', next: [List] } }
console.log(node.value);
// => A
console.log(node.next);
// => { value: 'B', next: [List] }
get(index)
list.- Return Type:
any
Return the value of node corresponding to the given index.
index
- Type:
Integer
Can be an integer corresponding to a list index.
list.append("A", "B");
console.log(list.get(0));
// => A
console.log(list.get(0));
// => B
remove(index)
list.- Return Type:
Linked List
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");
// => { value: 'A', next: [List] }
list.remove(0);
// => { value: 'B', next: [List] }
list.remove(0);
// => { value: 'C', next: [List] }
toArray()
list.- Return Type:
Array
Converts the list into an array.
list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }
const array = list.toArray();
console.log(array);
// => [ 'A', 'B', 'C' ]
clear()
list.- Return Type:
Empty Linked List
Removes all nodes from the list.
list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }
list.clear();
// => null
join([separator])
list.- Return Type:
String
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");
// => { value: 'A', next: { value: 'B', next: [List] } }
console.log(list.join());
// => 'A,B,C'
console.log(list.join(""));
// => 'ABC'
console.log(list.join(" "));
// => 'A B C'
forEach(function)
list.- Return Type:
undefined
Executes a provided function once for each node value.
function
- Type:
Function
Function to execute for each node value.
const array = [];
list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }
list.forEach(x => array.push(x));
console.log(array);
// => [ 'A', 'B', 'C' ];
map(function)
list.- Return Type:
Linked List
Executes a provided function once for each node value.
function
- Type:
Function
Function that produces a new node value for the new list.
list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }
const mapped = list.map(x => `[${x}]`);
console.log(array.join(" "));
// => '[A] [B] [C]'
Licence
My work is released under the MIT licence. One can consider this project to be a fork of Singlie by Klaus Sinani.