網站首頁 編程語言 正文
鏈表結構特點
鏈表是線性表的其中一種,用于存儲有固定順序的元素。而元素之間會通過”鏈“連接在一起。
鏈表存儲的元素稱為節點。每個節點內部存在兩個值。如下:
-
this.element
:鏈表需要存儲的單個元素 -
this.next
:指向下一個節點,也就是鏈表中的”鏈“,將節點連接在一起。
嘗試手動構建鏈表結構。過程如下:
class LinkedListNode { constructor(element) { this.element = element // 鏈表要存儲的值 this.next = null // 當前節點的下一個節點 } } let A = new LinkedListNode('A') // 第一個節點A let B = new LinkedListNode('B') // 第二個節點B let C = new LinkedListNode('C') // 第三個節點C // 節點之間通過this.next屬性值連起來,如下: A.next = B // 節點A下一個是節點B B.next = C // 節點B下一個是節點C console.log(A) // 輸出鏈表:A -> B -> C
面向對象方法封裝鏈表
構造函數
基本單元:鏈表節點
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 } }
查找節點
設計一個方法,可以獲取當前節點的前中后三個節點。這樣可以極大的方便接下來的增刪操作。如下:
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 } } }
增加節點
新節點可插入的范圍:index >= 0 && index <= this.length
。也就是說,可以在每個節點的前后都可以插入新的節點。
增加節點的情況分為四種,如下分析:
- 鏈表為空,不存在節點:直接將新節點的值賦給頭節點。
- 在頭部之前插入節點(存在index參數且范圍符合):利用新節點的
next
緩存當前頭節點,然后新節點覆蓋頭節點。 - 在兩個節點之間插入節點(存在index參數且范圍符合):利用新節點的
next
緩存當前節點,改變前一個節點的next
指向新節點。 - 在尾部插入節點:遍歷到最后一個節點,在節點末尾插入節點。
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 }
刪除節點
鏈表節點的刪除范圍:index >= 0 && index < this.length
。打個比方,鏈表長度為3,那么可刪除的位置為0,1,2。
刪除節點的情況分為三種,如下分析:
- 刪除頭節點:鏈表長度為1,頭節點置空。鏈表長度大于1,頭節點的下一個節點覆蓋當前頭節點。
- 刪除中間節點(存在index參數且范圍符合):前一個節點的
next
直接指向當前節點的下一個節點。 - 刪除尾節點:找到尾節點的前一個節點,將它的
next
屬性置空。
注意:每次刪除都將改變鏈表的長度,而節點的刪除位置也會跟著改變。
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 } }
本文相關代碼已放置我的Github倉庫 ?
項目地址:Algorithmlib|LinkedList
原文鏈接:https://juejin.cn/post/7179930193383915581
相關推薦
- 2022-10-26 如何查看git分支從哪個源分支拉的_相關技巧
- 2022-03-05 centos7下搭建DNS服務器介紹_Linux
- 2022-08-17 python熱力圖實現的完整實例_python
- 2023-01-17 解讀MaxPooling1D和GlobalMaxPooling1D的區別_python
- 2022-04-10 Pytest單元測試框架生成HTML測試報告及優化的步驟_python
- 2023-07-26 vite項目中處理各種靜態資源的引入方式介紹
- 2022-10-29 .Net?Core?配置文件讀取IOptions,IOptionsMonitor,IOptionsS
- 2022-10-19 Python基礎之類的定義和使用詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支