-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.ts
150 lines (126 loc) · 2.83 KB
/
LinkedList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @public
*/
export type LinkedListNode<T> = {
value: T;
next: LinkedListNode<T> | null;
};
/**
* Linked List
* @public
*/
class LinkedList<T> {
private nodes: LinkedListNode<T>[] = [];
constructor() {
this.nodes = [];
}
/**
* Returns the size of the list
*/
get size() {
return this.nodes.length;
}
/**
* Returns the head of the list
*/
get head() {
return this.size ? this.nodes[0] : null;
}
/**
* Returns the tail of the list
*/
get tail() {
return this.size ? this.nodes[this.size - 1] : null;
}
/**
* Check if the linked list is empty
*/
get isEmpty() {
return this.size === 0;
}
/**
* Adds a node to the list
* @param index - The index to add the node at
* @param value - The value to add to the node
* @returns The node that was added
* @public
*/
public insertAt(index: number, value: T) {
const previousNode = this.nodes[index - 1] || null;
const nextNode = this.nodes[index] || null;
const node = { value, next: nextNode };
if (previousNode) previousNode.next = node;
this.nodes.splice(index, 0, node);
return node;
}
/**
* inserts a node to the front of the list
* @param value - The value to add to the node
* @returns The node that was added
* @public
*/
public insertFirst(value: T) {
this.insertAt(0, value);
return this.head;
}
/**
* inserts a node to the end of the list
* @param value - The value to add to the node
* @returns The node that was added
* @public
*/
insertLast(value: T) {
this.insertAt(this.size, value);
return this.tail;
}
/**
* gets the node at the specified index
* @param index - The index to get the node at
* @returns The node at the specified index
* @public
*/
getAt(index: number) {
return this.nodes[index];
}
/**
* removes the node at the specified index
* @param index - The index to remove the node at
* @returns The node that was removed
* @public
*/
removeAt(index: number) {
const previousNode = this.nodes[index - 1];
const nextNode = this.nodes[index + 1] || null;
const node = this.nodes[index];
if (previousNode) previousNode.next = nextNode;
this.nodes.splice(index, 1);
return node;
}
/**
* clears the list
* @public
*/
clear() {
this.nodes = [];
}
/**
* reverses the list
* @public
*/
reverse() {
this.nodes = this.nodes.reduce(
(acc, { value }) => [{ value, next: acc[0] || null }, ...acc],
[] as LinkedListNode<T>[]
);
}
map(fn: (value: T, index: number) => T) {
return this.nodes.map(({ value }, index) => fn(value, index));
}
toString() {
return this.nodes.map(({ value }) => value).join(",");
}
*[Symbol.iterator]() {
yield* this.nodes;
}
}
export default LinkedList;