2015年6月11日木曜日

開発環境

Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 3(Lists)、Exercises 1.(No. 2421)を解いてみる。

Exercises 1.(No. 2421)

JavaScript(Emacs)

var List = function () {
        this.list_size = 0;
        this.pos = 0;
        this.data_store = [];
    };

List.prototype.append = function (element) {    
    this.data_store[this.list_size] = element;
    this.list_size += 1;
};
List.prototype.find = function (element) {
    var i,
        max;
    for (i = 0, max = this.data_store.length; i < max; i += 1) {
        if (this.data_store[i] === element) {
            return i;
        }
    }
    return -1;
};
List.prototype.remove = function (element) {
    var found_at = this.find(element);

    if (found_at > -1) {
        this.data_store.splice(found_at, 1);
        this.list_size -= 1;
        return true;
    }
    return false;
};
List.prototype.length = function () {
    return this.list_size;
};
List.prototype.toString = function () {
    return this.data_store;
};
List.prototype.insert = function (element, after) {
    var insert_pos = this.find(after);
    if (insert_pos > -1) {
        this.data_store.splice(insert_pos + 1, 0, element);
        this.list_size += 1;
        return true;
    }
    return false;
};
List.prototype.clear = function () {
    this.data_store = [];
    this.list_size = this.pos = 0;
};
List.prototype.contains = function (element) {
    var i,
        max;
    for (i = 0, max = this.dataStore.length; i < max; i += 1) {
        if(this.data_store[i] === element) {
            return true;
        }
    }
    return false;
}
List.prototype.moveTo = function (pos) {
    this.pos = pos;
};
List.prototype.getElement = function () {
    return this.data_store[this.pos];
};
List.prototype.previous = function () {
    this.pos -= 1;
    return this.data_store[this.pos];
};
List.prototype.next = function () {
    var element = this.data_store[this.pos];
    this.pos += 1;
    return element;
};
List.prototype.hasNext = function () {
    if (this.pos > this.list_size -1) {
        return false;
    }
    return true;
};
List.prototype.hasPrevious = function () {
    if (this.ps <= 0) {
        return false;
    } else {
        return true;
    }
};
List.prototype.front = function () {
    this.pos = 0;
};
List.prototype.end = function () {
    this.pos = this.list_size - 1;
};
List.prototype.currPos = function () {
    return this.pos;
};

var insert = function (list, element) {
        for (list.front(); list.hasNext();) {
            if (list.next() >= element){
                return false;
            }
        }
        list.append(element);
        return true;
    },
    strings = new List(),
    nums = new List();

strings.append('js');
strings.append('c');
strings.append('scheme');
strings.append('python');
insert(strings, 'JS');
insert(strings, 'z');
insert(strings, 'js');
insert(strings, 'jsjs');
print(strings.toString());

nums.append(1);
nums.append(9);
nums.append(3);
nums.append(5);
nums.append(7);
insert(nums, 0);
insert(nums, 10);
insert(nums, 1);
insert(nums, 2);
print(nums.toString());

出力結果(Terminal, shell, SpiderMonkey)

$ js sample1.js
js,c,scheme,python,z
1,9,3,5,7,10
$

0 コメント:

コメントを投稿