Last active
August 29, 2015 14:26
-
-
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
This file contains 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 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