Created
August 26, 2015 13:24
-
-
Save niisar/a9fe65863375a2cb0190 to your computer and use it in GitHub Desktop.
Queue Class Implementation
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 = []; | |
this.enqueue = enqueue; | |
this.dequeue = dequeue; | |
this.front = front; | |
this.back = back; | |
this.toString = toString; | |
this.empty = empty; | |
} | |
function enqueue(element){ | |
this.dataStore.push(element); | |
} | |
function dequeue(element){ | |
this.dataStore.shift(); | |
} | |
function front(){ | |
return this.dataStore[0]; | |
} | |
function back(){ | |
return this.dataStore[this.dataStore.length-1]; | |
} | |
function toString(){ | |
var retStr=""; | |
for (var i = 0; i < this.dataStore.length; ++i) { | |
retStr += this.dataStore[i] +"\n"; | |
} | |
return retStr; | |
} | |
function empty(){ | |
if(this.dataStore.length == 0){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
// test | |
// var q = new Queue(); | |
// q.enqueue("john"); | |
// q.enqueue("hill"); | |
// q.enqueue("Angela"); | |
// q.enqueue("Joshep"); | |
// console.log(q.toString()); | |
// q.dequeue(); | |
// console.log(q.toString()); | |
// console.log("First element "+ q.front()); | |
// console.log("Last element "+ q.back()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment