├── binary-search-tree.js ├── graph.js ├── hash-table.js ├── linked-list-double.js ├── linked-list-queue.js ├── linked-list-stack.js ├── linked-list-tail.js ├── linked-list.js ├── queue-array.js ├── queue-circular.js ├── queue-object.js ├── readme.md ├── stack-array.js └── stack-object.js /binary-search-tree.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value) { 3 | this.value = value; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | 9 | class BinarySearchTree { 10 | constructor() { 11 | this.root = null; 12 | } 13 | 14 | isEmpty() { 15 | return this.root === null; 16 | } 17 | 18 | insert(value) { 19 | const newNode = new Node(value); 20 | if (this.isEmpty()) { 21 | this.root = newNode; 22 | } else { 23 | this.insertNode(this.root, newNode); 24 | } 25 | } 26 | 27 | insertNode(root, newNode) { 28 | if (newNode.value < root.value) { 29 | if (root.left === null) { 30 | root.left = newNode; 31 | } else { 32 | this.insertNode(root.left, newNode); 33 | } 34 | } else { 35 | if (root.right === null) { 36 | root.right = newNode; 37 | } else { 38 | this.insertNode(root.right, newNode); 39 | } 40 | } 41 | } 42 | 43 | search(root, value) { 44 | if (!root) { 45 | return false; 46 | } 47 | if (root.value === value) { 48 | return true; 49 | } else if (value < root.value) { 50 | return this.search(root.left, value); 51 | } else { 52 | return this.search(root.right, value); 53 | } 54 | } 55 | 56 | min(root) { 57 | if (!root.left) { 58 | return root.value; 59 | } else { 60 | return this.min(root.left); 61 | } 62 | } 63 | 64 | max(root) { 65 | if (!root.right) { 66 | return root.value; 67 | } else { 68 | return this.max(root.right); 69 | } 70 | } 71 | 72 | delete(value) { 73 | this.root = this.deleteNode(this.root, value); 74 | } 75 | 76 | deleteNode(root, value) { 77 | if (root === null) { 78 | return root; 79 | } 80 | if (value < root.value) { 81 | root.left = this.deleteNode(root.left, value); 82 | } else if (value > root.value) { 83 | root.right = this.deleteNode(root.right, value); 84 | } else { 85 | if (!root.left && !root.right) { 86 | return null; 87 | } 88 | if (!root.left) { 89 | return root.right; 90 | } else if (!root.right) { 91 | return root.left; 92 | } 93 | root.value = this.min(root.right); 94 | root.right = this.deleteNode(root.right, root.value); 95 | } 96 | return root; 97 | } 98 | 99 | inOrder(root) { 100 | if (root) { 101 | this.inOrder(root.left); 102 | console.log(root.value); 103 | this.inOrder(root.right); 104 | } 105 | } 106 | 107 | preOrder(root) { 108 | if (root) { 109 | console.log(root.value); 110 | this.preOrder(root.left); 111 | this.preOrder(root.right); 112 | } 113 | } 114 | 115 | postOrder(root) { 116 | if (root) { 117 | this.postOrder(root.left); 118 | this.postOrder(root.right); 119 | console.log(root.value); 120 | } 121 | } 122 | 123 | levelOrder() { 124 | /** Use the optimised queue enqueue and dequeue from queue-object.js instead. 125 | * I've used an array for simplicity. */ 126 | const queue = []; 127 | queue.push(this.root); 128 | while (queue.length) { 129 | let curr = queue.shift(); 130 | console.log(curr.value); 131 | if (curr.left) { 132 | queue.push(curr.left); 133 | } 134 | if (curr.right) { 135 | queue.push(curr.right); 136 | } 137 | } 138 | } 139 | 140 | height(node) { 141 | if (!node) { 142 | return 0; 143 | } else { 144 | const leftHeight = this.height(node.left); 145 | const rightHeight = this.height(node.right); 146 | return Math.max(leftHeight, rightHeight) + 1; 147 | } 148 | } 149 | 150 | printLevel(node, level) { 151 | if (!node) { 152 | return; 153 | } 154 | if (level === 1) { 155 | console.log(`${node.element} `); 156 | } else if (level > 1) { 157 | this.printLevel(node.left, level - 1); 158 | this.printLevel(node.right, level - 1); 159 | } 160 | } 161 | 162 | isBST(node, min, max) { 163 | if (!node) { 164 | return true; 165 | } 166 | if (node.value < min || node.value > max) { 167 | return false; 168 | } 169 | return ( 170 | this.isBST(node.left, min, node.value) && 171 | this.isBST(node.right, node.value, max) 172 | ); 173 | } 174 | } 175 | 176 | // TODO level order and delete 177 | 178 | const bst = new BinarySearchTree(); 179 | console.log(bst.isEmpty()); 180 | bst.insert(10); 181 | bst.insert(5); 182 | bst.insert(15); 183 | bst.insert(3); 184 | bst.insert(7); 185 | bst.insert(13); 186 | bst.insert(17); 187 | bst.insert(2); 188 | console.log(bst.search(bst.root, 10)); 189 | console.log(bst.search(bst.root, 7)); 190 | bst.inOrder(); 191 | bst.preOrder(); 192 | bst.postOrder(); 193 | bst.levelOrder(); 194 | bst.printLevel(bst.root, 3); 195 | console.log(bst.min()); 196 | console.log(bst.max()); 197 | console.log(bst.height(bst.root)); 198 | -------------------------------------------------------------------------------- /graph.js: -------------------------------------------------------------------------------- 1 | class Graph { 2 | constructor() { 3 | this.adjacencyList = {}; 4 | } 5 | 6 | addVertex(vertex) { 7 | if (!this.adjacencyList[vertex]) { 8 | this.adjacencyList[vertex] = new Set(); 9 | } 10 | } 11 | 12 | addEdge(vertex1, vertex2) { 13 | if (!this.adjacencyList[vertex1]) { 14 | this.addVertex(vertex1); 15 | } 16 | if (!this.adjacencyList[vertex2]) { 17 | this.addVertex(vertex2); 18 | } 19 | this.adjacencyList[vertex1].add(vertex2); 20 | this.adjacencyList[vertex2].add(vertex1); 21 | } 22 | 23 | removeEdge(vertex1, vertex2) { 24 | this.adjacencyList[vertex1].delete(vertex2); 25 | this.adjacencyList[vertex2].delete(vertex1); 26 | } 27 | 28 | removeVertex(vertex) { 29 | if (!this.adjacencyList[vertex]) { 30 | return; 31 | } 32 | for (let adjacentVertex of this.adjacencyList[vertex]) { 33 | this.removeEdge(vertex, adjacentVertex); 34 | } 35 | delete this.adjacencyList[vertex]; 36 | } 37 | 38 | hasEdge(vertex1, vertex2) { 39 | return ( 40 | this.adjacencyList[vertex1].has(vertex2) && 41 | this.adjacencyList[vertex2].has(vertex1) 42 | ); 43 | } 44 | 45 | display() { 46 | for (let vertex in this.adjacencyList) { 47 | console.log(vertex + " -> " + [...this.adjacencyList[vertex]]); 48 | } 49 | } 50 | } 51 | 52 | const graph = new Graph(); 53 | graph.addVertex("A"); 54 | graph.addVertex("B"); 55 | graph.addVertex("C"); 56 | graph.addEdge("A", "B"); 57 | graph.addEdge("A", "C"); 58 | graph.addEdge("B", "C"); 59 | graph.display(); 60 | graph.removeEdge("A", "B"); 61 | graph.display(); 62 | graph.removeVertex("A"); 63 | graph.display(); 64 | -------------------------------------------------------------------------------- /hash-table.js: -------------------------------------------------------------------------------- 1 | class HashTable { 2 | constructor(size) { 3 | this.table = new Array(size); 4 | this.size = size; 5 | } 6 | 7 | hash(key) { 8 | let total = 0; 9 | for (let i = 0; i < key.length; i++) { 10 | total += key.charCodeAt(i); 11 | } 12 | return total % this.size; 13 | } 14 | 15 | set(key, value) { 16 | const index = this.hash(key); 17 | const bucket = this.table[index]; 18 | if (!bucket) { 19 | this.table[index] = [[key, value]]; 20 | } else { 21 | const sameKeyItem = bucket.find((item) => item[0] === key); 22 | if (sameKeyItem) { 23 | sameKeyItem[1] = value; 24 | } else { 25 | bucket.push([key, value]); 26 | } 27 | } 28 | } 29 | 30 | get(key) { 31 | const index = this.hash(key); 32 | const bucket = this.table[index]; 33 | if (bucket) { 34 | const sameKeyItem = bucket.find((item) => item[0] === key); 35 | if (sameKeyItem) { 36 | return sameKeyItem[1]; 37 | } 38 | } 39 | return undefined; 40 | } 41 | 42 | remove(key) { 43 | let index = this.hash(key); 44 | const bucket = this.table[index]; 45 | if (bucket) { 46 | const sameKeyItem = bucket.find((item) => item[0] === key); 47 | if (sameKeyItem) { 48 | bucket.splice(bucket.indexOf(sameKeyItem), 1); 49 | } 50 | } 51 | } 52 | 53 | display() { 54 | for (let i = 0; i < this.table.length; i++) { 55 | if (this.table[i]) { 56 | console.log(i, this.table[i]); 57 | } 58 | } 59 | } 60 | } 61 | 62 | const table = new HashTable(10); 63 | table.set("name", "Bruce"); 64 | table.set("age", 25); 65 | table.display(); 66 | console.log(table.get("name")); 67 | table.set("mane", "Clark"); 68 | table.set("name", "Diana"); 69 | console.log(table.get("mane")); 70 | table.remove("name"); 71 | table.display(); 72 | -------------------------------------------------------------------------------- /linked-list-double.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value) { 3 | this.value = value; 4 | this.prev = null; 5 | this.next = null; 6 | } 7 | } 8 | 9 | class DoublyLinkedList { 10 | constructor() { 11 | this.head = null; 12 | this.tail = null; 13 | this.size = 0; 14 | } 15 | 16 | isEmpty() { 17 | return this.size === 0; 18 | } 19 | 20 | getSize() { 21 | return this.size; 22 | } 23 | 24 | prepend(value) { 25 | const node = new Node(value); 26 | if (this.isEmpty()) { 27 | this.head = node; 28 | this.tail = node; 29 | } else { 30 | node.next = this.head; 31 | this.head.prev = node; 32 | this.head = node; 33 | } 34 | this.size++; 35 | } 36 | 37 | append(value) { 38 | const node = new Node(value); 39 | if (this.isEmpty()) { 40 | this.head = node; 41 | this.tail = node; 42 | } else { 43 | this.tail.next = node; 44 | node.prev = this.tail; 45 | this.tail = node; 46 | } 47 | this.size++; 48 | } 49 | 50 | removeFromFront() { 51 | if (this.isEmpty()) { 52 | return null; 53 | } 54 | const value = this.head.value; 55 | this.head = this.head.next; 56 | this.size--; 57 | return value; 58 | } 59 | 60 | removeFromEnd() { 61 | if (this.isEmpty()) { 62 | return null; 63 | } 64 | const value = this.tail.value; 65 | if (this.size === 1) { 66 | this.head = null; 67 | this.tail = null; 68 | } else { 69 | this.tail = this.tail.prev; 70 | this.tail.next = null; 71 | } 72 | this.size--; 73 | return value; 74 | } 75 | 76 | print() { 77 | if (this.isEmpty()) { 78 | console.log("List is empty"); 79 | } else { 80 | let curr = this.head; 81 | let list = ""; 82 | while (curr) { 83 | list += `${curr.value}<->`; 84 | curr = curr.next; 85 | } 86 | console.log(list); 87 | } 88 | } 89 | 90 | printReverse() { 91 | if (this.isEmpty()) { 92 | console.log("List is empty"); 93 | } else { 94 | let curr = this.tail; 95 | let list = ""; 96 | while (curr) { 97 | list += `${curr.value}<->`; 98 | curr = curr.prev; 99 | } 100 | console.log(list); 101 | } 102 | } 103 | } 104 | 105 | const list = new DoublyLinkedList(); 106 | list.append(1); 107 | list.append(2); 108 | list.append(3); 109 | list.prepend(0); 110 | list.print(); 111 | list.printReverse(); 112 | list.removeFromEnd(); 113 | list.print(); 114 | list.removeFromFront(); 115 | list.print(); 116 | -------------------------------------------------------------------------------- /linked-list-queue.js: -------------------------------------------------------------------------------- 1 | const LinkedList = require("./linked-list-tail"); 2 | 3 | class LinkedListQueue { 4 | constructor() { 5 | this.list = new LinkedList(); 6 | } 7 | 8 | enqueue(value) { 9 | this.list.append(value); 10 | } 11 | 12 | dequeue() { 13 | return this.list.removeFromFront(); 14 | } 15 | 16 | peek() { 17 | return this.list.head.value; 18 | } 19 | 20 | isEmpty() { 21 | return this.list.isEmpty(); 22 | } 23 | 24 | getSize() { 25 | return this.list.getSize(); 26 | } 27 | 28 | print() { 29 | return this.list.print(); 30 | } 31 | } 32 | 33 | const queue = new LinkedListQueue(); 34 | console.log(queue.isEmpty()); 35 | queue.enqueue(10); 36 | queue.enqueue(20); 37 | queue.enqueue(30); 38 | console.log(queue.getSize()); 39 | queue.print(); 40 | console.log(queue.dequeue()); 41 | queue.print(); 42 | console.log(queue.peek()); 43 | -------------------------------------------------------------------------------- /linked-list-stack.js: -------------------------------------------------------------------------------- 1 | const LinkedList = require("./linked-list-tail"); 2 | 3 | class LinkedListStack { 4 | constructor() { 5 | this.list = new LinkedList(); 6 | } 7 | 8 | push(value) { 9 | this.list.prepend(value); 10 | } 11 | 12 | pop() { 13 | return this.list.removeFromFront(); 14 | } 15 | 16 | peek() { 17 | return this.list.head.value; 18 | } 19 | 20 | isEmpty() { 21 | return this.list.isEmpty(); 22 | } 23 | 24 | getSize() { 25 | return this.list.getSize(); 26 | } 27 | 28 | print() { 29 | return this.list.print(); 30 | } 31 | } 32 | 33 | const stack = new LinkedListStack(); 34 | console.log(stack.isEmpty()); 35 | stack.push(20); 36 | stack.push(10); 37 | stack.push(30); 38 | console.log(stack.getSize()); 39 | stack.print(); 40 | console.log(stack.pop()); 41 | stack.print(); 42 | console.log(stack.peek()); 43 | -------------------------------------------------------------------------------- /linked-list-tail.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value) { 3 | this.value = value; 4 | this.next = null; 5 | } 6 | } 7 | 8 | class LinkedList { 9 | constructor() { 10 | this.head = null; 11 | this.tail = null; 12 | this.size = 0; 13 | } 14 | 15 | isEmpty() { 16 | return this.size === 0; 17 | } 18 | 19 | getSize() { 20 | return this.size; 21 | } 22 | 23 | prepend(value) { 24 | const node = new Node(value); 25 | if (this.isEmpty()) { 26 | this.head = node; 27 | this.tail = node; 28 | } else { 29 | node.next = this.head; 30 | this.head = node; 31 | } 32 | this.size++; 33 | } 34 | 35 | append(value) { 36 | const node = new Node(value); 37 | if (this.isEmpty()) { 38 | this.head = node; 39 | this.tail = node; 40 | } else { 41 | this.tail.next = node; 42 | this.tail = node; 43 | } 44 | this.size++; 45 | } 46 | 47 | removeFromFront() { 48 | if (this.isEmpty()) { 49 | return null; 50 | } 51 | const value = this.head.value; 52 | this.head = this.head.next; 53 | this.size--; 54 | return value; 55 | } 56 | 57 | removeFromEnd() { 58 | if (this.isEmpty()) { 59 | return null; 60 | } 61 | const value = this.tail.value; 62 | if (this.size === 1) { 63 | this.head = null; 64 | this.tail = null; 65 | } else { 66 | let prev = this.head; 67 | while (prev.next !== this.tail) { 68 | prev = prev.next; 69 | } 70 | prev.next = null; 71 | this.tail = prev; 72 | } 73 | this.size--; 74 | return value; 75 | } 76 | 77 | reverse() { 78 | let current = this.head; 79 | let prev = null; 80 | let next = null; 81 | while (current) { 82 | next = current.next; 83 | current.next = prev; 84 | prev = current; 85 | current = next; 86 | } 87 | this.tail = this.head; 88 | this.head = prev; 89 | } 90 | 91 | print() { 92 | if (this.isEmpty()) { 93 | console.log("List is empty"); 94 | } else { 95 | let curr = this.head; 96 | let list = ""; 97 | while (curr) { 98 | list += `${curr.value}->`; 99 | curr = curr.next; 100 | } 101 | console.log(list); 102 | } 103 | } 104 | } 105 | 106 | module.exports = LinkedList; 107 | 108 | /** Uncomment when testing only this file */ 109 | /** 110 | const list = new LinkedList(); 111 | list.append(1); 112 | list.append(2); 113 | list.append(3); 114 | list.prepend(0); 115 | list.print(); 116 | console.log(list.getSize()); 117 | list.removeFromFront(); 118 | list.print(); 119 | list.removeFromEnd(); 120 | list.print(); 121 | */ 122 | -------------------------------------------------------------------------------- /linked-list.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value) { 3 | this.value = value; 4 | this.next = null; 5 | } 6 | } 7 | 8 | class LinkedList { 9 | constructor() { 10 | this.head = null; 11 | this.size = 0; 12 | } 13 | 14 | isEmpty() { 15 | return this.size === 0; 16 | } 17 | 18 | getSize() { 19 | return this.size; 20 | } 21 | 22 | prepend(value) { 23 | const node = new Node(value); 24 | if (this.isEmpty()) { 25 | this.head = node; 26 | } else { 27 | node.next = this.head; 28 | this.head = node; 29 | } 30 | this.size++; 31 | } 32 | 33 | append(value) { 34 | const node = new Node(value); 35 | if (this.isEmpty()) { 36 | this.head = node; 37 | } else { 38 | let curr = this.head; 39 | while (curr.next) { 40 | curr = curr.next; 41 | } 42 | curr.next = node; 43 | } 44 | this.size++; 45 | } 46 | 47 | insert(value, index) { 48 | if (index < 0 || index > this.size) { 49 | return; 50 | } 51 | if (index === 0) { 52 | this.prepend(value); 53 | } else { 54 | const node = new Node(value); 55 | let prev = this.head; 56 | for (let i = 0; i < index - 1; i++) { 57 | prev = prev.next; 58 | } 59 | node.next = prev.next; 60 | prev.next = node; 61 | this.size++; 62 | } 63 | } 64 | 65 | removeFrom(index) { 66 | if (index < 0 || index >= this.size) { 67 | return null; 68 | } 69 | let removedNode; 70 | if (index === 0) { 71 | removedNode = this.head; 72 | this.head = this.head.next; 73 | } else { 74 | let prev = this.head; 75 | for (let i = 0; i < index - 1; i++) { 76 | prev = prev.next; 77 | } 78 | removedNode = prev.next; 79 | prev.next = removedNode.next; 80 | } 81 | this.size--; 82 | return removedNode.value; 83 | } 84 | 85 | removeValue(value) { 86 | if (this.isEmpty()) { 87 | return null; 88 | } 89 | if (this.head.value === value) { 90 | this.head = this.head.next; 91 | this.size--; 92 | return value; 93 | } else { 94 | let prev = this.head; 95 | while (prev.next && prev.next.value !== value) { 96 | prev = prev.next; 97 | } 98 | if (prev.next) { 99 | removedNode = prev.next; 100 | prev.next = removedNode.next; 101 | this.size--; 102 | return value; 103 | } 104 | return null; 105 | } 106 | } 107 | 108 | search(value) { 109 | if (this.isEmpty()) { 110 | return -1; 111 | } 112 | let i = 0; 113 | let curr = this.head; 114 | while (curr) { 115 | if (curr.value === value) { 116 | return i; 117 | } 118 | curr = curr.next; 119 | i++; 120 | } 121 | return -1; 122 | } 123 | 124 | reverse() { 125 | let prev = null; 126 | let curr = this.head; 127 | while (curr) { 128 | let next = curr.next; 129 | curr.next = prev; 130 | prev = curr; 131 | curr = next; 132 | } 133 | this.head = prev; 134 | } 135 | 136 | print() { 137 | if (this.isEmpty()) { 138 | console.log("List is empty"); 139 | } else { 140 | let curr = this.head; 141 | let list = ""; 142 | while (curr) { 143 | list += `${curr.value}->`; 144 | curr = curr.next; 145 | } 146 | console.log(list); 147 | } 148 | } 149 | } 150 | 151 | const l = new LinkedList(); 152 | 153 | console.log(l.isEmpty()); 154 | l.append(50); 155 | l.prepend(20); 156 | l.append(80); 157 | l.insert(60, 2); 158 | console.log(l.getSize()); 159 | l.print(); 160 | l.reverse(); 161 | l.print(); 162 | console.log(l.search(60)); 163 | l.removeFrom(4); 164 | console.log(l.getSize()); 165 | l.print(); 166 | l.removeValue(80); 167 | l.print(); 168 | console.log(l.getSize()); 169 | l.print(); 170 | -------------------------------------------------------------------------------- /queue-array.js: -------------------------------------------------------------------------------- 1 | class Queue { 2 | constructor() { 3 | this.items = []; 4 | } 5 | 6 | enqueue(element) { 7 | this.items.push(element); 8 | } 9 | 10 | dequeue() { 11 | return this.items.shift(); 12 | } 13 | 14 | peek() { 15 | if (!this.isEmpty()) { 16 | return this.items[0]; 17 | } 18 | return null; 19 | } 20 | 21 | isEmpty() { 22 | return this.items.length === 0; 23 | } 24 | 25 | size() { 26 | return this.items.length; 27 | } 28 | 29 | print() { 30 | console.log(this.items.toString()); 31 | } 32 | } 33 | 34 | const queue = new Queue(); 35 | console.log(queue.isEmpty()); 36 | queue.enqueue(10); 37 | queue.enqueue(20); 38 | queue.enqueue(30); 39 | console.log(queue.size()); 40 | queue.print(); 41 | console.log(queue.dequeue()); 42 | console.log(queue.peek()); 43 | queue.print(); 44 | -------------------------------------------------------------------------------- /queue-circular.js: -------------------------------------------------------------------------------- 1 | class CircularQueue { 2 | constructor(capacity) { 3 | this.items = new Array(capacity); 4 | this.rear = -1; 5 | this.front = -1; 6 | this.currentLength = 0; 7 | this.capacity = capacity; 8 | } 9 | 10 | isFull() { 11 | return this.currentLength === this.capacity; 12 | } 13 | 14 | isEmpty() { 15 | return this.currentLength === 0; 16 | } 17 | 18 | size() { 19 | return this.currentLength; 20 | } 21 | 22 | enqueue(item) { 23 | if (!this.isFull()) { 24 | this.rear = (this.rear + 1) % this.capacity; 25 | this.items[this.rear] = item; 26 | this.currentLength += 1; 27 | if (this.front === -1) { 28 | this.front = this.rear; 29 | } 30 | } 31 | } 32 | 33 | dequeue() { 34 | if (this.isEmpty()) { 35 | return null; 36 | } 37 | const item = this.items[this.front]; 38 | this.items[this.front] = null; 39 | this.front = (this.front + 1) % this.capacity; 40 | this.currentLength -= 1; 41 | if (this.isEmpty()) { 42 | this.front = -1; 43 | this.rear = -1; 44 | } 45 | return item; 46 | } 47 | 48 | peek() { 49 | if (!this.isEmpty()) { 50 | return this.items[this.front]; 51 | } 52 | return null; 53 | } 54 | 55 | print() { 56 | if (this.isEmpty()) { 57 | console.log("Queue is empty"); 58 | } else { 59 | let i; 60 | let str = ""; 61 | for (i = this.front; i !== this.rear; i = (i + 1) % this.capacity) { 62 | str += this.items[i] + " "; 63 | } 64 | str += this.items[i]; 65 | console.log(str); 66 | } 67 | } 68 | } 69 | 70 | const queue = new CircularQueue(5); 71 | console.log(queue.isEmpty()); 72 | queue.enqueue(10); 73 | queue.enqueue(20); 74 | queue.enqueue(30); 75 | queue.enqueue(40); 76 | queue.enqueue(50); 77 | console.log(queue.size()); 78 | queue.print(); 79 | console.log(queue.isFull()); 80 | console.log(queue.dequeue()); 81 | console.log(queue.peek()); 82 | queue.print(); 83 | queue.enqueue(60); 84 | queue.print(); 85 | -------------------------------------------------------------------------------- /queue-object.js: -------------------------------------------------------------------------------- 1 | class Queue { 2 | constructor() { 3 | this.items = {}; 4 | this.front = 0; 5 | this.rear = 0; 6 | } 7 | 8 | enqueue(element) { 9 | this.items[this.rear] = element; 10 | this.rear++; 11 | } 12 | 13 | dequeue() { 14 | const item = this.items[this.front]; 15 | delete this.items[this.front]; 16 | this.front++; 17 | return item; 18 | } 19 | 20 | peek() { 21 | return this.items[this.front]; 22 | } 23 | 24 | size() { 25 | return this.rear - this.front; 26 | } 27 | 28 | isEmpty() { 29 | return this.rear - this.front === 0; 30 | } 31 | 32 | print() { 33 | console.log(this.items); 34 | } 35 | } 36 | 37 | const queue = new Queue(); 38 | console.log(queue.isEmpty()); 39 | queue.enqueue(10); 40 | queue.enqueue(20); 41 | queue.enqueue(30); 42 | console.log(queue.size()); 43 | queue.print(); 44 | console.log(queue.dequeue()); 45 | console.log(queue.peek()); 46 | console.log(queue.isEmpty()); 47 | queue.print(); 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Source code related to the JavaScript Data Structures tutorial series on YouTube. -------------------------------------------------------------------------------- /stack-array.js: -------------------------------------------------------------------------------- 1 | class Stack { 2 | constructor() { 3 | this.items = []; 4 | } 5 | 6 | push(element) { 7 | this.items.push(element); 8 | } 9 | 10 | pop() { 11 | return this.items.pop(); 12 | } 13 | 14 | peek() { 15 | return this.items[this.items.length - 1]; 16 | } 17 | 18 | isEmpty() { 19 | return this.items.length === 0; 20 | } 21 | 22 | size() { 23 | return this.items.length; 24 | } 25 | 26 | print() { 27 | console.log(this.items.toString()); 28 | } 29 | } 30 | 31 | const stack = new Stack(); 32 | console.log(stack.isEmpty()); 33 | stack.push(20); 34 | stack.push(10); 35 | stack.push(30); 36 | console.log(stack.size()); 37 | stack.print(); 38 | console.log(stack.pop()); 39 | console.log(stack.peek()); 40 | stack.print(); 41 | -------------------------------------------------------------------------------- /stack-object.js: -------------------------------------------------------------------------------- 1 | class Stack { 2 | constructor() { 3 | this.items = {}; 4 | this.head = 0; 5 | } 6 | 7 | push(element) { 8 | this.items[this.head] = element; 9 | this.head++; 10 | } 11 | 12 | pop() { 13 | const item = this.items[this.head - 1]; 14 | delete this.items[this.head - 1]; 15 | this.head--; 16 | return item; 17 | } 18 | 19 | peek() { 20 | return this.items[this.head - 1]; 21 | } 22 | 23 | size() { 24 | return this.head; 25 | } 26 | 27 | isEmpty() { 28 | return this.head === 0; 29 | } 30 | 31 | print() { 32 | console.log(this.items); 33 | } 34 | } 35 | --------------------------------------------------------------------------------