Difference between “enqueue” and “dequeue”

前端 未结 6 1822
盖世英雄少女心
盖世英雄少女心 2021-01-30 13:26

Can somebody please explain the main differences? I don\'t have a clear knowledge about these functions in programming for any language.

6条回答
  •  清歌不尽
    2021-01-30 13:46

    Enqueue means to add an element, dequeue to remove an element.

    var stackInput= []; // First stack
    var stackOutput= []; // Second stack
    
    // For enqueue, just push the item into the first stack
    function enqueue(stackInput, item) {
      return stackInput.push(item);
    }
    
    function dequeue(stackInput, stackOutput) {
      // Reverse the stack such that the first element of the output stack is the
      // last element of the input stack. After that, pop the top of the output to
      // get the first element that was ever pushed into the input stack
      if (stackOutput.length <= 0) {
        while(stackInput.length > 0) {
          var elementToOutput = stackInput.pop();
          stackOutput.push(elementToOutput);
        }
      }
    
      return stackOutput.pop();
    }
    

提交回复
热议问题