Created
April 15, 2017 21:21
-
-
Save brianxautumn/89b1622a0da91f12cfceaae8d4aa284c to your computer and use it in GitHub Desktop.
Queue With Single Linked List
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
class Queue{ | |
constructor(){ | |
this.head; | |
this.tail; | |
} | |
enqueue(data){ | |
var node = new Node(data); | |
if(!this.head){ | |
this.head = node; | |
this.tail = node; | |
}else{ | |
this.head.next = node; | |
this.head = node; | |
} | |
} | |
dequeue(){ | |
if(!this.tail) | |
return null; | |
var node = this.tail; | |
if(this.head === this.tail){ | |
this.head = null; | |
this.tail = null; | |
}else{ | |
this.tail = this.tail.next; | |
} | |
return node.data; | |
} | |
} | |
class Node{ | |
constructor(data){ | |
this.data = data; | |
this.next = null; | |
} | |
} | |
var queue = new Queue(); | |
queue.enqueue(1); | |
queue.enqueue(2); | |
queue.enqueue(3); | |
console.log(queue.dequeue()); | |
console.log(queue.dequeue()); | |
console.log(queue.dequeue()); | |
console.log(queue.dequeue()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment