1. 程式人生 > 其它 >位元組跳動面試題(封裝一個佇列)

位元組跳動面試題(封裝一個佇列)

技術標籤:面試題javascript

function ArrayQueue(){  
    var arr = [];  
        //入隊操作  
    this.push = function(element){  
        arr.push(element);  
        return true;  
    }  
        //出隊操作  
    this.pop = function(){  
        return arr.shift();  
    }  
        //獲取隊首  
    this.getFront = function
(){ return arr[0]; } //獲取隊尾 this.getRear = function(){ return arr[arr.length - 1] } //清空佇列 this.clear = function(){ arr = []; } //獲取隊長 this.size = function(){ return arr.length; } }