Skip to content

Instantly share code, notes, and snippets.

@kavitshah8
Last active October 11, 2015 21:22
Show Gist options
  • Save kavitshah8/3bef8d5da0579d4608de to your computer and use it in GitHub Desktop.
Save kavitshah8/3bef8d5da0579d4608de to your computer and use it in GitHub Desktop.
Queue
function Queue () {
this.first = null;
this.last = null;
}
Queue.prototype.enqueue = function (val) {
var q1 = {
data : val,
next : null
};
if (!this.first) {
this.last = q1;
this.first = q1;
} else {
this.last.next = q1;
this.last = q1;
}
};
Queue.prototype.dequeue = function () {
if (this.first) {
var retData = this.first.data;
this.first = this.first.next;
if (!this.first) {
this.first = this.last; // Empty Queue
}
}
return retData;
};
var Q1 = new Queue();
Q1.enqueue(5);
Q1.enqueue(6);
Q1.enqueue(7);
Q1.enqueue(8);
Q1.enqueue(9);
Q1.enqueue(10);
Q1.dequeue();
console.log(Q1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment