为了设计列表的抽象数据类型,需要给出列表的定义,包括列表应该拥有哪些属性,应该
在列表上执行哪些操作。
列表是一组有序的数据。每个列表中的数据项称为元素。在 JavaScript 中,列表中的元素
可以是任意数据类型。
列表的抽象数据类型定义
方法 | 定义 |
---|---|
listSize(属性) | 列表的元素个数 |
pos(属性) | 列表的当前位置 |
length(属性) | 返回列表中元素的个数 |
clear(方法) | 清空列表中的所有元素 |
toString(方法) | 返回列表的字符串形式 |
getElement(方法) | 返回当前位置的元素 |
insert(方法) | 在现有元素后插入新元素 |
append(方法) | 在列表的末尾添加新元素 |
remove(方法) | 从列表中删除元素 |
front(方法) | 将列表的当前位置设移动到第一个元素 |
end(方法) | 将列表的当前位置移动到最后一个元素 |
prev(方法) | 将当前位置后移一位 |
next(方法) | 将当前位置前移一位 |
currPos(方法) | 返回列表的当前位置 |
moveTo(方法) | 将当前位置移动到指定位置 |
function List() {
this.listSize = 0; // 初始化列表元素个数
this.pos = 0; // 初始化列表元素个数
this.dataStore = []; // 初始化一个空数组来保存列表元素
this.append = append; // 给列表的下一个位置增加一个新的元素
this.find = find; // 查找指定元素
this.insert = insert; // 向列表中指定元素后添加元素
this.remove = remove; // 删除查找到的元素
this.contains = contains; // 判断列表中是否有某个值,和 find 差不多
this.clear = clear; // 清空元素:先删除数组,在下面新建空数组,设置列表长度为0
this.toString = toString; // 以数组形式显示所有列表元素(不是字符串)
this.length = length; // 查询列表中有多少元素
this.front = front; // 开头位置
this.end = end; // 结尾位置
this.prev = prev; // 位置前移
this.next = next; // 位置后移
this.currPos = currPos; // 返回当前所在位置
this.moveTo = moveTo; // 移动到指定位置
this.getElement = getElement; // 获取指定位置的元素
this.nextCon = nextCon; // 新增从前向后遍历的方法
this.prevCon = prevCon; // 新增从后向前遍历的方法
}
/* 定义方法 */
// 添加元素
function append(element) {
this.dataStore[this.listSize++] = element;
}
// 查找元素
function find(element) {
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.splice(foundAt,1);
--this.listSize;
return true;
}
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;
}
// 清空元素:先删除数组,在下面新建空数组,设置列表长度为0
function clear() {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
}
// 判断列表中是否有某个值,和 find 差不多
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-1) {
++this.pos;
}
}
// 返回当前所在位置
function currPos() {
return this.pos;
}
// 移动到指定位置
function moveTo(position) {
this.pos = position;
}
// 获取指定位置的元素
function getElement() {
return this.dataStore[this.pos];
}
遍历列表的例子:
这个方法有缺陷,输出结果如下,当遍历到最后一个元素以后
next 方法将不会增加当前位置的索引,导致一直循环
所以遍历不要用这种方法
var names = new List();
names.append("Clayton");
names.append("Raymond");
names.append("Cynthia");
names.append("Jennifer");
names.append("Bryan");
names.append("Danny");
for(names.front(); names.currPos() < names.length(); names.next()) {
console.log(names.getElement());
/**
这个方法有缺陷,输出结果如下,当遍历到最后一个元素以后
next 方法将不会增加当前位置的索引,导致一直循环
所以遍历不要用这种方法
getElement currPos length
Clayton 0 6
Raymond 1 6
Cynthia 2 6
Jennifer 3 6
Bryan 4 6
Danny 5 6 // 无限循环
*/
}
可以这样写:
for(names.front(); names.currPos() < names.length(); names.pos++) {
console.log(names.getElement());
}
同样从后向前遍历的方法也要改成:
for(names.end(); names.currPos() >= 0; names.prev()) {
console.log(names.getElement());
}
----->
for(names.end(); names.currPos() >= 0; names.pos--) {
console.log(names.getElement());
}
使用中也可以将这两种遍历封装成方法使用
function nextCon() {
for(this.front(); this.currPos() < this.length(); this.pos++) {
console.log(this.getElement());
}
}
function prevCon() {
for(this.end(); this.currPos() >= 0; this.pos--) {
console.log(this.getElement());
}
}
// 使用方法
names.prevCon();
names.nextCon();