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 = ; const list = ; list; console;// => Node { value: 'A', next: Node { value: 'B', next: [CircularLinkedList] } } console;// => Node { value: 'B', next: Node { value: 'A', next: [CircularLinkedList] } } console;// => Node { value: 'A', next: Node { value: 'B', next: [CircularLinkedList] } } console;// => [ '[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;// => { 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;// => { value: 'C', next: [List] } list;// => { 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;console;// => A
last
list.- Return Type:
any
Returns the value of the last node on the list.
list;console;// => B
length
list.- Return Type:
Integer
Returns the length of the list.
list;console;// => 2
isEmpty()
list.- Return Type:
Boolean
Checks whether or not the list is empty.
list;console;// => 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;list;// => { 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;const node = list;console;// => { value: 'A', next: { value: 'B', next: [List] } } console;// => A console;// => { 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;console;// => A console;// => 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;// => { value: 'A', next: [List] } list;// => { value: 'B', next: [List] } list;// => { value: 'C', next: [List] }
toArray()
list.- Return Type:
Array
Converts the list into an array.
list;// => { value: 'A', next: { value: 'B', next: [List] } } const array = list;console;// => [ 'A', 'B', 'C' ]
clear()
list.- Return Type:
Empty Linked List
Removes all nodes from the list.
list;// => { value: 'A', next: { value: 'B', next: [List] } } listclear;// => 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;// => { value: 'A', next: { value: 'B', next: [List] } } console;// => 'A,B,C' console;// => 'ABC' console;// => '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;// => { value: 'A', next: { value: 'B', next: [List] } } list;console;// => [ '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;// => { value: 'A', next: { value: 'B', next: [List] } } const mapped = list;console;// => '[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.