当不需要在一个很长的序列中查找元素,或者对其进行排序时,列表显得尤为有用。反之,如果数据结构非常复杂,列表的作用就没那么大了。
列表是一组有序数据。每个列表中的数据称为元素。在JavaScript中,列表中的元素可以是任意数据类型。
不包含任何元素的列表称为空列表。列表中包含元素的个数称为列表的长度(length),用listSize保存列表的个数。
用append 在列表末尾添加一个元素,
在一个给定元素后或列表的起始位置insert一个元素,
使用remove方法从列表中删除元素,
使用clear方法清空列表中的元素,
使用toString 显示列表中的所有元素,
使用getElement方法显示当前元素。
列表拥有描述元素位置的属性,列表有前(front)和有后(end),
使用next方法可以从当前元素移动到下一个元素,
使用prev方法可以移动到当前元素的前一个元素,
使用moveTo(n)移动到指定位置。
列表实现类
function List() {
this.listSize = 0;
this.pos = 0;
this.dataStore = [];//初始化一个空数组来保存列表元素
this.clear = clear;
this.find = find;
this.insert = insert;
this.toString = toString;
this.append = append;
this.remove = remove;
this.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.curPos = curPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.length = length;
this.contains = contains;
}