How to implement two queues to represent a stack in JS
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

The difference between Java heap and stack: 1. Memory allocation and management; 2. Storage content; 3. Thread execution and life cycle; 4. Performance impact. Detailed introduction: 1. Memory allocation and management. The Java heap is a dynamically allocated memory area, mainly used to store object instances. In Java, objects are allocated through heap memory. When an object is created, the Java virtual machine Allocate corresponding memory space on the system, and automatically perform garbage collection and memory management. The size of the heap can be dynamically adjusted at runtime, configured through JVM parameters, etc.

js methods to refresh the current page: 1. location.reload(); 2. location.href; 3. location.assign(); 4. window.location. Detailed introduction: 1. location.reload(), use the location.reload() method to reload the current page; 2. location.href, you can refresh the current page by setting the location.href attribute, etc.
