Constructor
new DoublyLinkedList(value)
Name | Type | Description |
---|---|---|
value | Array | | The value to initialize the list with (optional). |
Name | Type | Description |
---|---|---|
head | DLLNode | The first node in the list |
tail | DLLNode | The last node in the list |
size | Number | The number of nodes in the list |
new DoublyLinkedList();
new DoublyLinkedList("Beep");
new DoublyLinkedList([10,20,30]);
Methods
clear() → {DoublyLinkedList}
Clears the linked list.
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([10,20,30]);
dll.clear();
dll.toArray(); // []
get(index, returnNode) → {Number|DLLNode}
Retrieves the value at the specified index in the linked list.
Name | Type | Default | Description |
---|---|---|---|
index | Number | The index of the value to retrieve. | |
returnNode | Boolean | false | Whether to return the Node or the value. |
The value at the specified index, or the Node if returnNode is true.
- Type:
- Number |
DLLNode
const dll = new DoublyLinkedList([10,20,30]);
dll.get(1); // 20
insert(index, value) → {DoublyLinkedList}
Inserts a new value at the specified index in the linked list.
Name | Type | Description |
---|---|---|
index | Number | The index to insert the value at. |
value | Number | The value to insert. |
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([10,20,30]);
dll.insert(1, 0); // 0,10,20,30
pop() → {Number}
Removes the value from the end of the linked list.
The value that was removed.
- Type:
- Number
const dll = new DoublyLinkedList([10,20,30]);
dll.pop(); // 10,20
print() → {DoublyLinkedList}
Prints the values of the linked list.
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([10,20,30]);
dll.print(); // 10,20,30
push(value) → {DoublyLinkedList}
Adds a new value to the end of the linked list.
Name | Type | Description |
---|---|---|
value | Number | The value to add. |
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([10,20,30]);
dll.push(40); // 10,20,30,40
remove(index) → {Number}
Removes the value at the given index of the linked list.
Name | Type | Description |
---|---|---|
index | Number | The index of the value to remove. |
The value that was removed.
- Type:
- Number
const dll = new DoublyLinkedList([10,20,30]);
dll.remove(1); // 10,30
reverse() → {DoublyLinkedList}
Reverses the linked list.
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([10,20,30]);
dll.reverse(); // 30,20,10
set(index, value) → {Boolean}
Sets the value at the specified index in the linked list.
Name | Type | Description |
---|---|---|
index | Number | The index of the value to set. |
value | Number | The new value to set. |
True if the value was set successfully, false otherwise.
- Type:
- Boolean
const dll = new DoublyLinkedList([10,20,30]);
dll.set(1, 0); // 0
shift() → {Number}
Removes the value at the start of the linked list.
The value that was removed.
- Type:
- Number
const dll = new DoublyLinkedList([10,20,30]);
dll.shift(); // 20,30
sort() → {DoublyLinkedList}
Sorts the linked list in ascending order.
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([50,20,40,10,30]);
dll.sort(); // 10,20,30,40,50
toArray() → {Array}
Returns an array representation of the linked list.
The array representation of the linked list.
- Type:
- Array
const dll = new DoublyLinkedList([10,20,30]);
dll.toArray(); // [10,20,30]
unshift(value) → {DoublyLinkedList}
Adds a new value to the beginning of the linked list.
Name | Type | Description |
---|---|---|
value | Number | The value to add. |
The current DoublyLinkedList instance.
- Type:
- DoublyLinkedList
const dll = new DoublyLinkedList([10,20,30]);
dll.unshift(0); // 0,10,20,30