Skip to content

Commit 3ceef2f

Browse files
create linked list classes
1 parent ef92b07 commit 3ceef2f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/linked-list/Graph.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Node } from './Node'
2+
3+
export class Graph {
4+
private _records: Node[]
5+
private _indexedRecords: Map<PropertyKey, Node[]>
6+
7+
constructor() {
8+
this._records = []
9+
this._indexedRecords = new Map()
10+
}
11+
12+
protected get records(): Node[] {
13+
return this._records
14+
}
15+
16+
protected get indexedRecords(): Map<PropertyKey, Node[]> {
17+
return this._indexedRecords
18+
}
19+
20+
protected addNodeBranch(newNode: Node, mapKey: PropertyKey): void {
21+
const existingNodes = this.indexedRecords.get(mapKey) ?? []
22+
this._indexedRecords.set(mapKey, [...existingNodes, newNode])
23+
this._records.push(newNode)
24+
}
25+
26+
}

src/linked-list/Node.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Graph } from './Graph'
2+
3+
export class Node<TGraph extends Graph = Graph, TNode extends Node = Node<Graph, any>> {
4+
private _parent: TGraph | TNode
5+
private _child?: TNode
6+
private _head: TNode & { parent: TGraph }
7+
8+
constructor() { }
9+
10+
set parent(parent: TGraph | TNode) {
11+
this._parent = parent
12+
}
13+
14+
get parent(): TGraph | TNode {
15+
return this._parent
16+
}
17+
18+
set child(child: TNode) {
19+
this._child = child
20+
}
21+
22+
get child(): TNode {
23+
return this._child
24+
}
25+
26+
set head(head: TNode & { parent: TGraph }) {
27+
this._head = head
28+
}
29+
30+
get head(): TNode & { parent: TGraph } {
31+
return (this.isHead() ? this : this._head)
32+
}
33+
34+
protected isHead(): this is TNode & { parent: TGraph } {
35+
return typeof this._head === 'undefined'
36+
}
37+
38+
protected isIntermediateNode(): this is TNode & { parent: TNode } {
39+
return !this.isHead()
40+
}
41+
}

0 commit comments

Comments
 (0)