Created
July 24, 2014 17:18
-
-
Save JackTanRoo/1a40ae3467803d96506f to your computer and use it in GitHub Desktop.
shared - queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeQueue = function() { | |
// Hey! Rewrite in the new style. Your code will wind up looking very similar, | |
// but try not not reference your old code in writing the new style. | |
var instance = {}; | |
instance.storage = {}; | |
instance.index = 0; | |
instance.pointer = 0; | |
instance.enqueue = queueMethods.enqueue; | |
instance.dequeue = queueMethods.dequeue; | |
instance.size = queueMethods.size; | |
return instance; | |
}; | |
var queueMethods = { | |
enqueue: function(value) { | |
this.storage[this.index] = value; | |
this.index++; | |
// return this.storage; | |
}, | |
dequeue: function() { | |
if (this.size() > 0) { | |
var result = this.storage[this.pointer]; | |
this.pointer++; | |
return result; | |
}; | |
}, | |
size: function() { | |
return this.index - this.pointer; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment