列表的定义
# 列表的定义
列表是一组有序的数据。每个列表中的数据项称为元素。元素的数量受内存控制。
不包含任何元素的列表称为空列表。
# 列表的使用场景
日常生活中,我们使用购物清单、待办事项列表都是列表。计算机中的列表也一样。
元素不是很多。
不需要很长序列查找元素或者排序。
列表是一种最自然的数据组织方式。
# 迭代器的优点
访问元素时不必关心底层数据结构。
增加和删除元素要比 for 更加灵活。
迭代器访问列表里的元素提供了统一的方法。
# 列表的代码实现
function List() {
this.listSize = 0; // 列表元素个数
this.pos = 0; // 列表当前位置
this.dataStore = []; // 初始化一个空数组用来保存列表元素
this.clear = clear; // 清空列表中的所有元素
this.find = find; // 查找元素
this.toString = toString; // 返回列表的字符串形式
this.insert = insert; // 在现有元素后面插入新元素
this.append = append; // 在列表元素末尾添加新元素
this.remove = remove; // 从列表中删除元素
this.front = front; // 从列表的当前位置移动到第一个位置
this.end = end; // 从列表的当前位置移动到最后一个位置
this.prev = prev; // 将当前位置前移一位
this.next = next; // 将当前位置后移一位
this.length = length; // 列表包含的元素个数
this.currPos = currPos; // 返回列表的当前位置
this.moveTo = moveTo; // 将当前位置的元素移动到指定位置
this.getElement = getElement; // 获取当前的元素
this.contains = contains; // 是否包含某个元素
}
// 在列表元素末尾添加新元素
function append(element) {
this.dataStore[this.listSize++] = element;
}
// 查找元素
function find(element) {
// 注意此处 ++ 应该置于 i 之前,以便返回正确的索引
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] === element) {
return i;
}
}
return -1;
}
// 从列表中删除元素
function remove(element) {
var foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.slice(foundAt, 1);
--this.listSize;
return;
}
return false;
}
// 列表包含的元素个数
function length() {
return this.listSize;
}
// 返回列表的字符串形式
function toString() {
return this.dataStore;
}
// 在现有元素后面插入新元素
function insert(element, after) {
var insertPos = this.find(after);
if (insertPos > -1) {
this.dataStore.splice(insertPos + 1, 0, element);
++this.listSize;
return true;
}
return false;
}
// 清空列表中的所有元素
function clear() {
delete this.dataStore;
this.dataStore.length = 0; // 创建一个空数组
this.listSize = this.pos = 0;
}
// 是否包含某个元素
function contains(element) {
for (var i = 0; i < this.dataStore.length; i++) {
if (this.dataStore[i] === element) {
return true;
}
}
return false;
}
// 从列表的当前位置移动到第一个位置
function front() {
this.pos = 0;
}
// 从列表的当前位置移动到最后一个位置
function end() {
this.pos = this.listSize - 1;
}
// 将当前位置前移一位
function prev() {
if (this.pos > 0) {
--this.pos;
}
}
// 将当前位置后移一位
function next() {
if (this.pos < this.listSize) {
++this.pos;
}
}
// 返回列表的当前位置
function currPos() {
return this.pos;
}
// 将当前位置的元素移动到指定位置
function moveTo(position) {
this.pos = position;
}
// 获取当前的元素
function getElement() {
return this.dataStore[this.pos];
}
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
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
# 列表的实际应用
var names = new List();
names.append("小白");
names.append("小王");
names.append("小红");
// names.next();
// console.log(names.getElement());
// 迭代器
for (names.front; names.currPos() < names.length(); names.next()) {
console.log(names.getElement());
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11