Skip to content

Commit

Permalink
feat: add linkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
javaswing committed Nov 13, 2023
1 parent 856d022 commit 292fc56
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 169 deletions.
4 changes: 2 additions & 2 deletions code/linked-list/LinkedListNote.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class LinkedListNode<T> {
value: T;
next: LinkedListNode<T> | null;
next: LinkedListNode<T> | undefined;

constructor(val: T, next = null) {
constructor(val: T, next = undefined) {
this.value = val;
this.next = next;
}
Expand Down
91 changes: 91 additions & 0 deletions code/linked-list/book/LinkedList.ts
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(',');
}
}
11 changes: 11 additions & 0 deletions code/linked-list/book/LinkedNode.ts
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;
}
}
50 changes: 50 additions & 0 deletions code/linked-list/book/type.ts
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;
}
Loading

0 comments on commit 292fc56

Please sign in to comment.