實(shí)現(xiàn)列表類
// 列表類
function List () {
this.listSize = 0; // 列表的元素個(gè)數(shù)
this.pos = 0; // 列表的當(dāng)前位置
this.dataStore = []; // 初始化一個(gè)空數(shù)組來保存列表元素
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.hasNext = hasNext;
this.hasPrev = hasPrev;
this.length = length;
this.currPos = currPos; // 返回列表的當(dāng)前位置
this.moveTo = moveTo;
this.getElement = getElement; // 返回當(dāng)前位置的元素
this.contains = contains;
}
// append: 給列表添加元素
function append (element) {
this.dataStore[this.listSize++] = element;
}
// find: 在列表中查找某一元素 indexOf?
function find (element) {
for (let i = 0; i < this.dataStore.length; i++) {
if (this.dataStore[i] === element) {
return i;
}
}
return -1;
}
// remove: 從列表中刪除元素
function remove (element) {
let foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt, 1);
this.listSize--;
return true;
}
return false;
}
// length: 列表中有多少個(gè)元素 與listSize區(qū)別?
function length () {
return this.listSize;
}
// toString: 顯示列表中的元素
function toString () {
return this.dataStore;
}
// insert: 向列表中插入一個(gè)元素
function insert (element, after) {
let insertPos = this.find(after);
if (insertPos > -1) {
this.dataStore.splice(insertPos + 1, 0, element);
this.listSize++;
return true;
}
return false;
}
// clear: 清空列表中所有的元素
function clear () {
delete this.dataStore;
this.dataStore.length = 0;
this.listSize = this.pos = 0;
}
// contains: 判斷給定值是否在列表中 find?
function contains (element) {
for (let i = 0; i < this.dataStore.length; i++) {
if (this.dataStore[i] === element) {
return true;
}
}
return false;
}
// 遍歷列表
function front () {
this.pos = 0;
}
function end () {
this.pos = this.listSize - 1;
}
function prev () {
--this.pos;
}
function next () {
if (this.pos < this.listSize) {
++this.pos;
}
}
function currPos () {
return this.pos;
}
function moveTo (position) {
this.pos = position;
}
function getElement () {
return this.dataStore[this.pos];
}
function hasNext () {
return this.pos < this.listSize;
}
function hasPrev () {
return this.pos >= 0;
}
練習(xí)
一. 增加一個(gè)向列表中插入元素的方法蛤奥,該方法只在待插元素大于列表中的所有元素時(shí)才執(zhí)行插入操作晒夹。這里的大于有多重含義裆馒,對于數(shù)字,它是指數(shù)值上的大胸で印喷好;對于字母,它是指在字母表中出現(xiàn)的先后順序响逢。
List.prototype.insertThen = function (element) {
let type = typeof element;
if (type === `number`) {
// debugger;
for (let i = 0; i < this.dataStore.length; i++) {
if (typeof this.dataStore[i] === `number` && element > this.dataStore[i]) { // 同時(shí)滿足是數(shù)字然后插入的元素大于數(shù)組內(nèi)的元素
this.append(element);
return true;
}
}
} else {
let newArr = this.dataStore.filter((val) => {
return typeof val !== `number`;
}).concat([element]).sort();
if (newArr.indexOf(element) === (newArr.length - 1)) {
this.append(element);
return true;
}
}
return false;
};
// 示例
let DataThen = new List();
DataThen.append(`Mazey`);
DataThen.append(`Cherrie`);
DataThen.append(`Luna`);
DataThen.append(`John`);
DataThen.append(`July`);
DataThen.append(23);
DataThen.append(73);
console.log(DataThen.toString()); // ["Mazey", "Cherrie", "Luna", "John", "July", 23, 73]
DataThen.insertThen(99);
DataThen.insertThen(12);
console.log(DataThen.toString()); // ["Mazey", "Cherrie", "Luna", "John", "July", 23, 73, 99]
DataThen.insertThen(`Jay`);
DataThen.insertThen(`Zero`);
console.log(DataThen.toString()); // ["Mazey", "Cherrie", "Luna", "John", "July", 23, 73, 99, "Zero"]
二. 增加一個(gè)向列表中插入元素的方法绒窑,該方法只在待插元素小于列表中的所有元素時(shí)才執(zhí)行插入操作。
List.prototype.insertThen = function (element) {
let type = typeof element;
if (type === `number`) {
// debugger;
for (let i = 0; i < this.dataStore.length; i++) {
if (typeof this.dataStore[i] === `number` && element < this.dataStore[i]) { // 同時(shí)滿足是數(shù)字然后插入的元素大于數(shù)組內(nèi)的元素
this.append(element);
return true;
}
}
} else {
let newArr = this.dataStore.filter((val) => {
return typeof val !== `number`;
}).concat([element]).sort();
if (newArr.indexOf(element) === 0) {
this.append(element);
return true;
}
}
return false;
};
// 示例
let DataThen = new List();
DataThen.append(`Mazey`);
DataThen.append(`Cherrie`);
DataThen.append(`Luna`);
DataThen.append(`John`);
DataThen.append(`July`);
DataThen.append(23);
DataThen.append(73);
console.log(DataThen.toString()); // ["Mazey", "Cherrie", "Luna", "John", "July", 23, 73]
DataThen.insertThen(99);
DataThen.insertThen(12);
console.log(DataThen.toString()); // ["Mazey", "Cherrie", "Luna", "John", "July", 23, 73, 12]
DataThen.insertThen(`Jay`);
DataThen.insertThen(`Zero`);
DataThen.insertThen(`Ada`);
console.log(DataThen.toString()); // ["Mazey", "Cherrie", "Luna", "John", "July", 23, 73, 12, "Ada"]
三. 創(chuàng)建Person類舔亭,該類用于保存人的姓名和性別信息些膨。創(chuàng)建一個(gè)至少包含10個(gè)Person對象的列表。寫一個(gè)函數(shù)顯示列表中所有擁有相同性別的人钦铺。
function Person () {
this.list = [];
this.save = save;
this.showSameGender = showSameGender;
}
// 保存人名和性別
function save (name, gender) {
let littleCase = {
name,
gender
};
this.list.push(littleCase);
}
// 顯示相同性別的人
function showSameGender (gender) {
let ret = [];
let len = this.list.length;
while (len--) {
if (this.list[len].gender === gender) {
ret.push(this.list[len].name);
}
}
return ret;
}
// 示例
let people = new Person();
people.save(`Mazey`, `male`);
people.save(`John`, `male`);
people.save(`Zero`, `male`);
people.save(`July`, `male`);
people.save(`Bob`, `male`);
people.save(`Ada`, `female`);
people.save(`Cherrie`, `female`);
people.save(`Luna`, `female`);
people.save(`Lucy`, `female`);
people.save(`June`, `female`);
console.log(people.list);
console.log(people.showSameGender(`male`)); // ["Bob", "July", "Zero", "John", "Mazey"]
console.log(people.showSameGender(`female`)); // ["June", "Lucy", "Luna", "Cherrie", "Ada"]