Skip to content

Instantly share code, notes, and snippets.

@RUPOJS
Last active August 29, 2015 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RUPOJS/a215d5e1fb29d02d9a08 to your computer and use it in GitHub Desktop.
Save RUPOJS/a215d5e1fb29d02d9a08 to your computer and use it in GitHub Desktop.
Simple implementation of queue data structure using linked list(kind of) in javascript
function Node(data) {
this.data = data;
this.next = null;
};
function Queue() {
this.head = null;
this.tail = null;
};
Queue.prototype.enqueue = function(data) {
var newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
};
Queue.prototype.dequeue = function() {
var newNode;
if (this.head !== null) {
newNode = this.head.data;
this.head = this.head.next;
}
return newNode;
};
Queue.prototype.print = function() {
var curr = this.head;
while (curr) {
console.log(curr.data);
curr = curr.next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment