Skip to content

Instantly share code, notes, and snippets.

@brianxautumn
Created April 15, 2017 21:21
Show Gist options
  • Save brianxautumn/89b1622a0da91f12cfceaae8d4aa284c to your computer and use it in GitHub Desktop.
Save brianxautumn/89b1622a0da91f12cfceaae8d4aa284c to your computer and use it in GitHub Desktop.
Queue With Single Linked List
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