当前位置:网站首页>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;
}
边栏推荐
- 2.855 billion yuan! Qingdao Xinen completed the capital increase: Xingcheng Jidian became the largest shareholder, holding 57.10%
- How as makes intelligent prompts regardless of case
- Learn about the native application management platform of rainbow cloud
- 2021 肯特面试题3
- DNS域名解析协议
- 多功能混合信号AI采集/开关量DI/DO采集转RS485/232/MODBUS模块
- Rust 入门指南(crate 管理)
- 比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器
- Rust Getting Started Guide (rustup, cargo)
- Minimum heap improves the efficiency of each sort
猜你喜欢

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

Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier
![[wechat applet development (VII)] subscription message](/img/aa/f0c68bc0c068ac208820df42cce3db.png)
[wechat applet development (VII)] subscription message

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

DNS domain name resolution protocol

A tour of grp:05 - GRP server streaming service end stream

js 双向链表 01

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

Software architecture and design (I) -- key principles

Pyqt5 rapid development and practice 5.1 tables and trees
随机推荐
德国电信否认加强与华为合作,并称过去3年已缩减与华为的合作
Framework customization series (I) -- systemui NavigationBar slide back to launcher on the navigation bar
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
电压转电流/电流转电压模块
Has won Huawei's 8.5 billion yuan screen order? Vicino responded: the customer asked for confidentiality and could not reply!
js 双向链表 01
高速计数器转RS485Modbus RTU模块IBF150
PyQt5快速开发与实战 5.1 表格与树
MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
js 栈
28.55亿元!青岛芯恩增资完成:兴橙集电成第一大股东,持股57.10%
一波骚操作解决Laya场景编辑器报错问题
Software architecture and design (VII) -- interactive architecture
Software architecture and design (I) -- key principles
Note: numerical accumulation animation
A tour of grp:05 - GRP server streaming service end stream
编码器高速脉冲计数器Modbus RTU模块IBF150
Zhaoqi scientific innovation and entrepreneurship competition platform, activity roadshow, investment and financing docking
光学雨量计对比翻斗式雨量计的优势
Rust 入门指南(rustup, cargo)