当前位置:网站首页>JS stack
JS stack
2022-07-28 15:56:00 【PBitW】
List of articles
Stack
Stack is also a very common data structure , And it is widely used in programs , Stacks and queues are common Restricted linear structure .
The stack structure

Stack application – Function stack

So if a function keeps recursing , Then the function will always be added to the function call stack , Then because it will never be finished , No function popup , There will be stack overflow !
Function stack diagram

The realization of the stack – Array mode

Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Stack encapsulation </title>
</head>
<body>
<script> // Encapsulate stack classes function Stack(){
// Properties in stack this.items = []; // Stack related operations // 1. Push elements onto the stack // It is equivalent to adding methods to each object instance ( Don't write like this ) // this.push = function(element){
// this.item.push(element); // } // It is equivalent to giving the whole object ( class ) Added methods Stack.prototype.push = function(element){
this.items.push(element); } // 2. Take a look at the top of the stack element Stack.prototype.peak = function(){
return this.items[this.items.length -1]; } // 3. Take the elements out of the stack Stack.prototype.pop = function(){
return this.items.pop(); } // 4. Judge whether it is an empty stack Stack.prototype.isEmpty = function(){
return this.items.length == 0; } // 5. Get the number of elements in the stack Stack.prototype.size = function(){
return this.items.length; } // 6.toString Method Stack.prototype.toString = function(){
return this.items.join(""); } } // Use of stack let s = new Stack(); console.log(s.isEmpty()); s.push(2); s.push(5); s.push(8); s.push(10); console.log(s); console.log(s.isEmpty()); console.log(s.size()); console.log(s.toString()); s.pop(); console.log(s); console.log(s.size()); console.log(s.toString()); </script>
</body>
</html>
Details
There is a problem when printing here , The first answer printed out only 258
The problem lies in the result of manual expansion in the first step . We have always understood that ,console.log A snapshot of the current output result will be output , This understanding is indeed correct , But for performance reasons , When the browser outputs the results , For more complex data structures , These data will not be expanded , Until you manually poke the data , To get the value inside , But obviously , This is the time , The data has been followed by us for The operation in the loop has been modified , This creates a kind of “ asynchronous ” The feeling of , As a matter of fact , The whole operation is actually synchronous .
thank : Synchronous or asynchronous ?console.log Analysis of strange phenomena
The application of the stack – Decimal to binary

The calculation process of converting decimal system to binary system :
The calculation process is from top to bottom , But the number of conversions is from bottom to top , Isn't this the first in and last out feature of stack ?
Code
function dec2bin(decNum){
let stack1 = new Stack();
while(decNum > 0){
//decNum be equal to 0 It's about to end
stack1.push(decNum % 2);
decNum = Math.floor(decNum / 2);
}
let binString = "";
while(!stack1.isEmpty()){
// Because I don't know stack1 The length of , So according to whether pop It's empty
binString += stack1.pop();
}
return binString;
}
console.log(dec2bin(100));
边栏推荐
- 低成本/小体积模块RS485/232转模拟信号的原理以及应用IBF33
- 2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
- 2.855 billion yuan! Qingdao Xinen completed the capital increase: Xingcheng Jidian became the largest shareholder, holding 57.10%
- Give you a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.
- 2021 亚鸿笔试题2
- Docker implements redis cluster mode hash slot partition for 100 million level data storage
- Deutsche Telekom denied strengthening its cooperation with Huawei and said it had reduced its cooperation with Huawei in the past three years
- Ethernet to RS485 serial port counter WiFi module LED light controller ibf165
- Transformation principle of voltage and frequency
- js 数组(总结)
猜你喜欢

Rust 入门指南(crate 管理)

Open light input / relay output rs485/232 remote data acquisition IO module ibf70

多用型混合信号8AI/4DI/DO转串口RS485/232MODBUS采集模块IBF30

Docker容器实现MySQL主从复制

编码器高速脉冲计数器Modbus RTU模块IBF150

多功能混合信号AI采集/开关量DI/DO采集转RS485/232/MODBUS模块

0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8

What is the concept of game testing? What are the test methods and processes?

0-75mv/0-100mv to rs485/232 communication interface Modbus RTU acquisition module ibf8

Shell programming specifications and variables
随机推荐
Camera continuous shooting automatic test shell script
电压转电流/电流转电压模块
PXE网络装机
学习方法123
兆骑科创创业大赛平台,活动路演,投融资对接
屏下指纹价格战再起,二线厂商今年有望拿下30%市场?
开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70
Where is the RDS MySQL read-only instance of Alibaba cloud created
[wechat applet development (VII)] subscription message
Open light input / relay output rs485/232 remote data acquisition IO module ibf70
How to compress and decompress ramdisk.img
Voltage to current / current to voltage module
虚拟机之NAT模式下设置静态IP
如何快速接入统一的认证鉴权体系
Virturalbox solves the problem of kernel driver
深部位移监测系统wk813应用边坡、大坝、堤防、铁路和建筑基坑开挖等深部位移测量
Minimum heap improves the efficiency of each sort
Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
Problem of fetching combinatorial numbers
Preparing for listing in the United States? Arm announced that it would divest the Internet of things service business: the future will focus on the underlying chip design