Skip to content

Instantly share code, notes, and snippets.

@JackTanRoo
Created July 24, 2014 17:18
Show Gist options
  • Save JackTanRoo/1a40ae3467803d96506f to your computer and use it in GitHub Desktop.
Save JackTanRoo/1a40ae3467803d96506f to your computer and use it in GitHub Desktop.
shared - queue
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