-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
87 lines (82 loc) · 2.06 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
/**
* Created by shijin on 2016/3/19.
*/
function List() {
this.dataStore = [];
this.listSize = arguments.length;
this.pos = 0;
this.length = function () {
return this.listSize;
};
this.clear = function () {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
};
this.toString = function () {
return this.dataStore;
};
this.getElement = function () {
return this.dataStore[this.pos];
};
this.insert = function (element, after) {
var afterIndex = this.dataStore.indexOf(after);
if (afterIndex >= 0) {
this.dataStore.splice(afterIndex + 1, 0, element);
this.listSize++;
return true;
}
return false;
};
this.append = function (element) {
this.dataStore[this.listSize++] = element;
};
this.remove = function (element) {
var index = this.dataStore.indexOf(element);
if (index >= 0) {
this.dataStore.splice(index, 1);
this.listSize--;
return true
}
return false;
};
this.front = function () {
this.pos = 0;
};
this.end = function () {
this.pos = this.listSize - 1;
};
this.prev = function () {
if (this.pos > 0)
this.pos--;
};
this.next = function () {
if (this.pos < this.listSize - 1)
this.pos++;
};
this.currPos = function () {
return this.pos;
};
this.moveTo = function (position) {
this.pos=position;
};
this.contain = function (element) {
return this.dataStore.indexOf(element) >= 0
}
}
var aList = new List();
aList.append("stone");
aList.append("orange");
console.log(aList.toString());
aList.insert("apple", "stone");
console.log(aList.toString());
aList.moveTo(1);
console.log(aList.getElement());
aList.prev();
console.log(aList.getElement());
aList.next();
console.log(aList.getElement());
aList.front();
console.log(aList.getElement());
aList.end();
console.log(aList.getElement());