Home > Web Front-end > JS Tutorial > Stack and queue operations in Javascript

Stack and queue operations in Javascript

autoload
Release: 2021-04-13 09:58:37
Original
1532 people have browsed it

Stack and queue operations in Javascript

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>
Copy after login


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>
Copy after login


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!

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