標籤:div 資訊 down .sh 增加 尋找 size save type
實現列表類
// 列表類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.hasNext = hasNext; this.hasPrev = hasPrev; this.length = length; this.currPos = currPos; // 返回列表的當前位置 this.moveTo = moveTo; this.getElement = getElement; // 返回當前位置的元素 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: 列表中有多少個元素 與listSize區別?function length () { return this.listSize;}// toString: 顯示列表中的元素function toString () { return this.dataStore;}// insert: 向列表中插入一個元素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;}
練習一. 增加一個向列表中插入元素的方法,該方法只在待插元素大於列表中的所有元素時才執行插入操作。這裡的大於有多重含義,對於數字,它是指數值上的大小;對於字母,它是指在字母表中出現的先後順序。
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]) { // 同時滿足是數字然後插入的元素大於數組內的元素 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"]
二. 增加一個向列表中插入元素的方法,該方法只在待插元素小於列表中的所有元素時才執行插入操作。
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]) { // 同時滿足是數字然後插入的元素大於數組內的元素 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"]
三. 建立Person類,該類用於儲存人的姓名和性別資訊。建立一個至少包含10個Person對象的列表。寫一個函數顯示列表中所有擁有相同性別的人。
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"]
JavaScript資料結構與演算法-列表練習