papercats/stack.js

41 lines
590 B
JavaScript
Raw Normal View History

2017-02-22 01:34:59 -05:00
function Stack(initSize)
2017-02-22 01:34:59 -05:00
{
var len = 0;
var arr = [];
this.ensureCapacity = function(size)
{
arr.length = Math.max(arr.length, size || 0);
};
this.push = function(ele)
{
this[len] = ele;
len++;
};
2017-02-22 01:34:59 -05:00
this.pop = function()
2017-02-22 01:34:59 -05:00
{
if (len === 0)
return;
len--;
var tmp = this[len];
this[len] = undefined;
return tmp;
};
this.isEmpty = function() {
return len === 0;
2017-02-22 01:34:59 -05:00
}
this.ensureCapacity(initSize);
Object.defineProperty(this, "length", {
get: function() {return len;}
});
}
module.exports = Stack;