Code coverage report for lib/algorithms/3-stacks/stackQueue.js

Statements: 92% (23 / 25)      Branches: 0% (0 / 2)      Functions: 85.71% (6 / 7)      Lines: 92% (23 / 25)      Ignored: none     

All files » lib/algorithms/3-stacks/ » stackQueue.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  1   1 6 6 6     1 4 6       1 9         1 9   9   9     1 2   2   2     1       1 2   2     1  
//3.5 Implement a Queue with Two Stacks
var stack = require("../../../lib/dataStructures/stack.js");
 
var stackQueue = function() {
    this.popStack = new stack();
    this.pushStack = new stack();
    this.length = 0;
};
 
stackQueue.prototype.moveToPop = function() {
    while(!this.pushStack.isEmpty()) {
        this.popStack.push(this.pushStack.pop());
    }
};
 
stackQueue.prototype.moveToPush = function() {
    while(!this.popStack.isEmpty()) {
        this.pushStack.push(this.popStack.pop);
    }
};
 
stackQueue.prototype.push = function(data) {
    this.moveToPush();
 
    this.pushStack.push(data);
 
    this.length++;
};
 
stackQueue.prototype.pop = function() {
    this.moveToPop();
 
    this.length--;
 
    return this.popStack.pop();
};
 
stackQueue.prototype.isEmpty = function() {
    return this.popStack.isEmpty() && this.pushStack.isEmpty();
};
 
stackQueue.prototype.peek = function() {
    this.moveToPop();
 
    return this.popStack.peek();
};
 
module.exports = stackQueue;