Topic describes

Their thinking

  1. A stack is used to simulate the enqueue operation.
  2. Another stack is used to simulate the queue operation.
  3. Add to the constructor’s prototype object add the end of the queue element function and remove the head element function. Note that neither of these functions use the arrow function because this is involved.
  4. To add a tail element to a queue, simply join the queue in the queue stack.
  5. To delete the header element, make the following decision
    • If there are elements in the queue stack, it will be directly out of the stack
    • Whether there are elements in the queue stack
      • There are: add all elements in queue stack to queue stack in reverse order, and then return the top element of queue stack.
      • None: returns -1.

AC code

function CQueue() {
    // simulate queue entry
    this.stackA = [];
    // Simulate a queue stack
    this.stackB = []; 
}

/ * * *@description: Adds elements * to the end of the queue@param {*}
 * @return {*}* /
CQueue.prototype.appendTail = function (value) {
    // Join the queue directly in the queue stack
    this.stackA.push(value);
}

CQueue.prototype.deleteHead = function() {
    // Check if there are any elements in the stack
    if (this.stackB.length ! = =0) {
        return this.stackB.pop();
    } 
    // If there are no elements in the queue, we can check if there are elements in the queue
    if (this.stackA.length ! = =0) {
        // Add elements in queue to queue in reverse order
        while(this.stackA.length ! = =0) {
            this.stackB.push(this.stackA.pop());
        }
        return this.stackB.pop();

    } else {
        return -1}}Copy the code

The title to reflect

Met this topic belongs to the second brush, this topic is to offer a topic, the second encounter no very well done, the thinking of ontology is not difficult, the difficulty of the main is I want to use the arrow function, but because the subject involves the problem of this point, this arrow function point to is to look at the runtime context, Therefore, it is hoped that in the future study and work, once the problem of this pointing to is involved, we must consider the problem of arrow function clearly.