// 列表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 = listLength; // 列表长度 this.currPos = currPos; // 当前指针位置 // this.moveTo = moveTo; this.getElement = getElement; // 获取当前位置的元素 this.contains = contains; // 查询是否包含某元素}function append(element) { this.dataStore[this.listSize++] = element;}function find(element) { for(let i = 0; i< this.listSize; i++) { if (this.dataStore[i] === element) { return i; } } return -1;}function remove(element) { var findIndex = this.find(element); if (findIndex > -1) { this.dataStore.slice(findIndex, 1); --this.listSize; return; } return false;}function listLength() { 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 = 0;}function contains(element) { for(let i = 0; i< this.listSize; 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 getElement() { return this.dataStore[this.pos];}var names = new List();names.append('小红');names.append('小王');names.append('小李');names.next();// 获取当前指针元素console.log(names.getElement())// 遍历列表for(names.front();names.currPos() < names.listSize; names.next()) { console.log(names.getElement())}