当前位置:网站首页>JS bidirectional linked list 02
JS bidirectional linked list 02
2022-07-25 10:44:00 【PBitW】
List of articles
get Realization – Get the corresponding location element
The idea is the same as that of the single linked list !
Code
// 7 get Method -- The efficiency is not high , It can be divided equally , After all, there is a tail node !
DoublyLinkedList.prototype.get = function(position){
if(position < 0 || position >= this.length) return null;
let current = this.head;
let index = 0;
while(index++ < position){
current = current.next;
}
return current.data;
}
The code shared equally here is not written by rookies , The idea is hold position and length/2 Compare , If it is larger than tail, Use less than head!
indexOf Realization – Determine whether it contains elements
This is also the same idea as the single linked list !
Code
DoublyLinkedList.prototype.indexOf = function(data){
let current = this.head;
let index = 0;
while(current){
if(current.data === data){
return index;
}
current = current.next;
index++;
}
return -1;
}
update Realization – Change the value of a position
Here rookies use the idea of equal sharing , above get Readers who don't write equally , You can learn from here !
Code
// 9 update Method
DoublyLinkedList.prototype.update = function(position,data){
if(position < 0 || position >= this.length) return false;
if(position < this.length/2){
let current = this.head;
let index = 0;
while(index++ < position){
current = current.next;
}
current.data = data;
}else{
let current2 = this.tail;
// Be careful :index be equal to length-1, because length It's length. , And the subscript is from 0 Start , therefore index The biggest can only be length-1
let index = this.length - 1;
while(index-- > position){
current2 = current2.prev;
}
current2.data = data;
}
return true;
}
removeAt Realization – Remove the element at a certain position
There are a lot of things here ,eg:
- Only one node
- The second node is removed
- What is removed is the last node
- Remove intermediate nodes
2、3 It has been , Because we need to change head、tail The direction of !
Rookies didn't consider so many situations when they wrote their own code at the beginning , Then I thought of it while watching the video , I wrote it with my own ideas and the video .
Code
// 11 removeAt1 Video method
DoublyLinkedList.prototype.removeAt1 = function(position){
if(position < 0 || position >= this.length) return false;
// Determine whether there is only one node
if(this.length === 1){
this.head = null;
this.tail = null;
}else {
if(position === 0){
// Determine whether the deleted node is the first node
this.head.next.prev = null;
this.head = this.head.next;
}else if(position === this.length - 1){
// Judge whether the last node is deleted
this.tail.prev.next = null;
this.tail = this.tail.prev;
}else{
// Divide ideas equally
if(position < this.length/2){
let current = this.head;
let index = 0;
while(index++ < position - 1){
current = current.next;
}
current.next.neaxt.prev = current;
current.next = current.next.next;
}else{
let current = this.tail;
let index = this.length - 1;
while(index-- > position+1){
current = current.prev;
}
current.prev.prev.next = current;
current.prev =current.prev.prev;
}
}
}
this.length -= 1;
return true;
}
Rookies here have different ideas from the video , What is saved is the previous or next node to be deleted , Not the deleted node itself , So the value cannot be returned , But I feel The video is better ( Save the deleted node ), Because a two-way linked list is not like a one-way linked list , You need to save the information of two nodes , Because it can access both the front node and the back node !
Video code 
remove Realization – Remove a value
Just call two methods directly here , Rookies don't write 
Other methods

summary
It's not hard to see , The difficulty of one-way linked list and two-way linked list lies in insert and removeAt Method , Just figure out these two methods Some special circumstances , In fact, it is not particularly difficult to write ; Other operations of the two-way linked list are really no different from those of the one-way linked list , Some even because With tail Pointer and simpler !
边栏推荐
- 微波技术大作业课设-分立电容电感+微带单枝短截线+微带双枝短截线
- 9.shell文本处理三剑客之awk
- Microwave technology homework course design - Discrete capacitance and inductance + microstrip single stub + microstrip double stub
- 推荐系统-协同过滤在Spark中的实现
- 如何通过开源数据库管理工具 DBeaver 连接 TDengine
- DHCP configuration (take Huawei ENSP as an example)
- 一个 DirectShow 播放问题的排查记录
- 存储、计算、分布式虚拟化篇(收集整理适合小白)
- Vscode latex workshop set xelatex compilation
- Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field
猜你喜欢

UE4 external open EXE file
![[strategic mode] like Zhugeliang's brocade bag](/img/c8/4baeabbc3674b956f6d376ab71e15a.png)
[strategic mode] like Zhugeliang's brocade bag

Pytorch tensor list is converted to tensor list of tensor to tensor using torch.stack()

AI技术栈太庞大!吴恩达给出职业生涯规划:终生学习

1. Shell programming specifications and variables
QT | mouse events and wheel events qmouseevent, qwheelevent

Kraken中事件通道原理分析

Trojang attack on neural networks paper reading notes

Introduction to onnx (open neural network exchange)

2.介绍部署LAMP平台+DISCUZ论坛
随机推荐
Voxceleb1 dataset Download
Angr (VI) -- angr_ ctf
Supervisor deployment (offline deployment requires downloading the deployment package in advance)
For cycle: daffodil case
微信小程序WxPrase中包含文件无法点击解决
Storage, computing, distributed storage (collection and sorting is suitable for Xiaobai)
Software test notes, test case design
C class library generation, use class library objects to data bind DataGridView
1.Shell编程规范与变量
Pytorch code template (CNN)
Attention is all you need paper intensive reading notes transformer
2021 致景笔试总结
2021 CEC笔试总结
AI技术栈太庞大!吴恩达给出职业生涯规划:终生学习
4、 Testfixture test fixture, or test firmware
1. Shell programming specifications and variables
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
9.shell文本处理三剑客之awk
Mysql5.7 master-slave database deployment (offline deployment)
JS hash table 02