Doubly Linked List in Javascript (Data Structures)
Short and Precise Js code to create a doubly linked list
Almost identical to the singly-linked lists, except every node has another pointer, to the previous node. It provides more flexibility over singly-linked lists.
Todos:
1. Push and Pop
2. Unshift and Shift
3. Get and Set
4. Insert and Remove
Creating a Node: It contains a value and two pointers next, prev.
Doubly Linked List: three important things - head, tail, length
Push: Adding a node to the end of the doubly linked list
- This function will accept a value. Create a new node with the value passed to the function.
- If the head property is null, then set the head and tail to be the new node.
- If not, set the next property on the tail of DLL to that node.
- Set the previous property on the new node to be the tail.
- Set the tail to be the newly created node.
- Increment the length of the DLL by one.
- Return DLL.
Pop: removing a node from the end of the DLL.
- If there is no head, return undefined.
- Store the current tail in a variable to return later.
- If length is one, set the head and tail to be null;
- Update the tail to be the previous node.
- Set the new tail’s next value to null.
- Decrement the length by one.
- Return the removed value.
Unshift: adding a new node to the beginning of the DLL.
- This function should accept a value and create a new node using that value.
- If the length of the DLL is zero, set the head and tail to be the new node.
- Otherwise,
- Set the prev property on the head of the list to be the new node.
- Set the next property on the new node to the current head.
- Update the head of the DLL to be the newly created node.
4. Increment the length by one.
5. Return the list.
Shift: removing a node from the beginning of the DLL.
- If length is zero, return undefined.
- Store the value of the head in a variable.
- If length is one, set the head and tail to null.
- Update the head to be the next of the old head.
- Set the head's previous property to null.
- Set the old heads next to null.
- Decrement the length by one.
- Return old head.
Get: accessing a node in a DLL.
- This function will accept an index. If the index is less than 0 (zero) or ≥ (gte) to the length, return null.
- If the index is ≤ (lte) half of the length of the list.
- Loop through the list starting from the head and loop towards the middle.
- Return the node once it's found.
3. If the index is ≥ (gte) half of the length of the list.
- Loop through the list starting from the tail and loop towards the middle.
- Return the node once it is found.
Set: replacing the value of the node to the index in a DLL.
- Create a variable which is the result of the get method at the index passed to the function.
- If the get method returns a valid node, set the value of that node to be the value passed to the function.
- Return true.
2. Otherwise, return false.
Insert: adding a node in a DLL at a certain position.
- If the index is less than zero or ≥ (gte) the length return false.
- If the index is zero, unshift.
- If the index is the same as the length, push.
- Use the get method to access the index-1.
- Set the next and prev property on the correct nodes to link everything together.
- Increment the length by one.
- return true.
Remove: removing a node in the DLL from a certain position.
- If the index is less than 0 or ≥ (gte) the length return undefined.
- If the index is zero, shift.
- If the index is the same as the length-1, pop.
- Use the get method to retrieve the item to be removed.
- Update the next and previous properties to remove the found node from the list.
- Set next and previous to null on the found node.
- Decrement the length by one.
- Return the removed node.
Time Complexity of Doubly Linked List:
- Access: O(N)
- Search: O(N)
- Insertion: O(1)
- Removal: O(1)
Summary:
- Almost identical to SLL except there is an additional pointer to the previous node.
- Better at finding nodes and can do it in half time compared to SLL.
- They need more memory considering the extra pointer.