当前位置:网站首页>js 链表 02
js 链表 02
2022-07-28 14:51:00 【PBitW】
文章目录
get实现 – 获取指定位置元素值
代码
// 5 get方法
LinkedList.prototype.get = function(position){
// 1 越界判断 -- 有关位置都要判断越界
if(position < 0 || position >= this.length) return null;
// 2 获取对应的数据
let current = this.head;
let index = 0;
while(index++ < position){
current = current.next;
}
return current.data;
}
indexOf实现 – 返回指定值位置
代码
// 6 indexOf方法
LinkedList.prototype.indexOf = function(data){
// 1 定义变量
let current = this.head;
// 记录返回位置
let index = 0;
// 2 开始查找
while(current){
if(current.data === data){
return index;
}
current = current.next;
index += 1;
}
// 没有找到返回-1
return -1;
}
update实现 – 修改指定位置值
代码
// 7 update方法
LinkedList.prototype.update = function(position,newData){
// 1 判断越界
if(position < 0 || position >= this.length) return false;
// 2 查找正确的节点
let current = this.head;
let index = 0;
while(index++ < position){
current = current.next;
}
// 3 将position上的data修改
current.data = newData;
return true;
}
removeAt方法实现 – 删除指定位置元素
视频代码
视频的代码思路更加清晰,因为保存了两个节点的数据,更好理解!我的代码就是我自己上次写插入的思路,可以只用一个变量保存,但是因为我这个没有保存删除的节点的信息,所以无法像视频一样返回节点的数据!
我的代码
// 8 removeAt方法
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;
}
// 注意length
this.length -= 1;
return true;
}
remove实现 – 移除指定值
代码
// 9 remove方法
LinkedList.prototype.remove = function(data){
// 获取位置
let num = this.indexOf(data);
// 删除
this.removeAt(num);
}
这里菜鸟就不写剩下的isEmpty和size了,毕竟有了length,这两个就是小儿科!
总结
理解了链表之后,发现写链表的操作其实也还算比较简单,值得注意的就是current指向的谁你得搞得很清楚(就是while循环得搞清楚执行了多少次),基本上所有的操作就是靠current的移动来完成的,还有你给定的位置和操作次数的关系也要搞清楚,其和数组一样是从0开始,感觉真的和数组很像!
边栏推荐
- [wechat applet development (VII)] subscription message
- 突发!微星CEO江胜昌坠楼身亡
- Several slips of X rust, those things that have to be said
- 占空比开关量输出高速脉冲计数器RTUModbus模块IBF63
- Rust 入门指南(rustup, cargo)
- H265 streaming on OBS
- 已拿下华为85亿元屏幕订单?维信诺回应:客户要求保密,无法回复!
- Problem of fetching combinatorial numbers
- 使用systemd管理服务
- Rxdart is used instead of stateful in fluent
猜你喜欢

5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95

How to obtain and embed go binary execution package information

Docker container implements MySQL master-slave replication

Software architecture and design (VI) -- hierarchy

太阳能路灯的根本结构及作业原理
What are the process specifications of software testing? How to do it?

Open light input / relay output rs485/232 remote data acquisition IO module ibf70

FTP file transfer protocol

Shell programming specifications and variables

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
随机推荐
[live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
0-75mv/0-100mv to rs485/232 communication interface Modbus RTU acquisition module ibf8
Huawei has a record number of employees worldwide: 194000, with research and development personnel accounting for nearly 50%
学习方法123
热敏电阻PT100,NTC转0-10V/4-20mA转换器
生命的感悟
Software architecture and design (IX) -- component based architecture
软件架构与设计(一)-----关键原则
1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151
samba服务器如何配置
Youdao cloud notes remove the bottom advertisement
How to configure Samba server
Set static IP in NAT mode of virtual machine
MySQL add and delete indexes
Zhaoqi scientific innovation and entrepreneurship competition platform, activity roadshow, investment and financing docking
Framework customization series (I) -- systemui NavigationBar slide back to launcher on the navigation bar
Learn about the native application management platform of rainbow cloud
Docker实现Redis Cluster(集群)模式 哈希槽分区进行亿级数据存储
Framework定制系列(十)-----SystemUI定制状态栏statusbar和导航栏navigationbar教程
Open light input / relay output rs485/232 remote data acquisition IO module ibf70