-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.js
91 lines (73 loc) · 1.39 KB
/
List.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
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
class List {
constructor(listArray = []) {
this.dataStore = listArray;
}
listSize = this.dataStore.length || 0;
pos = 0;
length() {
return this.listSize;
}
clear() {
delete this.dataStore;
this.dataStore = [];
}
toString() {
return this.dataStore;
}
getElement() {
return this.dataStore[this.pos];
}
insert(element, after) {
const insertPos = this.find(after);
if (insertPos > -1) {
this.dataStore.splice(insertPos+1, 0, element);
return true;
}
return false;
}
append(element) {
this.dataStore[this.length++] = element;
}
remove(element) {
const foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt, 1);
return true;
}
return false;
}
contains(element) {
for (let i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] === element) {
return true;
}
}
return false;
}
front() {
this.pos = 0;
}
end() {
this.pos = this.listSize - 1;
}
prev() {
if (this.pos > 0) --this.pos;
}
next() {
if (this.pos < this.listSize -1) ++this.pos;
}
currPos() {
return this.pos;
}
moveTo(position) {
this.pos = position;
}
find(element) {
for (let i = 0; i < this.length; ++i) {
if (this.dataStore[i] === element) {
return i;
}
}
return -1;
}
}