網(wǎng)站首頁 編程語言 正文
鏈表結(jié)構(gòu)特點
鏈表是線性表的其中一種,用于存儲有固定順序的元素。而元素之間會通過”鏈“連接在一起。
鏈表存儲的元素稱為節(jié)點。每個節(jié)點內(nèi)部存在兩個值。如下:
-
this.element
:鏈表需要存儲的單個元素 -
this.next
:指向下一個節(jié)點,也就是鏈表中的”鏈“,將節(jié)點連接在一起。
嘗試手動構(gòu)建鏈表結(jié)構(gòu)。過程如下:
class LinkedListNode { constructor(element) { this.element = element // 鏈表要存儲的值 this.next = null // 當(dāng)前節(jié)點的下一個節(jié)點 } } let A = new LinkedListNode('A') // 第一個節(jié)點A let B = new LinkedListNode('B') // 第二個節(jié)點B let C = new LinkedListNode('C') // 第三個節(jié)點C // 節(jié)點之間通過this.next屬性值連起來,如下: A.next = B // 節(jié)點A下一個是節(jié)點B B.next = C // 節(jié)點B下一個是節(jié)點C console.log(A) // 輸出鏈表:A -> B -> C
面向?qū)ο蠓椒ǚ庋b鏈表
構(gòu)造函數(shù)
基本單元:鏈表節(jié)點
class LinkedListNode { element: any next: (null | LinkedListNode) constructor(element: any) { this.element = element this.next = null } }
主體:鏈表
class LinkedList { length: number head: (null | LinkedListNode) constructor() { this.length = 0 this.head = null } }
查找節(jié)點
設(shè)計一個方法,可以獲取當(dāng)前節(jié)點的前中后三個節(jié)點。這樣可以極大的方便接下來的增刪操作。如下:
getLinkedListNode(index: number): (void | { [index: number]: (null | LinkedListNode) }) { if (this.isEmpty()) { throw new Error('LinkedList is empty') } else if (index < 0 || index >= this.length) { throw new Error(`${index} does exist in [0, ${this.length})`) } else if (index >= 0 && index < this.length) { let previous: (null | LinkedListNode) = null let current: (null | LinkedListNode) = this.head for (let i = 0; i < index; i++) { previous = current current = current!.next } return { 0: previous, 1: current, 2: current!.next } } }
增加節(jié)點
新節(jié)點可插入的范圍:index >= 0 && index <= this.length
。也就是說,可以在每個節(jié)點的前后都可以插入新的節(jié)點。
增加節(jié)點的情況分為四種,如下分析:
- 鏈表為空,不存在節(jié)點:直接將新節(jié)點的值賦給頭節(jié)點。
- 在頭部之前插入節(jié)點(存在index參數(shù)且范圍符合):利用新節(jié)點的
next
緩存當(dāng)前頭節(jié)點,然后新節(jié)點覆蓋頭節(jié)點。 - 在兩個節(jié)點之間插入節(jié)點(存在index參數(shù)且范圍符合):利用新節(jié)點的
next
緩存當(dāng)前節(jié)點,改變前一個節(jié)點的next
指向新節(jié)點。 - 在尾部插入節(jié)點:遍歷到最后一個節(jié)點,在節(jié)點末尾插入節(jié)點。
insert(element: any, index?: number): LinkedList { let node: (null | LinkedListNode) = new LinkedListNode(element) if (this.isEmpty()) { this.head = node } else if (index !== undefined && (index >= 0 && index < this.length)) { if (index === 0) { node.next = this.head this.head = node } else { let current: (void | object) = this.getLinkedListNode(index) node.next = current[0]!.next current[0]!.next = node } } else if (index !== undefined && (index < 0 || index > this.length)) { throw new Error(`${index} does exist in [0, ${this.length}]`) } else if (index === undefined || index === this.length) { let current: (null | LinkedListNode) = this.head while (current!.next !== null) { current = current!.next } current!.next = node } this.length++ return this }
刪除節(jié)點
鏈表節(jié)點的刪除范圍:index >= 0 && index < this.length
。打個比方,鏈表長度為3,那么可刪除的位置為0,1,2。
刪除節(jié)點的情況分為三種,如下分析:
- 刪除頭節(jié)點:鏈表長度為1,頭節(jié)點置空。鏈表長度大于1,頭節(jié)點的下一個節(jié)點覆蓋當(dāng)前頭節(jié)點。
- 刪除中間節(jié)點(存在index參數(shù)且范圍符合):前一個節(jié)點的
next
直接指向當(dāng)前節(jié)點的下一個節(jié)點。 - 刪除尾節(jié)點:找到尾節(jié)點的前一個節(jié)點,將它的
next
屬性置空。
注意:每次刪除都將改變鏈表的長度,而節(jié)點的刪除位置也會跟著改變。
remove(index: number): LinkedList { if (this.isEmpty()) { throw new Error('LinkedList is empty') } else { if (index === 0) { this.head = (this.length === 1) ? null : this.head!.next } else if (index > 0 && index < this.length) { let current: (void | object) = this.getLinkedListNode(index) current[0]!.next = (index + 1 === this.length) ? null : current[2] } else { throw new Error(`${index} does exist in [0, ${this.length})`) } this.length-- return this } }
本文相關(guān)代碼已放置我的Github倉庫 ??
項目地址:Algorithmlib|LinkedList
原文鏈接:https://juejin.cn/post/7179930193383915581
相關(guān)推薦
- 2022-06-07 ?分享一個Python?遇到數(shù)據(jù)庫超好用的模塊_python
- 2021-10-12 shell實現(xiàn)Fisher–Yates?shuffle洗牌算法介紹_linux shell
- 2022-02-25 C++構(gòu)造函數(shù)的初始化列表詳解_C 語言
- 2022-12-26 Python?gRPC流式通信協(xié)議詳細講解_python
- 2023-02-04 GO語言并發(fā)之好用的sync包詳解_Golang
- 2023-03-20 C#?BeginInvoke實現(xiàn)異步編程方式_C#教程
- 2022-09-29 Python模塊域名dnspython解析_python
- 2022-06-21 分享四個python接口常用封裝函數(shù)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支