Quick actions

cmd+k|ctrl+k

Navigation

Languages

Stacks

Snippet info

Language

JavaScript

Visibility

public

Author

jameeulla3

Created

2024-08-21T05:10:20.710349Z

Updated

2024-08-21T10:03:34.949277Z

class Node{
    constructor(value){
        this.data = value; 
        this.next = null;
    }
}
class Stack{
    constructor()
    {
        this.length = 0; 
        this.top = null; 
    }
    push(value)
    {
        let newNode = new Node(value);
        if(this.top==null)
        this.top = newNode; 
        else 
        {
            newNode.next = this.top; 
            this.top = newNode; 
        }
        this.length++;
    }
    pop()
    {
        let temp = this.top; 
        this.top = temp.next; 
        this.length--;
        return temp.data;
    }
    peek(){
        return this.top.data; 
    }
}
let obj = new Stack(); 
obj.push('google');
obj.push('udemy');
obj.push('satellite');
console.log(obj.pop()); 
console.log(obj.peek());
INFO