Skip to content

Commit

Permalink
Initial Doubly Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
andrdc committed Oct 10, 2020
1 parent 58aeec6 commit b984d9b
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/doubly_linked_list.js
@@ -0,0 +1,46 @@
import { default as createDoublyLinkedNode } from './add_doubly_linked_node';

// Doubly Linked List

export default function DoublyLinkedList() {
return {
head: null,
tail: null,
length: 0,
isEmpty() {
return this.length == 0;
},
insertHead(value) {
let node = createDoublyLinkedNode(value);
if (this.head == null) {
this.head = node;
this.tail = node;
this.length++;
return node;
} else {
this.head.previous = node;
node.next = this.head;
this.head = node;
this.length++;
return node;
}
},
insertTail() {},
insertAt() {},
getItemAt() {},
removeHead() {},
removeTail() {},
removeFrom() {},
print() {
let current = this.head;
let listStr = '';

while (current) {
listStr += current.value + (current.next ? ', ' : '');
current = current.next;
}

return listStr;
}
};
}

0 comments on commit b984d9b

Please sign in to comment.