forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list.ts
160 lines (136 loc) · 3.36 KB
/
linked-list.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
151
152
153
154
155
156
157
158
159
160
import { defaultCompare, defaultEquals, ICompareFunction, IEqualsFunction } from '../util';
import { Node } from './models/linked-list-models';
export default class LinkedList<T> {
protected count = 0;
protected head: Node<T> | undefined;
constructor(
protected equalsFn: IEqualsFunction<T> = defaultEquals,
protected compareFn: ICompareFunction<T> = defaultCompare
) {}
push(element: T) {
const node = new Node(element);
let current;
if (this.head == null) { // catches null && undefined
this.head = node;
} else {
current = this.head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
this.count++;
}
getElementAt(index: number) {
if (index >= 0 && index <= this.count) {
let node = this.head;
for (let i = 0; i < index; i++) {
if (node != null) {
node = node.next;
}
}
return node;
}
return undefined;
}
insert(index: number, element: T) {
if (index >= 0 && index <= this.count) {
const node = new Node(element);
const current = this.head;
if (index === 0) {
node.next = current;
this.head = node;
} else {
const previous = this.getElementAt(index - 1);
if (previous != null) {
node.next = previous.next;
previous.next = node;
}
}
this.count++;
return true;
}
return false;
}
insertSorted(element: T) {
if (this.isEmpty()) {
this.push(element);
} else {
const index = this.getIndexNextSortedElement(element);
this.insert(index, element);
}
}
private getIndexNextSortedElement(element: T) {
let current = this.head;
for (let i = 0; i < this.size() && current; i++) {
const comp = this.compareFn(element, current.element);
if (comp >= 0) {
return i;
}
current = current.next;
}
return -1;
}
removeAt(index: number) {
if (index >= 0 && index < this.count) {
let current = this.head;
if (index === 0) {
if (this.head != null) {
this.head = this.head.next;
}
} else {
const previous = this.getElementAt(index - 1);
if (previous != null) {
current = previous.next;
if (current != null) {
previous.next = current.next;
}
}
}
if (current != null) {
this.count--;
return current.element;
}
}
return undefined;
}
remove(element: T) {
const index = this.indexOf(element);
return this.removeAt(index);
}
indexOf(element: T) {
let current = this.head;
for (let i = 0; i < this.size() && current != null; i++) {
if (this.equalsFn(element, current.element)) {
return i;
}
current = current.next;
}
return -1;
}
isEmpty() {
return this.size() === 0;
}
size() {
return this.count;
}
getHead() {
return this.head;
}
clear() {
this.head = undefined;
this.count = 0;
}
toString() {
if (this.head == null) {
return '';
}
let objString = `${this.head.element}`;
let current = this.head.next;
for (let i = 1; i < this.size() && current != null; i++) {
objString = `${objString},${current.element}`;
current = current.next;
}
return objString;
}
}