/** * 查看是否包含某个元素 * @param item * @return boolean */ contains(item: T): boolean { for (let e of this.dataStore) { if (e.data === item.data) { returntrue } } returnfalse }
/** * 查找下一条数据,把游标往后移动一位 */ next(): T | null { if (this.pos > this.listSize - 1) { returnnull } returnthis.dataStore[this.pos++] }
/** * 查找前一个元素,把游标向前移动一位 */ pre(): T | null { if (this.pos < 0) { returnnull } returnthis.dataStore[--this.pos] } }
let l = new List() l.append({data: 0}) l.append({data: 1}) l.insert({data: 1}, {data: 2}) l.find({data: 0}) l.remove({data: 0}) console.log(l.length()) console.log(l.contains({data: 1})) console.log(l.contains({data: 3})) console.log(l.toString()) // l.clear() console.log(l.length()) console.log(l.toString()) let next = l.next() while (next) { console.log(next) next = l.next() } let pre = l.pre() while (pre) { console.log(pre) pre = l.pre() }