当前位置:网站首页>js 链表 01
js 链表 01
2022-07-28 14:51:00 【PBitW】
链表 – 数组和链表的优缺点



链表结构封装

看这个视频之前,菜鸟以前只知道链表怎么写,每次都要看别人的代码,不够理解怎么样定义链表结构,现在感觉可以不看别人的代码自己定义出来列表的结构了。
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单向链表</title>
</head>
<body>
<script> // 封装链表类 function LinkedList(){
// 属性 this.head = null; this.length = 0; //记录长度 // 内部类:节点类 function Node(data){
this.data = data; this.next = null; } } </script>
</body>
</html>
这里比较难以想到的就是内部类,但是写了优先级列表后,就发现这个也可以类比,只要是一个节点有多个变量的时候,都可以用到这内部类!(记住类基本上就是函数)
head属性是因为每个链表都必须从头开始访问,所以这个head自然是少不了的!
length属性就是用于获得链表的长度,和数组的length属性一样,不然你今后获取链表的长度还要把链表整个的给遍历一次,很耗时间!
注意
该视频讲的主要是无头链表!
链表操作

append实现 – 尾部添加

代码
// 1 追加方法
LinkedList.prototype.append = function(data){
let newNode = new Node(data);
if(this.length == 0){
// 没有节点,就直接让head指针指向添加的元素
this.head = newNode;
}else{
// 通过变量找最后的节点
let current = this.head;
// current.next为null的时候,此时以跳出循环,current指向的就是最后一个节点而不是null
while(current.next){
current = current.next;
}
// 最后节点的next指向新节点
current.next = newNode;
}
this.length += 1;
}
toString实现

视频代码
代码
// 2 toString方法
LinkedList.prototype.toString = function(){
// 定义变量
let current = this.head;
let arr = [];
// 这里就不能是current·next了,因为要遍历全部,而不是到最后一个就退出!
while(current){
arr.push(current.data);
current = current.next;
}
return arr.join(" ");
}
insert实现 – 指定位置插入

这里添加到其它位置之所以要两个变量是因为,当你获取到了后一个节点就不能通过其获取前一个节点获取前一个节点!
所以这里菜鸟感觉还有一种思路:就是获取到前一个节点,然后将新加入的节点的next指向前一个节点的next,然后再将前一个节点的next指向新的节点,感觉挺好的,不会缺失!
代码
// 3 insert方法
LinkedList.prototype.insert = function(position,data){
// 1 对position进越界判断
if(position < 0 || position > this.length) return false;
// 2 创建节点
let newNode = new Node(data);
// 3 判断节点插入的位置是否是第一个
if(position == 0){
// 这里是this.head而不是head.next,因为head指向的就是第一个节点
// head是指针而非节点
newNode.next = this.head;
this.head = newNode;
}else{
let index = 0;
let current = this.head;
// current指向第一个节点时,previous代表前一个节点自然是null
let previous = null;
while(index++ < position){
previous = current;
current = current.next;
}
newNode.next = current;
previous.next = newNode;
}
this.length += 1;
return true;
}
// 4 insert2我的想法
LinkedList.prototype.insert2 = function(position,data){
if(position < 0 || position > this.length) return false;
let newNode = new Node(data);
if(position == 0){
newNode.next = this.head;
this.head = newNode;
}else{
let index = 0;
let current = this.head;
while(index++ < position-1){
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
this.length += 1;
return true;
}
边栏推荐
- Huawei has a record number of employees worldwide: 194000, with research and development personnel accounting for nearly 50%
- FTP file transfer protocol
- How to turn on and off flight mode through ADB
- How to compress and decompress ramdisk.img
- 为赴美上市做准备?Arm宣布将剥离物联网服务业务:未来将聚焦芯片底层设计
- Several slips of X rust, those things that have to be said
- Samba Server Setup Guide
- 活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
- monkey压力测试
- 高精度绝对角度传感器应用高速度角度监测
猜你喜欢

多用型混合信号8AI/4DI/DO转串口RS485/232MODBUS采集模块IBF30

语音社交系统——完善有声系统产业链

Communication between client and server based on rsocket protocol

Docker container implements MySQL master-slave replication

太阳能路灯的根本结构及作业原理

FTP文件传输协议

激光测距仪非接触式地表裂缝监测仪

What is the concept of game testing? What are the test methods and processes?

Multipurpose mixed signal 8ai/4di/do to serial port rs485/232modbus acquisition module ibf30

多功能混合信号AI采集/开关量DI/DO采集转RS485/232/MODBUS模块
随机推荐
Docker容器实现MySQL主从复制
一波骚操作解决Laya场景编辑器报错问题
Shell编程规范与变量
PyQt5快速开发与实战 5.2 容器:装载更多的控件
A wave of operation to solve the error problem of Laya scene editor
Docker container implements MySQL master-slave replication
Software architecture and design (IX) -- component based architecture
FTP file transfer protocol
Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier
The price war of off screen fingerprints has resumed, and second-line manufacturers are expected to win 30% of the market this year?
DNS domain name resolution protocol
有道云笔记去除底部广告
Rxdart is used instead of stateful in fluent
一次失败的破解经历
记:数值向上取整十,整百,整千,整万
Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
Communication between client and server based on rsocket protocol
激光测距仪非接触式地表裂缝监测仪
远距离串口服务器( 适配器)UART/I2C/1-Wire/SPI PS304常见问题及注意事项
Pyqt5 rapid development and practice 5.2 container: load more controls