Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript stack

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}
class Stack{
    constructor(){
        this.top = null;
        this.bottom = null;
        this.length = 0;
    };
    peek(){
        return this.top;
    };
    push(value){
        const newNode = new Node(value);
        newNode.next = this.top
        this.top = newNode;

        if(this.length === 0) this.bottom = newNode;
        this.length++;
    }
    pop(){
        if(!this.top) return;
        if(this.top === this.bottom){
            this.top = null;
            this.bottom = null;
            this.length--;
            return
        }
        const temp = this.top
        this.top = temp.next
        this.length--;
    }
}

const a = new Stack();
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #javascript #stack
ADD COMMENT
Topic
Name
1+9 =