Skip to content

Instantly share code, notes, and snippets.

@jasterix
Last active August 18, 2020 12:03
Show Gist options
  • Save jasterix/6a197b95f7e2ed74051892390db0d92c to your computer and use it in GitHub Desktop.
Save jasterix/6a197b95f7e2ed74051892390db0d92c to your computer and use it in GitHub Desktop.
Queue - Implementing a queue using a singly linked list
class Node {
constructor(value){
this.value = value;
this.next = null;
}
}
class Queue {
constructor(){
this.first = null;
this.last = null;
this.size = 0;
}
enqueue(val){
var newNode = new Node(val);
if(!this.first){
this.first = newNode;
this.last = newNode;
} else {
this.last.next = newNode;
this.last = newNode;
}
return ++this.size;
}
dequeue(){
if(!this.first) return null;
var temp = this.first;
if(this.first === this.last) {
this.last = null;
}
this.first = this.first.next;
this.size--;
return temp.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment