diff options
Diffstat (limited to 'javascripts/Queue.js')
-rw-r--r-- | javascripts/Queue.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/javascripts/Queue.js b/javascripts/Queue.js new file mode 100644 index 0000000..795c433 --- /dev/null +++ b/javascripts/Queue.js @@ -0,0 +1,46 @@ +/* + * Item + * + */ + +function Item() { + var obj; + var nxt; +} + + +/* + * Queue + * + */ +function Queue() { + var topItem; + var botItem; +} + +Queue.prototype.empty = function() { + return this.topItem == null; +} + +Queue.prototype.push = function(p) { + var b = new Item(); + + if (this.empty()) { + this.topItem = b; + } else { + this.botItem.next = b; + } + this.botItem = b; + this.botItem.obj = p; +} + + +Queue.prototype.pop = function() { + if (this.empty()) { + throw('Queue empty'); + } + var b = this.topItem; + this.topItem = b.nxt; + return b.obj; +} + |