Quick actions

cmd+k|ctrl+k

Navigation

Languages

Stack Implementation

Snippet info

Language

JavaScript

Visibility

public

Author

anikeshdey2020

Created

2023-02-28T11:40:55.180118Z

Updated

2023-02-28T11:40:55.180118Z

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 node = new Node(value);

        node.next = this.top;

        this.top = node;
        this.length++;
        return this;
    }

    pop(){
        if(!this.top){
            return null;
        }

        if(this.top == this.bottom){
            this.bottom = null;
        }

        this.top = this.top.next;
        this.length--;
        return this;
    }
}

const myStack = new Stack();

myStack.push("google");

myStack.push("udemy");

myStack.push("discord");

//myStack.pop();

//myStack.pop();

//myStack.pop();

myStack.peek();
INFO