forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-SortedLinkedList.js
53 lines (38 loc) · 1.3 KB
/
04-SortedLinkedList.js
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
const { SortedLinkedList } = PacktDataStructuresAlgorithms;
const { util } = PacktDataStructuresAlgorithms;
const list = new SortedLinkedList();
console.log('');
for (let i = 5; i > 0; i--) {
list.push(i);
}
console.log('list after pushing 5, 4, 3, 2, and 1 => ', list.toString());
console.log('list.removeAt(1) => ', list.removeAt(1));
console.log('remove element 16 => ', list.remove(5));
console.log('list.toString() => ', list.toString());
// ------- Example 02
class MyObj {
constructor(el1, el2) {
this.el1 = el1;
this.el2 = el2;
}
toString() {
return `${this.el1.toString()}|${this.el2.toString()}`;
}
}
function myObjCompare(a, b) {
return a.toString().localeCompare(b.toString());
}
const ds = new SortedLinkedList(util.defaultEquals, myObjCompare);
console.log('*** SortedLinkedList with custom sorting function');
ds.push(new MyObj(3, 4));
console.log('push MyObj(3, 4)');
console.log('list.toString() => ', ds.toString());
ds.push(new MyObj(1, 2));
console.log('push MyObj(1, 2)');
console.log('list.toString() => ', ds.toString());
ds.push(new MyObj(5, 6));
console.log('push MyObj(5, 6)');
console.log('list.toString() => ', ds.toString());
ds.insert(new MyObj(0, 0), 4);
console.log('insert MyObj(0, 0) pos 4 (pos ignored)');
console.log('list.toString() => ', ds.toString());