-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5806874
commit eaf6692
Showing
11 changed files
with
385 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ListNode } from './ListNode'; | ||
|
||
export class CircularLinkedList<T> { | ||
head: ListNode<T> | null; | ||
|
||
constructor() { | ||
this.head = null; | ||
} | ||
|
||
// 在尾端新增節點並形成環狀鏈結串列 | ||
append(value: T): void { | ||
const newNode = new ListNode(value); | ||
|
||
if (this.head === null) { | ||
// 如果是第一個節點,將其設為頭節點,並指向自己 | ||
this.head = newNode; | ||
newNode.next = this.head; // 環狀結構 | ||
} else { | ||
let current = this.head; | ||
|
||
// 找到最後一個節點(指向頭節點的節點) | ||
while (current.next !== null && current.next !== this.head) { | ||
current = current.next; | ||
} | ||
|
||
current.next = newNode; | ||
newNode.next = this.head; // 使其成為環狀結構 | ||
} | ||
} | ||
|
||
// 印出環狀鏈結串列 | ||
print(): void { | ||
if (this.head === null) return; | ||
|
||
let current = this.head; | ||
const values: T[] = []; | ||
|
||
do { | ||
values.push(current.value); | ||
current = current.next as ListNode<T>; | ||
} while (current !== this.head); | ||
|
||
console.log(`${values.join(' -> ')} -> (回到頭)`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ListNode } from './ListNode'; | ||
|
||
export class LinkedList<T> { | ||
head: ListNode<T> | null; | ||
|
||
constructor() { | ||
this.head = null; | ||
} | ||
|
||
// 新增節點到鏈結串列的尾端 | ||
append(value: T): void { | ||
const newNode = new ListNode(value); | ||
|
||
if (this.head === null) { | ||
// 如果鏈結串列為空,則將新節點設為頭節點 | ||
this.head = newNode; | ||
} else { | ||
let current = this.head; | ||
|
||
while (current.next !== null) { | ||
current = current.next; | ||
} | ||
|
||
current.next = newNode; | ||
} | ||
} | ||
|
||
// 將節點插入到鏈結串列的開頭 | ||
prepend(value: T): void { | ||
const newNode = new ListNode(value); | ||
newNode.next = this.head; | ||
this.head = newNode; | ||
} | ||
|
||
// 印出鏈結串列中的所有值 | ||
print(): void { | ||
let current = this.head; | ||
const values: T[] = []; | ||
|
||
while (current !== null) { | ||
values.push(current.value); | ||
current = current.next; | ||
} | ||
|
||
console.log(values.join(' -> ')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class ListNode<T> { | ||
value: T; | ||
next: ListNode<T> | null; | ||
|
||
constructor(value: T, next: ListNode<T> | null = null) { | ||
this.value = value; | ||
this.next = next; | ||
} | ||
} |
Oops, something went wrong.