Unlike other languages in JS, JS does not have a dedicated stack and queue type. All stacks and queues are stacks simulated by Arrays: closed at one end, the special array that can only be imported from the other end is called the first in last out of the stack. It features the last out of the stack: when to use: If you want to always use the latest elements, the stack structure is required. How to use:
1. From the end to the stackInbound stack (pressure stack): arr. push (value) outbound stack (bullet stack): var last = arr. pop () features: no influence on the position of the remaining elements, whether it is an outbound stack or an inbound Stack
2. Access the stack from the beginning,Inbound Stack: arr. unshift (value); out Stack: var first = arr. the disadvantage of shirt () is that every time the stack goes in and out, the position of other elements changes. Let's test whether push and unshift can be pushed into multiple values at a time? Can I scatter the array and press it in? The array parameters will not be split, and the push and unshift parameters will not be split. It is pressed into a suite. Then, can we press on two elements? This can be multiple values, but the full example of array parameters is not supported in the stack: (note the difference between push and unsfitf) mainly depends on the execution sequence. Queue: the array that enters from the end and comes out from the beginning. When to use first in first out: when to use elements in the order of first in, first in, and then in the queue: arr. push (); 2. output queue: var first = arr. shift ();
Stack and queue