Home > Web Front-end > JS Tutorial > How to implement two queues to represent a stack in JS

How to implement two queues to represent a stack in JS

亚连
Release: 2018-06-20 18:10:58
Original
1424 people have browsed it

This article mainly introduces the JS method of using two queues to represent a stack, briefly analyzes the principle of using two queues to represent a stack, and analyzes javascript related operation skills with specific examples. Friends in need can refer to it. Next

The example in this article describes the method of using JS to represent a stack using two queues. Share it with everyone for your reference, the details are as follows:

Look at the schematic diagram first:

Clear your ideas before writing:

<!DOCTYPE html>
<html>
<head>
  <title>2 Queue</title>
  <meta charset="utf-8"/>
  <script type="text/javascript">
  var arr1 = [];
  var arr2 = [];
    function Queue(arr){
      this.push = function(element){
        return arr.push(element);
      }
      this.pop = function(){
        return arr.shift();
      }
      this.size = function(){
        return arr.length;
      }
      this.display = function(){
        console.log(arr);
      }
    }
    var queue1 = new Queue(arr1);
    queue1.push(1);
    queue1.push(4);
    queue1.push(5);
    queue1.push(7);
    queue1.display();
    var queue2 = new Queue(arr2);
    //利用两个队列实现栈。放在数组中打印
    var res = [], k = 0;
    while(queue1.size()!=0){
      var len = queue1.size();
      for(var i = 0; i<len-1; i++){
        queue2.push(queue1.pop());
      }
      // 打印queue1最后一个元素,并出队
      res[k] = queue1.pop();
      ++k;
      // 队列2的元素放置到队列1中
      for(var i = 0; i<len-1; i++){
        queue1.push(queue2.pop());
      }
    }
    console.log("res:" + res);
    //利用两个队列实现栈。单个弹出
    while(queue1.size()!=0){
      var len = queue1.size();
      for(var i = 0; i<len-1; i++){
        queue2.push(queue1.pop());
      }
      // 打印queue1最后一个元素,并出队
      console.log(queue1.pop());
      // 队列2的元素放置到队列1中
      for(var i = 0; i<len-1; i++){
        queue1.push(queue2.pop());
      }
    }
  </script>
</head>
<body>
</body>
</html>
Copy after login

Run results:

#The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to write Form components using async-validator (detailed tutorial)

How to use WeChat applet to upload images Function

How to use vue to achieve CSS transition effect

How to implement the upload and compress image function in js (detailed tutorial)

The above is the detailed content of How to implement two queues to represent a stack in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template