📜  javascript代码示例中的堆栈

📅  最后修改于: 2022-03-11 15:01:31.681000             🧑  作者: Mango

代码示例1
class Stack{
   constructor() 
    { 
        this.items = []; 
    } 
   
    push(element) 
   { 
    // push element into the items 
    this.items.push(element); 
    }
  
    pop() 
    { 
    if (this.items.length == 0) 
        return "Underflow"; 
    return this.items.pop(); 
    } 
  
    peek() 
    { 
        return this.items[this.items.length - 1]; 
    } 
  
    printStack() 
    { 
        var str = ""; 
        for (var i = 0; i < this.items.length; i++) 
            str += this.items[i] + " "; 
        return str; 
    } 

}