当前位置:网站首页>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;
}
边栏推荐
- Docker实现Redis Cluster(集群)模式 哈希槽分区进行亿级数据存储
- 【直播预约】数据架构演进下的新挑战——上海站
- Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
- 分体式测斜探头安装要点及注意事项
- Voice social system -- improve the audio system industry chain
- Software architecture and design (V) -- data centric architecture
- MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
- Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
- VirturalBox解决kernel driver问题
- 华为全球员工总数创新高:19.4万人,研发人员占比近50%
猜你喜欢

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

Software architecture and design (x) -- Architecture Technology

远距离串口服务器( 适配器)UART 转 1-Wire 应用

Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it

多功能混合信号AI采集/开关量DI/DO采集转RS485/232/MODBUS模块

Docker容器实现MySQL主从复制

Pyqt5 rapid development and practice 5.1 tables and trees

光学雨量计对比翻斗式雨量计的优势

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

Software architecture and design (I) -- key principles
随机推荐
有道云笔记去除底部广告
How to obtain and embed go binary execution package information
12V脉冲转速测量转24V电平信号转换变送器
Pyqt5 rapid development and practice 5.1 tables and trees
振弦采集模块测量振弦传感器的流程步骤?
【微信小程序开发(七)】订阅消息
阿里云的rds mysql 只读实例在哪里创建
Framework customization series (10) -- systemui customization status bar statusbar and navigation bar tutorial
PyQt5快速开发与实战 5.2 容器:装载更多的控件
如何快速接入统一的认证鉴权体系
Docker container implements MySQL master-slave replication
Framework customization series (I) -- systemui NavigationBar slide back to launcher on the navigation bar
Love programming
String (3)
Deutsche Telekom denied strengthening its cooperation with Huawei and said it had reduced its cooperation with Huawei in the past three years
Preparing for listing in the United States? Arm announced that it would divest the Internet of things service business: the future will focus on the underlying chip design
电压转电流/电流转电压模块
Return the two subscripts of the array according to the input target.
太阳能路灯的根本结构及作业原理
Software architecture and design (V) -- data centric architecture