当前位置:网站首页>JS linked list 02
JS linked list 02
2022-07-28 15:56:00 【PBitW】
List of articles
- get Realization -- Gets the element value at the specified location
- indexOf Realization -- Return the specified value position
- update Realization -- Modify the specified position value
- removeAt Method realization -- Delete the specified location element
- remove Realization -- Remove the specified value
- summary
get Realization – Gets the element value at the specified location
Code
// 5 get Method
LinkedList.prototype.get = function(position){
// 1 Cross border judgment -- Relevant positions should be judged to be out of bounds
if(position < 0 || position >= this.length) return null;
// 2 Get the corresponding data
let current = this.head;
let index = 0;
while(index++ < position){
current = current.next;
}
return current.data;
}
indexOf Realization – Return the specified value position
Code
// 6 indexOf Method
LinkedList.prototype.indexOf = function(data){
// 1 Defining variables
let current = this.head;
// Record the return location
let index = 0;
// 2 Start looking for
while(current){
if(current.data === data){
return index;
}
current = current.next;
index += 1;
}
// No return found -1
return -1;
}
update Realization – Modify the specified position value
Code
// 7 update Method
LinkedList.prototype.update = function(position,newData){
// 1 Judge out of bounds
if(position < 0 || position >= this.length) return false;
// 2 Find the correct node
let current = this.head;
let index = 0;
while(index++ < position){
current = current.next;
}
// 3 take position Upper data modify
current.data = newData;
return true;
}
removeAt Method realization – Delete the specified location element
Video code 
The code idea of video is clearer , Because the data of two nodes is saved , Better understanding ! My code is what I wrote and inserted last time , You can save with only one variable , But because of me There is no information about the deleted node , Therefore, the data of the node cannot be returned like the video !
My code
// 8 removeAt Method
LinkedList.prototype.removeAt = function(position){
if(position < 0 || position >= this.length) return false;
if(position === 0){
this.head = this.head.next;
}else{
let current = this.head;
let index = 0;
while(index++ < position-1){
current = current.next;
}
current.next = current.next.next;
}
// Be careful length
this.length -= 1;
return true;
}
remove Realization – Remove the specified value
Code
// 9 remove Method
LinkedList.prototype.remove = function(data){
// To obtain position
let num = this.indexOf(data);
// Delete
this.removeAt(num);
}
Here rookies don't write the rest isEmpty and size 了 , After all, there is length, These two are Pediatrics !
summary
After understanding the linked list , It is found that the operation of writing the linked list is actually relatively simple , It's worth noting that current To whom You have to make it clear ( Namely while The loop has to figure out how many times it is executed ), Basically All operations depend on current To complete the move , And you The relationship between the given position and the number of operations Also find out , Like arrays, it is from 0 Start , It feels really like an array !
边栏推荐
- 知识点qwer
- Multipurpose mixed signal 8ai/4di/do to serial port rs485/232modbus acquisition module ibf30
- A tour of gRPC:05 - gRPC server straming 服务端流
- 低成本/小体积模块RS485/232转模拟信号的原理以及应用IBF33
- An article about rsocket protocol
- PXE网络装机
- 如何快速接入统一的认证鉴权体系
- Docker container implements MySQL master-slave replication
- 记项目 常用js方法
- Software architecture and design (IV) -- data flow architecture
猜你喜欢

High speed counter to rs485modbus RTU module ibf150

12V pulse speed measurement to 24V level signal conversion transmitter

NTC,PT100热电阻转4-20mA温度信号转换器

MicTR01 Tester 开发套件(振弦采集读数仪)使用说明

Shell编程规范与变量

Set static IP in NAT mode of virtual machine

Docker implements redis cluster mode hash slot partition for 100 million level data storage

高精度绝对角度传感器应用高速度角度监测

5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95

开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70
随机推荐
Leetcode bracket validity problem
记:数值向上取整十,整百,整千,整万
Flutter中是使用RxDart代替Stateful
Docker container implements MySQL master-slave replication
js 优先级队列
High speed counter to rs485modbus RTU module ibf150
A tour of grp:05 - GRP server streaming service end stream
多用型混合信号8AI/4DI/DO转串口RS485/232MODBUS采集模块IBF30
Duty cycle switch output high speed pulse counter rtumodbus module ibf63
Rust Getting Started Guide (rustup, cargo)
Framework customization series (I) -- systemui NavigationBar slide back to launcher on the navigation bar
Shell编程规范与变量
一次失败的破解经历
Give you a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
2021 肯特面试题2
振弦采集模块测量振弦传感器的流程步骤?
5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95
多功能混合信号AI采集/开关量DI/DO采集转RS485/232/MODBUS模块
Love programming