早上参加小孩的一年级入学前,看看相关的东东啦。。

javascript栈的建立样码

function Stack() {
    var items = [];

    this.push = function(element){
        items.push(element);
    }

    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());
    }
}

var stack = new Stack();
console.log(stack.isEmpty());

stack.push(5);
stack.push(8);
console.log(stack.peek());
stack.push(11);
console.log(stack.size());
console.log(stack.isEmpty());
stack.push(15);
stack.pop();
stack.pop();
console.log(stack.size());
stack.print();

javascript栈的建立样码

相关文章:

  • 2022-12-23
  • 2021-12-15
  • 2021-11-02
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-08-04
  • 2021-06-17
猜你喜欢
  • 2021-09-28
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案