1. 程式人生 > >JavaScript數據結構與算法-列表練習

JavaScript數據結構與算法-列表練習

出現 保存 contains next let delete pro push 含義

實現列表類

// 列表類
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數據結構與算法-列表練習