Last active
October 11, 2015 21:22
-
-
Save kavitshah8/3bef8d5da0579d4608de to your computer and use it in GitHub Desktop.
Queue
This file contains hidden or 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.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