当前位置:网站首页>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开始,感觉真的和数组很像!
边栏推荐
- Software architecture and design (VIII) -- distributed architecture
- Love programming
- 0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8
- Perception of life
- NTC,PT100热电阻转4-20mA温度信号转换器
- 虚拟机之NAT模式下设置静态IP
- AS如何不区分大小写去进行智能提示
- What is the concept of game testing? What are the test methods and processes?
- A failed cracking experience
- The advanced path of programmers
猜你喜欢

如何有效进行回顾会议(上)?

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

软件架构与设计(六)-----层次结构体

0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8

开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70

软件架构与设计(一)-----关键原则

Shell programming specifications and variables

软件架构与设计(十)-----架构技术

Using SYSTEMd to manage services

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
随机推荐
File and directory operations (5)
An article about rsocket protocol
比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器
samba服务器如何配置
Software architecture and design (VI) -- hierarchy
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
A tour of gRPC:05 - gRPC server straming 服务端流
PXE网络装机
Duty cycle switch output high speed pulse counter rtumodbus module ibf63
软件架构与设计(四)-----数据流架构
DNS域名解析协议
H265 streaming on OBS
Try... Except exception handling statement (6)
Data real-time feedback technology
有道云笔记去除底部广告
电压频率的变换原理
Learn RX programming from me -- concat
The advanced path of programmers
Samba Server Setup Guide
Matlab exports high-definition pictures without distortion in word compression and PDF conversion