Skip to content

Instantly share code, notes, and snippets.

@niisar
Created August 26, 2015 13:24
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 niisar/a9fe65863375a2cb0190 to your computer and use it in GitHub Desktop.
Save niisar/a9fe65863375a2cb0190 to your computer and use it in GitHub Desktop.
Queue Class Implementation
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