Skip to content

Instantly share code, notes, and snippets.

@d-rat
Created March 24, 2022 07:32
Show Gist options
  • Save d-rat/0da1cd8ba6c174e7f725873fdf1a2efa to your computer and use it in GitHub Desktop.
Save d-rat/0da1cd8ba6c174e7f725873fdf1a2efa to your computer and use it in GitHub Desktop.
class QueueStack {
constructor() {
this.q = [];
}
push(val) {
let size = this.q.length;
this.q.push(val);
while (size--) {
this.q.push(this.q.shift());
}
}
pop() {
if (!this.q.length) return null;
return this.q.shift();
}
peek() {
if (!this.q.length) return null;
return this.q[0];
}
isEmpty() {
return this.q.length === 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment