1670. 设计前中后队列

83 阅读1分钟

请你设计一个队列,支持在前,中,后三个位置的 push 和 pop 操作。

请你完成 FrontMiddleBack 类:

FrontMiddleBack() 初始化队列。 void pushFront(int val) 将 val 添加到队列的 最前面 。 void pushMiddle(int val) 将 val 添加到队列的 正中间 。 void pushBack(int val) 将 val 添加到队里的 最后面 。 int popFront() 将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 int popMiddle() 将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 int popBack() 将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 请注意当有 两个 中间位置的时候,选择靠前面的位置进行操作。比方说:

将 6 添加到 [1, 2, 3, 4, 5] 的中间位置,结果数组为 [1, 2, 6, 3, 4, 5] 。 从 [1, 2, 3, 4, 5, 6] 的中间位置弹出元素,返回 3 ,数组变为 [1, 2, 4, 5, 6] 。

     constructor(val , next , pre){
         this.val = val;
         this.next = next ;
         this.pre = pre ;
     }
     insert_pre (node){
         node.next = this ;
         node.pre = this.pre ;
         this.pre && (this.pre.next =  node)
         this.pre = node ;
     }
     insert_next(node){
         node.pre  =this ;
         node.next = this.next ;
         this.next &&(this.next.pre = node);
         this.next =  node ;
     }
     /* 删除当前元素 */
     erase(){
         this.pre &&(this.pre.next = this.next)
         this.next &&(this.next.pre = this.pre)
     }
 }

 class MyQueue{
     constructor(){
         this.count = 0;
         this.head = new Node(-1) ;
         this.tail = new Node(-1) ;
         this.head.next = this.tail ;
         this.tail.pre = this.head ;

     }
     pushBack(val){
         this.tail.insert_pre(new Node(val))
         this.count ++;
         return

     }
     popBack(){
         let ret = this.tail.pre  ;
         if(this.count){
             ret.erase();
             this.count--;

         }
         return ret ;
     }
    pushFront(val){
       this.head.insert_next(new Node(val))
       this.count++
       return 
    }
    popFront(){
        let ret = this.head.next ;
        if(this.count){
            ret.erase()
            this.count--
        }
        return ret ;
    }
    get front(){
        return this.head.next.val ;
    }
    get back(){
        return this.tail.pre.val ;
    }
    get size(){
        return this.count 
    }
 }
var FrontMiddleBackQueue = function() {
    this.q1 = new MyQueue()
    this.q2 = new MyQueue()
};
 FrontMiddleBackQueue.prototype.balance= function (){
     if(this.q1.size < this.q2.size){
         this.q1.pushBack(this.q2.popFront().val)
     }else if(this.q1.size -this.q2.size >1){
         this.q2.pushFront(this.q1.popBack().val)
     } 
     return
 }

/** 
 * @param {number} val
 * @return {void}
 */
FrontMiddleBackQueue.prototype.pushFront = function(val) {
    this.q1.pushFront(val);
    this.balance()
    return
};

/** 
 * @param {number} val
 * @return {void}
 */
FrontMiddleBackQueue.prototype.pushMiddle = function(val) {
    // if(this.q1.size === this.q2.size){
    //     this.q1.pushBack(val)
    // }else{
    //     this.q2.pushFront(this.q1.popBack())
    //     this.q1.pushBack(val)
    // }
    if(this.q1.size > this.q2.size){
        this.q2.pushFront(this.q1.popBack().val)
    }
    this.q1.pushBack(val)
    return 
};

/** 
 * @param {number} val
 * @return {void}
 */
FrontMiddleBackQueue.prototype.pushBack = function(val) {
    this.q2.pushBack(val)
    this.balance()
    return 
};

/**
 * @return {number}
 */
FrontMiddleBackQueue.prototype.popFront = function() {
    if(this.q1.size + this.q2.size === 0) return -1
    let  ret =  this.q1.popFront()
    this.balance() ;

    return ret.val
};

/**
 * @return {number}
 */
FrontMiddleBackQueue.prototype.popMiddle = function() {
    if(this.q1.size + this.q2.size === 0) return -1
    let ret = this.q1.popBack()
    this.balance()
    return ret.val
};

/**
 * @return {number}
 */
FrontMiddleBackQueue.prototype.popBack = function() {
    if(this.q1.size + this.q2.size === 0) return -1
    let ret ;
    if(this.q2.size){ ret = this.q2.popBack()}else{
        ret = this.q1.popBack()
    }
    this.balance()
    return ret.val 
};