Implement a Stack by using Queue
2022年5月1日大约 1 分钟
Implement a Stack by using Queue
Question
This is reversed problem of 13. Implement a Queue by using Stack
In JavaScript, we could use array to work as both a Stack or a queue.
const arr = [1, 2, 3, 4]
arr.push(5) // now array is [1, 2, 3, 4, 5]
arr.pop() // 5, now the array is [1, 2, 3, 4]
Above code is a Stack, while below is a Queue
const arr = [1, 2, 3, 4]
arr.push(5) // now the array is [1, 2, 3, 4, 5]
arr.shift() // 1, now the array is [2, 3, 4, 5]
now suppose you have a Queue, which has only follow interface:
class Queue {
enqueue(element) { /* add element to queue, similar to Array.prototype.push */ }
peek() { /* get the head element*/ }
dequeue() { /* remove the head element, similar to Array.prototype.pop */ }
size() { /* count of elements */ }
}
Could you implement a Stack by using only above Queue? A Stack must have following interface
class Stack {
push(element) { /* add element to stack */ }
peek() { /* get the top element */ }
pop() { /* remove the top element */}
size() { /* count of elements */}
}
note
you can only use Queue as provided. Don't use Array, it is not what this is for.
Code
/* you can use this Queue which is bundled together with your code
class Queue {
enqueue(element) {
// add new element to the queue
}
peek() {
// return the head element
}
dequeue() {
// remove head element from the queue
}
size() {
// return the queue size
}
}
*/
// you need to complete the following Stack, using only Queue
class Stack {
push(element) {
// push an element into the stack
}
peek() {
// get the top element
}
pop() {
// remove top element from stack
}
size() {
// return count of elements
}
}