When it comes to data structures, there’s very little to do with the front end, like lists. There’s no front end, but you can do almost anything with lists through arrays and JavaScript’s built-in apis. That said, learning data structures well is certainly beneficial for our future development. You can even see that a lot of everyday operations are actually mapping data structures.
Let me introduce you to eight common data structures.
The data structure
Data structure: The way a computer stores and organizes data, like POTS and pans.
The stack
- a
Last in, first out
Data structure.
This is like when we put briquettes, put them out first. And the process of putting is pushing, which corresponds to the JavaScript push operation. The corresponding take out is out of the stack operation. Corresponds to the pop. The final place to put the briquettes is the corresponding top of the stack.
-
In JavaScript, there is no stack, but you can use Array to implement all the functions of a stack.
-
Application scenarios, such as a function’s call stack.
function fun1() {
// 1 some codes
fun2()
// 2 some codes
}
function fun2() {
return "Hello Word"
}
fun1()
Copy the code
The above code is a stack call process that calls fun1, then fun2, and fun1 is not finished until fun2 is finished.
The function that is called last is executed first.
- Common stack operations:
Push, pop, stack[stack.length-1]
The queue
- A first-in, first-out data structure.
-
There is also no queue in JavaScript, but you can use Array to implement all the functions of a stack.
-
Application scenarios, such as task queues in JS asynchrony.
-
JS is single threaded and cannot simultaneously handle concurrent tasks in asynchrony.
-
Use the task queue method to process asynchronous tasks sequentially.
- Common stack operations:
Push, shift, queue[0]
The End!