Heap
and Stack
are both a data structure
in which data items are arranged in order, which involves data The storage method in memory, Javascript
, as a scripting language
, naturally cannot avoid the operation of heap
, stack
.
1. Stack operation
mainly uses the two methods pop() and push(). Add/delete elements from the end of the array.
<script> let arr=[]; //入栈push(),返回值为数组长度 console.log(arr.push(1,2,3)); console.log(arr); console.log(arr.push(4)); console.log(arr); //出栈pop() console.log(arr.pop()); console.log(arr); console.log(arr.pop()); console.log(arr); </script>
2. Queue operation
mainly uses unshift() These two methods, shift(), add/delete elements at the beginning of the array.
<script> arr=[]; arr.unshift("a","b","c"); console.log(arr); //入队列 arr.unshift("d"); console.log(arr); //出队列 arr.shift(); console.log(arr); arr.shift(); console.log(arr); </script>
Recommended: "2021 js interview questions and answers (large summary)"
The above is the detailed content of Stack and queue operations in Javascript. For more information, please follow other related articles on the PHP Chinese website!