第3章——列表类的实现
首先定义一个List
类,在构造函数constructor
体内设置2个变量dataStore
和 listSize
分别用来初始化一个空数组保存列表元素和列表元素个数,之后是一些方法的实现:👇👇👇
class List {
constructor() {
this.dataStore = []; //初始化一个空数组保存列表元素
this.listSize = 0; //列表元素个数
}
<!--方法-->
// apend()
//find()
......
}
append()
-
在
List
类里面添加append()
方法append(element) { this.dataStore[this.listSize++] = element; }
-
实践(增加几条数据)
let newList = new List(); newList.append('Anna'); newList.append('Jack'); console.log(newList);
-
结果
find()
-
在
List
类里面添加find()
find(element) { for (let i = 0; i < this.dataStore.length; i++) { if (this.dataStore[i] == element) return i; } return -1; }
-
实践(查找上面添加的某条数据)
let findData=newList.find("Anna"); console.log(findData); //0
remove()
-
在
List
类里面添加remove()
remove(element) { let foundAt = this.find(element); if (foundAt > -1) { this.dataStore.splice(foundAt, 1); --this.listSize; return true; } return false; }
-
实践(删除上面增加的某条数据)
let removeData=newList.remove("Kan"); console.log(removeData); //true console.log(newList);
-
效果
length()
-
在
List
类里面添加length()
length() { return this.listSize; }
-
实践
let length=newList.length(); console.log(length); //4
toString() — 显示列表中的元素
-
在
List
类里面添加toString()
toString() { return this.dataStore; }
-
实践
let stringData=newList.toString(); console.log(stringData); // ["Anna", "Jack", "Minni", "Kan"]
insert()
insert()
方法用到了 find()
方法,find()
方法会寻找传入的after
参数在列 表中的位置,找到该位置后,使用 splice()
方法将新元素插入该位置之后,然后将变量listSize
加 1
并返回true
,表明插入成功。
-
在
List
类里面添加insert()
insert(element, after) { let insertPos = this.find(after); if (insertPos > -1) { this.dataStore.splice(insertPos + 1, 0, element); ++this.listSize; return true; } return false; }
-
实践
比如我想在
Anna
后面 插入Linda
:let insertData=newList.insert("Linda",'Anna'); console.log(insertData); console.log(newList);
-
效果
clear()
-
在
List
类里面添加clear()
clear() { delete this.dataStore; this.dataStore = []; this.listSize = 0; }
-
实践
let clearData=newList.clear(); console.log(newList);
-
效果
contains
-
在
List
类里面添加contains()
contains(element) { for (let i = 0; i < this.dataStore.length; i++) { if (this.dataStore[i] == element) { return true; } } return false; }
-
实践
let containsData=newList.contains('Anna'); console.log(containsData); //true
遍历列表
以下方法允许用户在列表上自由移动,getElement()
返回列表的当前元素:
-
在
List
类里面添加以下方法:// 1)front()——最前位置 front() { this.pos = 1; } // 2)end()——最后位置 end() { this.pos = this.listSize - 1; } // 3)prev()—— 移动至前一个位置 prev() { if (this.pos > 0) { --this.pos; } } // 4)next()—— 移动至下一个位置 next() { if (this.pos < this.listSize - 1) { ++this.pos; } } // 5)currPos()—— 当前位置 currPos() { return this.pos; } // 6)moveTo()—— 移动至指定位置 moveTo(position) { this.pos = position; } // 7)getElement()—— 获取当前元素 getElement() { return this.dataStore[this.pos]; }
-
实践
调用以上任意方法后若想返回该元素,得使用
getElement()
方法://调用front() let frontData = newList.front(); console.log(newList.getElement()); //Jack //调用end() newList.end(); console.log(newList.getElement()); //Kan //调用prev()——调用之前得调用上面2个任意方法,得知道this.pos的值 newList.prev(); console.log(newList.getElement()); //调用这个之前看调用了front()还是end(),若是前者该结果是Anna,若是后者该结果是Minni //调用next() newList.next(); console.log(newList.getElement()); //Minni //调用currPos() newList.currPos(); console.log(newList.getElement()); //Jack //调用moveTo() newList.moveTo(0); console.log(newList.getElement()); //Anna