当前位置:网站首页>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;
}
边栏推荐
- How to turn on and off flight mode through ADB
- Problem of fetching combinatorial numbers
- Transformation principle of voltage and frequency
- Late 2021 year-end summary
- 分体式测斜探头安装要点及注意事项
- DNS domain name resolution protocol
- 5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95
- Software architecture and design (V) -- data centric architecture
- 软件架构与设计(七)-----互动架构
- 一波骚操作解决Laya场景编辑器报错问题
猜你喜欢

Learn about the native application management platform of rainbow cloud

Minimum heap improves the efficiency of each sort

生命的感悟

Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier

PyQt5快速开发与实战 5.1 表格与树

shell编程规范与变量

IFD-x 微型红外成像仪(模块)的温度测量和成像精度

How to effectively conduct the review meeting (Part 1)?

深部位移监测系统wk813应用边坡、大坝、堤防、铁路和建筑基坑开挖等深部位移测量

FTP文件传输协议
随机推荐
Pyqt5 rapid development and practice 5.1 tables and trees
How to configure Samba server
Zhaoqi scientific innovation and entrepreneurship competition platform, activity roadshow, investment and financing docking
记:数字累加动画
振弦采集模块测量振弦传感器的流程步骤?
记项目 常用js方法
跟我学Rx编程——Concat
Deutsche Telekom denied strengthening its cooperation with Huawei and said it had reduced its cooperation with Huawei in the past three years
Has won Huawei's 8.5 billion yuan screen order? Vicino responded: the customer asked for confidentiality and could not reply!
Shell编程规范与变量
如何压缩与解压缩ramdisk.img
Camera连拍自动化测试shell脚本
Several slips of X rust, those things that have to be said
Docker实现Redis Cluster(集群)模式 哈希槽分区进行亿级数据存储
Monkey stress test
Leetcode bracket validity problem
DNS域名解析协议
Learn RX programming from me -- concat
华为全球员工总数创新高:19.4万人,研发人员占比近50%
德国电信否认加强与华为合作,并称过去3年已缩减与华为的合作