Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Created June 4, 2014 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcanhelp/a66e195864eadb7113b8 to your computer and use it in GitHub Desktop.
Save joshcanhelp/a66e195864eadb7113b8 to your computer and use it in GitHub Desktop.
JS queue structure WTF
function Queue() {
this.dataStore = [];
}
Queue.prototype.enqueue = function (element) {
this.dataStore.push(element);
};
Queue.prototype.dequeue = function () {
return this.dataStore.shift();
};
Queue.prototype.toString = function () {
return this.dataStore.join('\n');
};
// Deque extension of Queue
function Deque() {}
Deque.prototype = new Queue();
Deque.prototype.cut = function(element) {
this.dataStore.unshift(element);
};
Deque.prototype.iKnowTheDj = function() {
return this.dataStore.pop();
};
// Testing
shittyLine.enqueue('Dippy');
shittyLine.enqueue('Sprinkles');
shittyLine.enqueue('Happy');
console.log(shittyLine.toString());
shittyLine.dequeue();
console.log(shittyLine.toString());
shittyLine.cut('Bozo');
console.log(shittyLine.toString());
shittyLine.iKnowTheDj();
console.log(shittyLine.toString());
// Why?
var testing = new Deque();
console.log('WTF');
console.log(testing.toString());
// Output:
// Bozo
// Sprinkles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment