diff options
author | Patrick Simianer <p@simianer.de> | 2010-05-18 08:52:24 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2010-05-18 08:52:24 +0200 |
commit | b90042ebc1f37fa5f911df54ce0d3827da074892 (patch) | |
tree | 271ffd81ccf00888380b8b578d9bf238d8e4387b /javascripts/Stack.js | |
parent | ec3f1801c258dbba07dfccbd9864f9b2de0bfae6 (diff) |
major
Diffstat (limited to 'javascripts/Stack.js')
-rw-r--r-- | javascripts/Stack.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/javascripts/Stack.js b/javascripts/Stack.js new file mode 100644 index 0000000..808dea9 --- /dev/null +++ b/javascripts/Stack.js @@ -0,0 +1,48 @@ +/* + * Stack + * + */ +function Stack() { + this.a = new Array(); + this.length = 0; +} + +Stack.prototype.push = function(obj) { + this.a.push(obj); + this.length++; +} + +Stack.prototype.pop = function() { + if (this.isEmpty()) { + throw('Pop from empty stack.'); + } + this.length--; + return this.a.pop(); +} + +Stack.prototype.isEmpty = function() { + if (this.length == 0) { + return true; + } + return false; +} + +Stack.prototype.get = function(index) { + return this.a[index]; +} + +Stack.prototype.copy = function() { + var c = ((new Array()).concat(this.a)); + var ret = new Stack(); + ret.a = c; + ret.length = this.length; + return ret; +} + +Stack.prototype.str = function() { + ret = ''; + for (var i=0; i < this.length; i++) { + ret += this.a[i].id + ' '; + } + return ret; +} |