-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
334 additions
and
169 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { defaultEquals } from '../../utils'; | ||
import { LinkedNode } from './LinkedNode'; | ||
import { type LinkedListInterface } from './type'; | ||
|
||
export default class LinkedList<T> implements LinkedListInterface<T> { | ||
count: number; | ||
head: null | LinkedNode<T>; | ||
eqFn: (a: T, b: T) => boolean; | ||
constructor(eq = defaultEquals) { | ||
this.count = 0; | ||
this.head = null; | ||
this.eqFn = eq; | ||
} | ||
|
||
push(element: T) { | ||
const node = new LinkedNode<T>(element); | ||
if (this.head == null) { | ||
this.head = node; | ||
} else { | ||
let current = this.head; | ||
while (current.next != null) { | ||
current = current.next; | ||
} | ||
current.next = node; | ||
} | ||
|
||
this.count++; | ||
} | ||
|
||
// @ts-ignore | ||
insert(element: T, position: number) { | ||
// @ts-ignore | ||
const node = new LinkedListNode(element); | ||
|
||
this.count++; | ||
return false; | ||
} | ||
// @ts-ignore | ||
getElementAt(index: number) { | ||
return undefined; | ||
} | ||
|
||
// @ts-ignore | ||
remove(element: T) { | ||
return; | ||
} | ||
// @ts-ignore | ||
indexOf(element: T) { | ||
return -1; | ||
} | ||
|
||
removeAt(index: number) { | ||
if (index >= 0 && index < this.count) { | ||
let prev = this.head; | ||
let i = 0; | ||
while (prev?.next != null) { | ||
if (i === index) { | ||
break; | ||
} | ||
prev = prev.next; | ||
i++; | ||
} | ||
|
||
const target = prev?.next; | ||
|
||
if (prev && prev.next && target) { | ||
prev.next = target.next; | ||
} | ||
this.count--; | ||
return target?.val; | ||
} | ||
return; | ||
} | ||
isEmpty() { | ||
return this.size() === 0; | ||
} | ||
size() { | ||
return this.count; | ||
} | ||
toString() { | ||
const arr: T[] = []; | ||
let c = this.head; | ||
while (c?.next !== null) { | ||
c?.val && arr.push(c.val); | ||
// @ts-ignore | ||
c = c?.next; | ||
} | ||
|
||
return arr.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,11 @@ | ||
/** | ||
* 链表节点 | ||
*/ | ||
export class LinkedNode<T> { | ||
val: T; | ||
next: null | LinkedNode<T>; | ||
constructor(v: T) { | ||
this.val = v; | ||
this.next = null; | ||
} | ||
} |
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,50 @@ | ||
export interface LinkedListInterface<T> { | ||
/** | ||
* 添加一个节点 | ||
* @param element | ||
* @returns | ||
*/ | ||
push: (element: T) => void; | ||
|
||
/** | ||
* 向特定位置插入一个元素 | ||
* @param element | ||
* @param position | ||
* @returns | ||
*/ | ||
insert: (element: T, position: number) => boolean; | ||
|
||
/** | ||
* 获取指定位置的元素,无返回undefined | ||
* @param index | ||
* @returns | ||
*/ | ||
getElementAt: (index: number) => T | undefined; | ||
|
||
/** | ||
* 从链表中移除一个元素 | ||
* @param element | ||
* @returns | ||
*/ | ||
remove: (element: T) => void; | ||
|
||
/** | ||
* 返回元素在链表中的索引 | ||
* @param element | ||
* @returns | ||
*/ | ||
indexOf: (element: T) => number; | ||
|
||
/** | ||
* 从链表指定索引中移除一个元素 | ||
* @param index | ||
* @returns | ||
*/ | ||
removeAt: (index: number) => T | undefined; | ||
|
||
isEmpty: () => boolean; | ||
|
||
size: () => number; | ||
|
||
toString: () => string; | ||
} |
Oops, something went wrong.