Data structures

Linked list

class LinkedList {
    constructor(value) {
        this.head = null;
        this.length = 0;
        this.addToHead(value);
    }
    
    addToHead(value) {
        const newNode = { value };
        newNode.next = this.head;
        this.head = newNode;
        this.length++;
        return this;
    }
    
    removeFromHead() {
        if (this.length === 0) {
            return undefined;
        }
        
        const value = this.head.value;
        this.head = this.head.next;
        this.length--;
        
        return value;
    }
    
    find(val) {
        let thisNode = this.head;
        
        while(thisNode) {
            if(thisNode.value === val) {
                return thisNode;
            }
            
            thisNode = thisNode.next;
        }
        
        return thisNode;
    }
    
    remove(val) {
        if(this.length === 0) {
            return undefined;
        }
        
        if (this.head.value === val) {
            return this.removeFromHead();
        }
        
        let previousNode = this.head;
        let thisNode = previousNode.next;
        
        while(thisNode) {
            if(thisNode.value === val) {
                break;
            }
            
            previousNode = thisNode;
            thisNode = thisNode.next;
        }
        
        if (thisNode === null) {
            return undefined;
        }
        
        previousNode.next = thisNode.next;
        this.length--;
        return this;
    }
}

Double linked list

    function Node(value, next, prev) {
        this.value = value;
        this.next = next;
        this.prev = prev;
    }

    class LinkedList {
        constructor() {
            this.head = null;
            this.tail = null;
        }

        addToHead(value) {
            const newNode = new Node(value, this.head, null);
            if (this.head) this.head.prev = newNode;
            else this.tail = newNode;
            this.head = newNode;
        }

        addToTail(value) {
            const newNode = new Node(value, null, this.tail);
            if (this.tail) this.tail.next = newNode;
            else this.head = newNode;
            this.tail = newNode;
        }

        removeHead() {
            if (!this.head) return null;
            let value = this.head.value;
            this.head = this.head.next;
        
            if (this.head) this.head.prev = null;
            else this.tail = null;
        
            return value;
        
        }

        removeTail() {
            if (!this.tail) return null;
            let value = this.tail.value;
            this.tail = this.tail.prev;
        
            if (this.tail) this.tail.next = null;
            else this.head = null;
        
            return value;
        }

        search(searchValue) {
            let currentNode = this.head;
      
            while (currentNode) {
                if (currentNode.value === searchValue) return currentNode;
                currentNode = currentNode.next;
            }
            return null;
        }
    }

Queue

class Queue {
    constructor() {
        this.data = [];
    }

    add(record) {
        this.data.unshift(record);
    }

    remove() {
        this.data.pop();
    }

    first() {
        return this.data[0];
    }

    last() {
        return this.data[this.data.length - 1];
    }

    size() {
        return this.data.length;
    }
}

BST

(Binary Search Tree)

    class BinarySearchTree {
        constructor (value) {
            this.root = new TreeNode(value)
        }

        addNode(value, node = this.root) {
            if (!node.leftChild && node.value > value) {
                node.leftChild = new TreeNode(value);
                return this;
            }
            if (!node.rightChild && node.value < value) {
                node.rightChild = new TreeNode(value);
                return this; 
            }
            return this.addNode(value, value > node.value ? node.rightChild : node.leftChild); 
        }

        _findMin(node) {
            if (node.leftChild !== null) {
                return this._findMin(node.leftChild);
            } else {
                return node;
            }
        }

        deleteNode(node, value) {
            if (node === null) {
                return null;
            }
            if (value < node.value) {
                node.leftChild = this.deleteNode(node.leftChild, value);
                return node;
            } else if (value > node.value) {
                node.rightChild = this.deleteNode(node.rightChild, value);
                return node;
            }
            if (node.leftChild === null) {
                return node.rightChild;
            } else if (node.rightChild === null) {
                return node.leftChild;
            } else {
                const minKey = this._findMin(node.rightChild).value;
                node.value = minKey;
                node.rightChild = this.deleteNode(node.rightChild, value);
                return node;
            }
        }

        searchNode (node, value) {
            if (node) {
                if (node.value === value) {
                    return node;
                }
                this.searchPosition(node.leftChild, value);
                this.searchPosition(node.rightChild, value);
            }
        }
    }

    class TreeNode {
        constructor (value, leftChild = null, rightChild = null) {
            this.value = value;
            this.leftChild = leftChild;
            this.rightChild = rightChild;
        }
    }

Stack

class Stack { 
  
    constructor() { 
        this.items = []; 
    } 
    
    push(element) { 
        this.items.push(element); 
    } 

    pop() { 
        if (this.items.length == 0) 
            return "Underflow"; 
        return this.items.pop(); 
    }   

    peek() { 
        return this.items[this.items.length - 1]; 
    }
    
    isEmpty() { 
        return this.items.length == 0; 
    }  

    printStack() { 
        let str = ""; 
        for (let i = 0; i < this.items.length; i++) 
            str += this.items[i] + " "; 
        return str; 
    } 
} 

deck

By Gleb Zhidkov

deck

  • 37