DEV Community

Aashish Panchal
Aashish Panchal

Posted on

Stack

class Node {

constructor(value) {

    this.value = value;

    this.next = null;

} 
Enter fullscreen mode Exit fullscreen mode

}

class Stack {

constructor(){

    this.first = null;

    this.last = null;

    this.size = 0;

}


// Add a new Value in the list

push(val){

    var newNode = new Node(val);

    if(!this.first){

        this.first = newNode;

        this.last = newNode;

    } else {

        var temp = this.first;

        this.first = newNode;

        this.first.next = temp;

    }

    console.log(`--> You are ${++this.size} Inserted Value and this is a <- ${val}`);

}


// Delet end value in the list 

pop(){

    if(!this.first) return null;

    var temp = this.first;

    if(this.first === this.last){

        this.last = null;

    }

    this.first = this.first.next;

    this.size--;

    return temp.value;

}
Enter fullscreen mode Exit fullscreen mode

}

var stack = new Stack()

stack.push("Java Script")

stack.push("Java")

stack.push("Html")

stack.push("Css")

Top comments (0)