当前位置:网站首页>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));
边栏推荐
- 在OBS上进行H265推流
- 【微信小程序开发(七)】订阅消息
- How to configure Samba server
- Minimum heap improves the efficiency of each sort
- What is the concept of game testing? What are the test methods and processes?
- 远距离串口服务器( 适配器)UART/I2C/1-Wire/SPI PS304常见问题及注意事项
- DNS域名解析协议
- Have you seen the management area decoupling architecture? Can help customers solve big problems
- 玩死原型链
- Summary of for loop in JS
猜你喜欢

2021 肯特面试题3

MLX90640 红外热成像仪测温传感器模块开发笔记(八)

DNS域名解析协议

Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module

Shell programming specifications and variables

12V脉冲转速测量转24V电平信号转换变送器

Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing

How to quickly access the unified authentication system

Docker容器实现MySQL主从复制

PyQt5快速开发与实战 5.1 表格与树
随机推荐
知识点qwer
Several slips of X rust, those things that have to be said
Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
【微信小程序开发(七)】订阅消息
Shell programming specifications and variables
Leetcode bracket validity problem
Virturalbox solves the problem of kernel driver
热敏电阻PT100,NTC转0-10V/4-20mA转换器
5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95
光学雨量计对比翻斗式雨量计的优势
学习方法123
How to build opengrok code server
Perception of life
编码器高速脉冲计数器Modbus RTU模块IBF150
电压频率的变换原理
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
Software architecture and design (I) -- key principles
Voice social system -- improve the audio system industry chain
Rxdart is used instead of stateful in fluent
How to obtain and embed go binary execution package information