This article mainly introduces how to implement queues and stacks in JavaScript, and analyzes the principles and related implementation skills of implementing queues and stacks in JavaScript in the form of instances, for more information about how to implement queues and stacks in JS, see the example in this article. We will share this with you for your reference. The details are as follows:
In the object-oriented programming, the queue and stack methods are generally provided. For JS, we can implement array-related operations, to implement the queue and stack functions, see the following introduction.
1. Let's take a look at their nature, which determines their usage scenarios.
Queue: a set that supports first-in-first-out (FIFO), that is, the first inserted data, first retrieved!
STACK: a collection that supports the "last-in-first-out" (LIFO) function, that is, the data that is inserted later!
2. Check the implementation code (JS Code)
Var a = new Array ();. unshift (1);. unshift (2);. unshift (3);. unshift (4); console. log ("first-in-first-out"). pop () var a = new Array ();. push (1);. push (2);. push (3);. push (4); console. log ("first-in-first-out"). pop ()
Check the running result.