tags: Data structure and algorithm JS description JS basics JS data structure stack JS data structure queue Algorithm in JS
The original intention of writing this series of blogs is to help myself and beginners, lay a higher level of programming foundation and philosophy, so that everyone can have a deeper understanding of JS, and use JS to understand advanced programming ideas.
All demonstrations of algorithms for a certain data structure are not standard answers. It’s just that I have imitated various materials and other mature languages based on experience. If there are any imprecise writings, or better writing, please contact me , I will correct it.
I wanted to implement these data structures in the form of constructors, but since the technology is being updated, it may be better to use the new syntax. So all code implementations are ES6+ code.
In the process of front-end development, we rarely encounter a batch of data that needs to be managed by the data structure of the stack, so the concept of the stack is relatively weak. In fact, there is a very vivid example of the stack, which is recursion. The calling and execution of functions are all in the stack, and closures are also related to the stack.
The stack plays a pivotal role in the operation of the program. The most important thing is that the stack saves the maintenance information needed for a function call, which is often called a stack frame or activity record. The stack frame generally contains two important pieces of information,The return address and parameters of the function and temporary variables: including non-static local variables of the function and other temporary variables automatically generated by the compiler.
Here I found a very specific example of the JS function execution stack, thanks to the author for sharing, I sincerely suggest that everyone take a look, the author writes very well.

Compared with the stack, we may be more familiar with the data structure of the queue, but it is not commonly used. Here is a queue-related example, which is the traditional snake game. Anyone who has written this game should know. The basic logic of the snake moving on the background is to add new elements to the head and delete old elements from the tail.

In fact, after the concept is clear, the code implementation is very simple. However, due to the characteristics of the JS language, if the code we write is executed in accordance with our regulations, it can reflect this conceptuality, but if it is operated at will, the conceptuality is not so strong, just like a class. Abstract classgetter with setter same.
class Stack {
top = 0; // The position of the top of the stack
data = []; // data source
push(el) {
this.data[this.top] = el;
this.top++;
// Shorthand: this.data[this.top++] = el;
}
pop() {
const lastEl = this.data[this.top - 1];
this.data[--this.top] = null;
return lastEl;
}
peek() {
return this.data[this.top - 1];
}
clear() {
this.data = [];
this.top = 0;
}
}
class Queue {
front = 0; // The position of the head of the team
end = 1; // position at the end of the team
data = [45, 67, 89, 90]; // data source
push(el) {
this.data[this.end++] = el;
}
shift() {
const firstEl = this.data[this.front];
this.data[this.front++] = null;
return firstEl;
}
first() {
return this.data[this.front];
}
last() {
return this.data[this.end - 1];
}
clear() {
this.data = [];
this.front = 0;
this.end = 1;
}
}
The code is too simple, so I did not write comments, but it is important that we use these codes to understand the data structure of stacks and queues.

Links to other articles:
Analysis and code implementation of common JS algorithms
Vue uses addRoutes to dynamically create routes, and retain the complete demo of dynamic routing after the page is refreshed
React scaffolding (5) Props and PropTypes check type
Vue data two-way response mechanism
Vue-cli request proxy configuration, package Axios and request interceptor and response interceptor
Vue-cli + Element-UI uses recursion to dynamically generate a menu bar
Stack is an ordered collection of follow-up advances (LIFO) principles. The newly added or deleted elements are stored in the end of the stack, called the top, and the other end is called the bottom o...
Data structure (1) Note: This article is based on the Bilibili video [JavaScript data structure and algorithm] 1. Basic concepts: How the data is organized 2. Examples in life: Placement of books, exp...
Stacks and queues are easier to understand in data structure and algorithm learning. But sometimes I feel that I have mastered the stack and the queue, but when I encounter different situations when I...
A linear stacks and queues are two special table data structures, among them is a linear relationship is the predecessor successor relationship. A stack 1 Overview Stack only allow linear table insert...
Stack The stack is a linear table limited to insert and delete operations only at the end of the table The end that allows insertion and deletion is called the top of the stack, and the other end is c...
1. the stack The stack is a linear structure The stack is a last in first out data structure (LIFO) Stack<>: press down Return value type method Features void push(E) Push the element to the top...
Stack concept The stack (stack), in some places called the stack, is a container that can store data elements, access elements, and delete elements. Its characteristic is that it can only be allowed a...
Following the last time the linear "operation of sex table" operation of sex table, this time I will bring a little abstract "stack and queue", first of all, let me talk about the ...
One, the stack Stack is limitedInsert and delete only at the end of the tableofLinear table. The one end that allows insertion and deletion is called the top of the stack, the other end is called the ...
Data structure and algorithm-stack and queue Stack:It is a linear table limited to insert and delete operations at one end of the table. Stack is also known as LIFO table for short queue:It is also a ...