class Stack {
constructor() {
this.arr = [];
}
peek() {
return this.arr[this.arr.length - 1]
}
push(n) {
this.arr.push(n);
}
pop() {
if (this.arr.length) {
return this.arr.pop();
}
return null;
}
print() {
console.log(this.arr);
}
}
class Queue {
constructor() {
this.main = new Stack();
this.aux = new Stack();
}
peek() {
while (this.main.peek()) {
this.aux.push(this.main.pop());
}
const e = this.aux.peek();
while (this.aux.peek()) {
this.main.push(this.aux.pop());
}
return e;
}
push(n) {
this.main.push(n);
}
dequeue() {
while (this.main.peek()) {
this.aux.push(this.main.pop());
}
const e = this.aux.pop();
while (this.aux.peek()) {
this.main.push(this.aux.pop());
}
return e;
}
}
Created
October 12, 2018 17:11
-
-
Save CaptainOfFlyingDutchman/8a9faa8669a7e03d0c7846491554fe01 to your computer and use it in GitHub Desktop.
TT Interview Solutions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment