当前位置:网站首页>JS linked list 01
JS linked list 01
2022-07-28 15:55:00 【PBitW】
List of articles
Linked list – Advantages and disadvantages of arrays and linked lists



Linked list structure encapsulation

Before watching this video , Rookies used to only know how to write a linked list , Always look at others' code , I don't understand how to define the linked list structure , Now I feel I can define the structure of the list without looking at other people's code .
Code
<!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> One way linked list </title>
</head>
<body>
<script> // Encapsulate linked list class function LinkedList(){
// attribute this.head = null; this.length = 0; // Record length // Inner class : Node class function Node(data){
this.data = data; this.next = null; } } </script>
</body>
</html>
What is hard to think of here is the inner class , But after writing the priority list , I found that this can also be compared , As long as a node has multiple variables , Can use this internal class !( Remember that classes are basically functions )
head Attribute is because Every linked list must be accessed from the beginning , So this head Nature is indispensable !
length Attribute is used to obtain the length of the linked list , And an array of length Same property , Otherwise, you need to traverse the whole list to get the length of the list in the future , It takes a lot of time. !
Be careful
This video is mainly about headless linked lists !
List operation

append Realization – Tail add

Code
// 1 Add method
LinkedList.prototype.append = function(data){
let newNode = new Node(data);
if(this.length == 0){
// No nodes , Just let head The pointer points to the added element
this.head = newNode;
}else{
// Find the last node through variables
let current = this.head;
// current.next by null When , At this point, jump out of the loop ,current It points to the last node instead of null
while(current.next){
current = current.next;
}
// At the last node next Point to a new node
current.next = newNode;
}
this.length += 1;
}
toString Realization

Video code 
Code
// 2 toString Method
LinkedList.prototype.toString = function(){
// Defining variables
let current = this.head;
let arr = [];
// This can't be current·next 了 , Because you have to traverse all , Instead of quitting at the last !
while(current){
arr.push(current.data);
current = current.next;
}
return arr.join(" ");
}
insert Realization – Insert... At specified location

The reason for adding two variables to other locations here is because , When you get the latter node, you cannot get the previous node through it !
So there's another idea for novices here : Is to get the previous node , Then add the new node next Point to the previous node next, Then, the previous node's next Point to new node , It feels good , There's no missing !
Code
// 3 insert Method
LinkedList.prototype.insert = function(position,data){
// 1 Yes position Entering and crossing boundary judgment
if(position < 0 || position > this.length) return false;
// 2 Create nodes
let newNode = new Node(data);
// 3 Determine whether the insertion position of the node is the first
if(position == 0){
// Here is this.head instead of head.next, because head It points to the first node
// head Pointer, not node
newNode.next = this.head;
this.head = newNode;
}else{
let index = 0;
let current = this.head;
// current When pointing to the first node ,previous Representing the previous node is naturally 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 My thoughts
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;
}
边栏推荐
- Problem of fetching combinatorial numbers
- Software architecture and design (IV) -- data flow architecture
- FTP文件传输协议
- 光学雨量计对比翻斗式雨量计的优势
- [wechat applet development (VII)] subscription message
- How to obtain and embed go binary execution package information
- [channel attention mechanism] senet
- A wave of operation to solve the error problem of Laya scene editor
- 12V脉冲转速测量转24V电平信号转换变送器
- Several slips of X rust, those things that have to be said
猜你喜欢

管理区解耦架构见过吗?能帮客户搞定大难题的

2021 亚鸿笔试题2
![[channel attention mechanism] senet](/img/e6/261ca0ae5a38c26e74de27d90993f7.jpg)
[channel attention mechanism] senet

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

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

Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing

js 链表 02

DNS domain name resolution protocol

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

Docker实现Redis Cluster(集群)模式 哈希槽分区进行亿级数据存储
随机推荐
Framework customization series (VI) -- shield fallbackhome mobile phone from pop-up during startup and directly enter the launcher
Software architecture and design (x) -- Architecture Technology
热敏电阻PT100,NTC转0-10V/4-20mA转换器
[live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
PyQt5快速开发与实战 5.2 容器:装载更多的控件
PXE network installation
以太网转RS485串口计数器WiFI模块 LED灯光控制器IBF165
高速计数器转RS485Modbus RTU模块IBF150
0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8
MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
js 队列
5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95
Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing
Shell programming specifications and variables
VM501开发套件开发版单振弦式传感器采集模块岩土工程监测
2路DI高速脉冲计数器1路编码器转Modbus TCP有线无线模块IBF161
js 栈
DNS domain name resolution protocol
远距离串口服务器( 适配器)UART 转 1-Wire 应用
How to compress and decompress ramdisk.img