Created
March 17, 2025 00:17
-
-
Save hbhutta/29dc0f5f6b65d1fe6eb6c2b057de993a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Dequeue() { | |
| /** | |
| Construct a dequeue of the form: | |
| B F | |
| --------------------- | |
| e/d e/d | |
| --------------------- | |
| */ | |
| private queue: number[]; | |
| constructor() { | |
| this.queue = []; | |
| } | |
| enqueueFront(v: number): void { // O(1) | |
| this.queue.push(v); | |
| } | |
| enqueueBack(v: number): void { // O(1) | |
| this.queue = [v].concat(this.queue); | |
| } | |
| dequeueFront(): number { // O(1) | |
| return this.queue.pop(); | |
| } | |
| dequeueBack(): number { // O(n) | |
| this.queue.reverse(); | |
| let d = this.queue.pop(); | |
| this.queue.reverse(); | |
| return d; | |
| } | |
| peekBack(): number { // O(1) | |
| return this.queue.at(0) | |
| } | |
| peekFront(): number { // O(1) | |
| return this.queue.at(-1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment