第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