Javascript Data Structure Stack via function

Description

Javascript Data Structure Stack via function


function Stack() {

    let items = [];

    this.push = function(element){
        items.push(element);/*  w  ww  .  jav a  2  s. com*/
    };

    this.pop = function(){
        return items.pop();
    };

    this.peek = function(){
        return items[items.length-1];
    };

    this.isEmpty = function(){
        return items.length == 0;
    };

    this.size = function(){
        return items.length;
    };

    this.clear = function(){
        items = [];
    };

    this.print = function(){
        console.log(items.toString());
    };

    this.toString = function(){
        return items.toString();
    };
}
let s = new Stack();
s.push("a");
s.push("b");
s.push("c");
s.print();



PreviousNext

Related