<font color="#8A2BE2">[DS筆記]</font> Linked List 鏈結串列 === 範例零 比較不好的寫法 --- ![](https://i.imgur.com/Ju46BA8.png) <br> ![](https://i.imgur.com/wysI8yL.png) ```javascript= class Node{ constructor(val) { this.val = val; this.next = null; } } var first = new Node("Hi") first.next = new Node("there") first.next.next = new Node("how") first.next.next.next = new Node("are") first.next.next.next.next = new Node("you") console.log(first) ``` [replit連結:](https://replit.com/@bobyin22/Chuan-Tong#index.js) <br> 範例一 push --- ### 單獨push程式碼 ```javascript= push(val){ //push(傳入參數) var newNode = new Node(val) //複製一份Node類 給newNode變數 if(!this.head) { //如果 頭 空 this.head = newNode; // 頭 當 newNode變數 this.tail = this.head; // 尾 當 newNode變數 } else { //如果 頭 非空 this.tail.next = newNode; // 尾 當 newNode變數 this.tail = newNode // 尾 當 newNode變數 } this.length++ //長度+1 return this; } ``` ### push影片 {%youtube lt8dnCZE918 %} ### 完整push程式碼 ![](https://i.imgur.com/FRNeXZW.png) ```javascript= class Node { constructor(val) { //只有一個參數 this.val = val; //把參數 給目前值 this.next = null; //指向 空 } } class SinglyLinkedList { constructor() { this.head = null; //頭 空 this.tail = null; //尾 空 this.length = 0; //長 0 } push(val){ //push(傳入參數) var newNode = new Node(val) //複製一份Node類 給newNode變數 if(!this.head) { //如果 頭 空 this.head = newNode; // 頭 當 newNode變數 this.tail = this.head; // 尾 當 newNode變數 } else { //如果 頭 非空 this.tail.next = newNode; // 尾 當 newNode變數 this.tail = newNode // 尾 當 newNode變數 } this.length++ //長度+1 return this; } } var list = new SinglyLinkedList() //複製一份SinglyLinkedList類 給list變數 console.log(list) list.push("HELLO") list.push("GOODBYE") list.push("99") console.log(list) ``` [replit連結:](https://replit.com/@bobyin22/linked-list-push#index.js) <br> 範例二 pop 刪除尾 --- ### 單獨pop程式碼 ```javascript= pop() { if(!this.head) return undefined; //沒有值,不做pop動作 var current = this.head; //頭 當 current變數 var newTail = current; //current變數 當 newTail變數 while(current.next){ //當 current變數.next 還有值 newTail = current; // 頭 等於 尾 current = current.next; // 頭 等於 尾 } this.tail = newTail; this.tail.next = null; //尾.next 等於空 this.length--; if(this.length === 0) { //沒有值 this.head = null; //頭 空 this.tail = null; //尾 空 } return current; } ``` ### pop影片 {%youtube ILHaaUV6SJ8%} ### 完整pop程式碼 ![](https://i.imgur.com/MZnz94j.png) ```javascript= class Node { constructor(val) { this.val = val; this.next = null; } } class SinglyLinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } push(val){ var newNode = new Node(val) if(!this.head) { this.head = newNode; this.tail = this.head; } else { this.tail.next = newNode; this.tail = newNode } this.length++ return this; } pop() { if(!this.head) return undefined; var current = this.head; var newTail = current; while(current.next){ newTail = current; current = current.next; } this.tail = newTail; this.tail.next = null; this.length--; if(this.length === 0) { this.head = null; this.tail = null; } return current; } } var list = new SinglyLinkedList() //console.log(list) list.push("HELLO") list.push("GOODBYE") list.push("99") console.log(list) list.pop() console.log(list) ``` <br> 範例三 shift 刪除頭 --- ### 單獨shift程式碼 ```javascript= shift() { if(!this.head) return undefined; var currentHead = this.head this.head = currentHead.next this.length--; if(this.length === 0) { this.tail = null; } return currentHead console.log(this) } ``` ### shift影片 {%youtube PjT0dYf087w %} ### 完整shift程式碼 ![](https://i.imgur.com/ECQD3Kk.png) ```javascript= class Node { constructor(val) { this.val = val; this.next = null; } } class SinglyLinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } push(val){ var newNode = new Node(val) if(!this.head) { //如果沒有值 this.head = newNode; //複製一個Node類 當作 SinglyLinkedList類的head this.tail = this.head; } else { this.tail.next = newNode; this.tail = newNode } this.length++ return this; } pop() { if(!this.head) return undefined; //沒有值,不做pop動作 var current = this.head; var newTail = current; while(current.next){ newTail = current; current = current.next; } this.tail = newTail; this.tail.next = null; this.length--; if(this.length === 0) { this.head = null; this.tail = null; } return current; } shift() { if(!this.head) return undefined; var currentHead = this.head this.head = currentHead.next this.length--; if(this.length === 0) { this.tail = null; } return currentHead console.log(this) } } var list = new SinglyLinkedList() //console.log(list) list.push("HELLO") list.push("GOODBYE") list.push("99") console.log(list) //HELLO->GOODBYE->99 list.pop() console.log(list) //HELLO->GOODBYE list.shift() console.log(list) //GOODBYE list.shift() console.log(list) //null ``` <br> 範例四 unshift 加入頭 --- ### 單獨 unshift程式碼 ```javascript= ``` ### unshift影片 {%youtube K1hBCHCZC1s %} ### 完整 unshift程式碼 ```javascript= ``` <br> ###### tags: `[DS]`