This article describes the method of implementing queues and stacks in JS. Share it for your reference, as follows:
In object-oriented programming, methods are generally provided to implement queues and stacks. For JS, we can implement array-related operations to implement queues and stack functions. See the related introduction below.
1. Look at their properties, which determine their use situation
Queue: It is a collection that supports first-in-first-out (FIFO), that is, the data inserted first is fetched first!
Stack: It is a collection that supports last-in-first-out (LIFO), that is, the data that is inserted later is fetched first!
2. Look at the implemented code (JS code)
var a=new Array();a.unshift(1);a.unshift(2);a.unshift(3);a.unshift(4);console.log("first-in-first-out")a.pop()var a=new Array();a.push(1);a.push(2);a.push(3);a.push(4);console.log("latest-in-first-out")a.pop()Check out the running results
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.