Constructor
new DoublyLinkedList()
In computer science, a doubly linked list is a linked data
structure that consists of a set of sequentially linked records called
nodes. Each node contains two fields, called links, that are references
to the previous and to the next node in the sequence of nodes. The beginning
and ending nodes' previous and next links, respectively, point to some kind
of terminator, typically a sentinel node or null, to facilitate traversal
of the list. Full wikipedia article at:
https://en.wikipedia.org/wiki/Doubly_linked_list
- Source:
Example
const DoublyLinkedList = require('dstructures').DoublyLinkedList;
const myDoublyLinkedList = new DoublyLinkedList();
Methods
append(element) → {Boolean|Void}
Appends element to a doubly linked list.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Given element. |
Returns:
- Type:
-
Boolean
|Void
Returns false if 'element' argument is not present.
Example
DoublyLinkedList.append(1); // [1]
DoublyLinkedList.append(2); // [1] -> [2]
DoublyLinkedList.append(3); // [1] -> [2] -> [3]
GetElement(element)
Returns the element property of the given node.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Element property of the node. |
Returns:
Returns the element property of the node.
insert(newElement, oldElementopt) → {Boolean|Void}
Inserts a node in a doubly linked list.
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
newElement |
any
|
Given element. | ||
oldElement |
any
|
<optional> |
head | 'oldElement' argument defaults to 'head' if is not present, and inserts at the head of doubly linked list. |
Returns:
- Type:
-
Boolean
|Void
Returns false if the 'newElement' argument is not present.
Example
DoublyLinkedList.insert(1); // [1]
DoublyLinkedList.insert(2, 1); // [1] -> [2]
DoublyLinkedList.insert(3, 1); // [1] -> [3] -> [2]
DoublyLinkedList.insert(4); // [4] -> [1] -> [3] -> [2]
remove(element) → {Boolean}
Removes element from a doubly linked list.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Element that will be removed. |
Returns:
- Type:
-
Boolean
Returns false if the element is not present.
Example
DoublyLinkedList; // [1] -> [2] -> [3]
DoublyLinkedList.remove(2); // [1] -> [3]
toArray() → {Array}
Returns array representation of a doubly linked list.
- Source:
Returns:
- Type:
-
Array
Returns array representation of the Doubly linked list.
Example
DoublyLinkedList; // ['cat'] -> ['pig'] -> ['dog']
DoublyLinkedList.toArray(); // ['cat', 'pig', 'dog']