Created
August 4, 2019 05:57
-
-
Save negarjf/5d937b904453c851da347f6932e859f9 to your computer and use it in GitHub Desktop.
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 = 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