Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Last active May 21, 2018 05:31
Show Gist options
  • Save trafficinc/6f5b5d27dfc62d1f3c0f9b3e3bc0312b to your computer and use it in GitHub Desktop.
Save trafficinc/6f5b5d27dfc62d1f3c0f9b3e3bc0312b to your computer and use it in GitHub Desktop.
JavaScript Queue
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() {
return 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 program
var q = new Queue();
q.enqueue("Maria");
q.enqueue("Layne");
q.enqueue("Jennifer");
//console.log(q.toString());
//q.dequeue();
console.log(q.toString());
console.log("Front of queue: " + q.front());
console.log("Back of queue: " + q.back());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment