📄 random_queue.js
/home/palash/git/misc/algorithms/random/random_queue.js
Language: js • Lines: 143
// add a method prepend() to the linked list that adds a node to the beginning of the list

class Node {
    constructor(value){
        this.value = value
        this.next = null
    }
}

class LinkedList {
    constructor(value) {
        this.head = new Node(value) 
        this.tail = this.head;
        this.length = 1;
    }

    append(value) {
        const newNode = new Node(value)
        this.tail.next = newNode;
        this.tail = newNode;
        this.length++;
    }

    prepend(value) {
        const newNode = new Node(value)
        newNode.next = this.head
        this.head = newNode
        this.length++;
    }

    insert(index, value){
        if(index === this.length){
            this.append(value)
            return
        }

        var newNode = new Node(value)
        var currentNode = this._getNode(index-1)
        newNode.next = currentNode.next
        currentNode.next = newNode
        this.length++
    }

    remove(index){
        if(index===0){
            this.head = this._getNode(1)
            return
        }
        var previousNode = this._getNode(index - 1)
        previousNode.next = previousNode.next===null?null:previousNode.next.next 
        this.length--
    }

    get(index) {
        if(this._getNode(index))
            return this._getNode(index).value
    }

    _getNode(index){
        if(index >= this.length || index < 0){
            throw "Index out of bound"
        }

        var currentIndex = 0;
        var currentNode = this.head

        while(currentIndex < index){
            currentNode = currentNode.next
            currentIndex++
        }
        return currentNode
    }

    traverse(){
        var currentNode = this.head
        var path = ""
        while(currentNode){
            path+= currentNode.value + "--->"
            currentNode = currentNode.next
        }
        console.log(path)
        console.log()
    }

    reverse(){
        var prev = null;
        var curr = this.head;
        this.tail = this.head
        while (curr) {
            var temp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = temp;
            if(curr){
                this.head = curr
            }
        }
    }
}

class Stack {
    constructor(value){
        this.list = new LinkedList(value)
    }

    peek(){
        return this.list.get(0)
    }

    push(value) {
        this.list.append(value)
    }

    pop() {
        var value = this.peek()
        this.list.remove(0)
    }

    traverse() {
        this.list.traverse()
    }
}


var stack = new Stack(10)
console.log(stack.peek())
stack.traverse()

stack.push(15)
console.log(stack.peek())
stack.traverse()

stack.push(20)
console.log(stack.peek())
stack.traverse()

stack.pop()
console.log(stack.peek())
stack.traverse()

stack.pop()
stack.pop()
stack.pop()