DoublyLinkedList

Represents a doubly linked list data structure.

Constructor

new DoublyLinkedList(value)

Parameters:
NameTypeDescription
valueArray | *

The value to initialize the list with (optional).

Properties
NameTypeDescription
headDLLNode

The first node in the list

tailDLLNode

The last node in the list

sizeNumber

The number of nodes in the list

Example
new DoublyLinkedList();
new DoublyLinkedList("Beep");
new DoublyLinkedList([10,20,30]);

Methods

clear() → {DoublyLinkedList}

Clears the linked list.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
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.

Parameters:
NameTypeDefaultDescription
indexNumber

The index of the value to retrieve.

returnNodeBooleanfalse

Whether to return the Node or the value.

Returns:

The value at the specified index, or the Node if returnNode is true.

Type: 
Number | DLLNode
Example
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.

Parameters:
NameTypeDescription
indexNumber

The index to insert the value at.

valueNumber

The value to insert.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
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.

Returns:

The value that was removed.

Type: 
Number
Example
const dll = new DoublyLinkedList([10,20,30]);
dll.pop(); // 10,20

print() → {DoublyLinkedList}

Prints the values of the linked list.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
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.

Parameters:
NameTypeDescription
valueNumber

The value to add.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
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.

Parameters:
NameTypeDescription
indexNumber

The index of the value to remove.

Returns:

The value that was removed.

Type: 
Number
Example
const dll = new DoublyLinkedList([10,20,30]);
dll.remove(1); // 10,30

reverse() → {DoublyLinkedList}

Reverses the linked list.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
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.

Parameters:
NameTypeDescription
indexNumber

The index of the value to set.

valueNumber

The new value to set.

Returns:

True if the value was set successfully, false otherwise.

Type: 
Boolean
Example
const dll = new DoublyLinkedList([10,20,30]);
dll.set(1, 0); // 0

shift() → {Number}

Removes the value at the start of the linked list.

Returns:

The value that was removed.

Type: 
Number
Example
const dll = new DoublyLinkedList([10,20,30]);
dll.shift(); // 20,30

sort() → {DoublyLinkedList}

Sorts the linked list in ascending order.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
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.

Returns:

The array representation of the linked list.

Type: 
Array
Example
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.

Parameters:
NameTypeDescription
valueNumber

The value to add.

Returns:

The current DoublyLinkedList instance.

Type: 
DoublyLinkedList
Example
const dll = new DoublyLinkedList([10,20,30]);
dll.unshift(0); // 0,10,20,30