Skip to content

Instantly share code, notes, and snippets.

@negarjf
Created August 4, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negarjf/5d937b904453c851da347f6932e859f9 to your computer and use it in GitHub Desktop.
Save negarjf/5d937b904453c851da347f6932e859f9 to your computer and use it in GitHub Desktop.
class Queue {
constructor(){
this.head = null;
this.tail = null;
this.size = 0;
}
get length(){
return this.size;
}
get peek(){
return this.head.data;
}
add(data){
const node = new Node(data);
if(this.tail){
this.tail.next = node;
}
this.tail = node;
if(!this.head){
this.head = node;
}
this.size++;
}
remove(){
const data = this.head.data;
this.head = this.head.next;
this.size--;
if(!this.head){
this.tail = null;
}
return data;
}
}
class Node {
constructor(data){
this.data = data;
this.next = null;
}
}
module.exports = Queue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment