-
Notifications
You must be signed in to change notification settings - Fork 0
/
ordered-list.ts
57 lines (47 loc) · 1.25 KB
/
ordered-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
import UnorderedList, { Node } from "./unordered-list.ts";
class OrderedList<T> extends UnorderedList<T> {
search(item: T): boolean {
let current = this.head;
while (current !== undefined) {
if (current.value === item) {
return true;
}
if (current.value > item) {
return false;
}
current = current.next;
}
return false;
}
add(item: T): void {
let current = this.head;
let previous = undefined;
while (current !== undefined) {
if (current.value > item) {
break;
}
[previous, current] = [current, current.next];
}
const node = new Node(item);
if (previous === undefined) {
node.next = this.head;
this.head = node;
} else {
node.next = current;
previous.next = node;
}
}
append(_item: T): void {
// append is a no-op for an ordered list,
// since it could break ordering
}
insert(_pos: number, _item: T): void {
// assert is a no-op for an ordered list,
// since it could break ordering
}
// remove(item: T): void
// the unordered version works, but it _could_ be made more
// efficient in an ordered list by finding the item for
// removal with binary search
}
export default OrderedList;