Created
June 4, 2014 18:47
-
-
Save joshcanhelp/a66e195864eadb7113b8 to your computer and use it in GitHub Desktop.
JS queue structure WTF
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
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