Double-Queue Code needs to be reduced

前端 未结 1 1991
南方客
南方客 2021-01-26 02:22

Is there any way I can reduce this code to do the same thing but with 100 less characters?

It\'s a simple double edge queue that has pushHead,popHead,pushTail,popTail, a

1条回答
  •  别那么骄傲
    2021-01-26 03:01

    You could get rid of the manual array handling. I guess you can't get any shorter than this (you could shorten variable names, but the code probably needs to be at least this long).

    function Deque() {}
    Deque.prototype = new Array();
    var prot = Deque.prototype;
    prot.pushHead = Deque.prototype.unshift;
    prot.popHead = Deque.prototype.shift;
    prot.pushTail = Deque.prototype.push
    prot.popTail = Deque.prototype.pop
    prot.isEmpty = function() {return this.length == 0}
    

    This way, you also get all the functionality of the default Arrays as well. Deque in this example effectively a sub-class of the Array class.

    0 讨论(0)
提交回复
热议问题