-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListObject.html
More file actions
134 lines (125 loc) · 2.86 KB
/
ListObject.html
File metadata and controls
134 lines (125 loc) · 2.86 KB
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
<script>
/**
length() //列表中包含元素的个数称
append() //列表末尾添加一个元素
insert() //给定元素后或列表的起始位置添加一个元素
remove() //删除元素
clear() //清空列表所有元素
toString() //显示列表所有元素
getElement() //显示当前元素
next() //从当前元素移动到下一个元素
prev() //移动到当前元素的前一个元素
moveTo() //移动到指定位置
currPos() //列表当前位置
find() //查找元素是否在列表中,并返回当前位置
contains() //判断当前元素是否在列表中
**/
function List() {
this.listSize = 0;
this.pos = 0;
this.dataStore = []; // 初始化一个空数组来保存列表元素
}
List.prototype = {
append: function(element){
this.dataStore[this.listSize++] = element;
},
length: function(){
return this.listSize;
},
toString: function(){
return dataStore;
},
find: function(element){
for (var i = 0; i < this.dataStore.length; i++) {
if(this.dataStore[i] == element){
return i;
}
}
return -1;
},
remove: function(element){
var foundAt = this.find(element);
if(foundAt > -1){
this.dataStore.splice(foundAt, 1);
this.listSize--;
}
},
insert: function(element, after){
var insertPos = this.find(after);
if(insertPos > -1){
this.dataStore.splice(insertPos+1, 0 , element);
this.listSize++;
return true;
}
return false;
},
clear: function(){
delete this.dataStore;
this.dataStore = [];
this.listSize = 0;
this.pos = 0;
},
contains: function(element){
for (var i = 0; i < this.dataStore.length; i++) {
if(this.dataStore[i] == element){
return true;
}
}
return false;
},
front: function(){
this.pos = 0;
},
end: function(){
this.pos = this.listSize-1;
},
prev: function(){
if(this.pos > 0){
this.pos--;
}
},
next: function(){
this.pos++;
},
currPos: function(){
return this.pos;
},
moveTo: function(position){
this.pos = position;
},
getElement: function(){
return this.dataStore[this.pos];
}
}
var names = new List();
names.append('a');
names.append('b');
names.append('c');
names.append('d');
names.append('e');
names.append('f');
// names.moveTo(0);
// console.log(names.currPos())
// names.next()
// console.log(names.getElement())
// names.next()
// names.next()
// console.log(names.getElement())
names.moveTo(0);
// console.log(names.getElement());
// names.moveTo(1);
// console.log(names.getElement())
// names.moveTo(2);
// console.log(names.getElement())
// names.moveTo(3);
// console.log(names.getElement())
// names.moveTo(4);
// console.log(names.getElement())
// names.moveTo(5);
// console.log(names.getElement())
// console.log(names.currPos())
// console.log(names.length())
for(names.front(); names.currPos() < names.length(); names.next()) {
document.writeln(names.getElement())
}
</script>