Created
February 28, 2021 01:00
-
-
Save osha7/cbc0c2fc46bb05d5dbddc9c1566a77a4 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.data = {} | |
this.head = 0 | |
this.tail = 0 | |
} | |
enqueue(element) { | |
this.data[this.tail] = element | |
this.tail++ | |
} | |
dequeue() { | |
let removed = this.data[this.head] | |
delete this.data[this.head] | |
this.head++ | |
return removed | |
} | |
peek() { | |
return this.data[this.tail] | |
} | |
isEmpty() { | |
return ((this.head - this.tail) === 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment