当前位置:网站首页>[JS] - [array, stack, queue, linked list basics] - Notes
[JS] - [array, stack, queue, linked list basics] - Notes
2022-06-24 23:17:00 【Interesting learning】
【js】-【 Array 】- note
Statement : This note is based on the Nuggets brochure , If you want to learn more details , Please move https://juejin.cn/book/6844733800300150797
1 Array
1.1 One dimensional creation
const arr = []
const arr = new Array()
Specify the length
const arr = new Array(7)
Create a length determination 、 At the same time, the value of each element is also determined by the array
const arr = (new Array(7)).fill(1)
1.2 One dimensional traversal
forloop
// Gets the length of the array
const len = arr.length
for(let i=0;i<len;i++) {
// Output the element value of the array , Output the current index
console.log(arr[i], i)
}
forEachMethod
arr.forEach((item, index)=> {
// Output the element value of the array , Output the current index
console.log(item, index)
})
mapMethod
On the basis of traversal “ Reprocessing ”
const newArr = arr.map((item, index)=> {
// Output the element value of the array , Output the current index
console.log(item, index)
// Add... To the current element value 1
return item+1
})
map To return a new array , The value of each element in the array is based on its existing element value +1 After the results of the
1.3 Two dimensional array
Initialize a two-dimensional array
const len = arr.length
for(let i=0;i<len;i++) {
// Initialize each pit of the array as an array
arr[i] = []
}
1.4 Two dimensional array traversal
# Cache the length of the external array
const outerLen = arr.length
for(let i=0;i<outerLen;i++) {
# The length of the internal array of the cache
const innerLen = arr[i].length
for(let j=0;j<innerLen;j++) {
# The value of the output array , The index of the output array `const arr = [1,2]
arr.push(3) // [1,2,3]`
console.log(arr[i][j],i,j)
}
}
2 Array addition and deletion methods
2.1 Add element to array
unshiftMethod - Add elements to the head of the array
const arr = [1,2]
arr.unshift(0) # [0,1,2]
pushMethod - Add elements to the end of the array
const arr = [1,2]
arr.push(3) # [1,2,3]
spliceMethod - Add elements anywhere in the array
const arr = [1,2]
arr.splice(1,0,3) # [1,3,2]
The first input parameter is the starting index value , The second input parameter indicates the number of elements to be deleted from the starting index , The third parameter is the incoming value
2.2 Delete elements from array
- shift Method - Delete the elements at the head of the array
const arr = [1,2,3]
arr.shift() # [2,3]
- pop Method - Delete the elements at the end of the array
const arr = [1,2,3]
arr.pop() # [1,2]
- splice Method - Delete elements anywhere in the array
const arr = [1,2,3]
arr.splice(1,1) # [1, 3]
3 Stack 、 queue
The implementation of stack and queue generally depends on array
The difference between the two is , Their respective pairs of arrays Add or delete The operation has different restrictions .
3.1 Stack
Only adding elements from the tail is allowed
Only elements are allowed to be taken from the tail
// The initial state , The stack is empty
const stack = []
// Stacking process
stack.push(' Northeast big board ')
stack.push(' Cornetto ')
stack.push(' Chilez ')
stack.push(' Ice factory ')
stack.push(' Bright milk brick ')
// Stack out process , Execute only when the stack is not empty
while(stack.length) {
// Simply access the top element of the stack ( Don't stack )
const top = stack[stack.length-1]
console.log(' Now the ice cream is ', top)
// Push the top element out of the stack
stack.pop()
}
// The stack is empty
stack // []
3.2 queue
Only push and shift Complete the addition and deletion of “ Array ”
const queue = []
queue.push(' Little book one sister ')
queue.push(' Little book second sister ')
queue.push(' Little book third sister ')
while(queue.length) {
// Simply access the team head element ( Not out of the team )
const top = queue[0]
console.log(top,' Take the meal ')
// Remove the team head element from the team
queue.shift()
}
// Team space
queue // []
4 Linked list
In the list , The name of the data unit is “ node ”, And the distribution of nodes and nodes , Can be discrete in memory .
To access any element in the linked list , We all have to start from the starting point , One by one next, Access to the target node . To ensure that the starting point is reachable , We sometimes set a head Pointer to point to the starting position of the linked list :
- Create linked list node , You need a constructor :
function ListNode(val) {
this.val = val;
this.next = null;
}
- Use the constructor to create a node
const node = new ListNode(1)
node.next = new ListNode(2)
Data field value is 1,next The node data field value is 2
3. Insert a new node between any two nodes
What we need to change is Precursor node and Target node Of next Pointer to
# If the target node does not exist , So remember to create it manually
const node3 = new ListNode(3)
# hold node3 Of next Pointer to node2( namely node1.next)
node3.next = node1.next
# hold node1 Of next Pointer to node3
node1.next = node3
- Deletion of linked list elements
Directly let its precursor node node1 Of next The pointer jumps over it 、 Point to node3 In the subsequent
node1.next = node3.next
You can use only one pointer , This pointer is used to locate the precursor node of the target node .
// utilize node1 You can go to node3
const target = node1.next
node1.next = target.next
Relative to arrays , Linked lists have an obvious advantage , Adding and deleting elements does not require moving redundant elements
In the list , Just make it clear that you want to insert / Delete target location , The complexity of the add / delete operation is constant , Expressed as O(1).
however Linked list There is also a drawback : When we try to read a particular ( The first i individual ) The linked list node of , You must traverse the entire linked list to find it , Expressed as O(n).
But in Array in , We access the index directly 、 You can do it in one step , The complexity of this operation will be downgraded to a constant level O(1)
Insertion of linked list / Delete more efficiently , And the access efficiency is low ; The access efficiency of array is high , The insertion efficiency is low
边栏推荐
- websocket学习
- golang convert json string to map
- Mycms we media CMS V3.0, resource push optimization, new free template
- 15 lines of code using mathematical formulas in wangeditor V5
- Selection (026) - what is the output of the following code?
- Use of laravel verifier
- Concurrent shared model management
- 二分查找数组下标
- 研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
- SimpleDateFormat 格式化和解析日期的具体类
猜你喜欢

03_SpingBoot 核心配置文件

Docker installation MySQL simple without pit

Blogs personal blog project details (servlet implementation)

案例解析:用「度量」提升企业研发效能|ONES Talk

Dynamic menu, auto align

【js】-【栈、队-应用】-学习笔记
![[JS] - [array, Stack, queue, Link List basis] - Notes](/img/c6/a1bd3b8ef6476d7d549abcb442949a.png)
[JS] - [array, Stack, queue, Link List basis] - Notes

canvas 实现图片新增水印

Tech talk activity review kubernetes skills of cloud native Devops

Attention, postgraduate candidates! They are the easiest scams to get caught during the preparation period?!
随机推荐
InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
Getting started with the go Cobra command line tool
MySQL kills 10 people. How many questions can you hold on to?
jar中没有主清单属性
【js】-【數組、棧、隊列、鏈錶基礎】-筆記
Uncover the secrets of Huawei cloud enterprise redis issue 16: acid'true' transactions beyond open source redis
Laravel add helper file
慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition
Design and implementation of spark offline development framework
Notes for laravel model
laravel 创建 service层
How to submit the shopee opening and settlement flow?
冒泡排序
Listen to the markdown file and hot update next JS page
对抗训练理论分析:自适应步长快速对抗训练
07_ Springboot for restful style
Selection (026) - what is the output of the following code?
研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
01_SpingBoot 框架入门

