2015年7月25日土曜日

開発環境

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

Exercises 5.(No. 4245)

JavaScript(Emacs)

/*jslint         browser : true, continue : true,
  devel  : true, indent  : 4,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : false,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global print, readline*/
var Node = function (element) {
        this.element = element;
        this.next = null;
    },
    LinkedList = function () {
        this.head = new Node('head');
    },
    DNode = function (element) {
        this.element = element;
        this.next = null;
        this.previous = null;
    },
    DLinkedList = function () {
        this.head = new DNode('head');
    },
    cities;

LinkedList.prototype.find = function (item) {
    var curr_node = this.head;
    while (curr_node.element !== item) {
        curr_node = curr_node.next;
    }
    return curr_node;
};
LinkedList.prototype.insert = function (new_element, item) {
    var new_node = new Node(new_element),
        current = this.find(item);
    new_node.nex = current.next;
    current.next = new_node;
};
LinkedList.prototype.display = function () {
    var curr_node = this.head;
    while (curr_node.next !== null) {
        print(curr_node.next.element);
        curr_node = curr_node.next;
    }
};
LinkedList.prototype.findPrevious = function (item) {
    var curr_node = this.head;
    while (curr_node !== null && curr_node.next.element !== item) {
        curr_node = curr_node.next;
    }
    return curr_node;
};
LinkedList.prototype.remove = function (item) {
    var prev_node = this.findPrevious(item);
    
    if (prev_node.next !== null) {
        prev_node.next = prev_node.next.next;
    }
};
DNode.prototype = new Node();
DNode.prototype.constructor = DNode;
DLinkedList.prototype = new LinkedList();
DLinkedList.prototype.constructor = DLinkedList;

DLinkedList.prototype.findLast = function () {
    var curr_node = this.head;

    while (curr_node.next !== null) {
        curr_node = curr_node.next;
    }
    return curr_node;
};
DLinkedList.prototype.dispReverse = function () {
    var curr_node = this.findLast();
    
    while (curr_node.previous !== null) {
        print(curr_node.element);        
        curr_node = curr_node.previous;
    }
};
DLinkedList.prototype.remove = function (item) {
    var curr_node = this.find(item);
    if (curr_node.next !== null) {
        curr_node.previous.next = curr_node.next;
        curr_node.next.previous = curr_node.previous;
        curr_node.next = null;
        curr_node.previous = null;
    }
};
DLinkedList.prototype.insert = function (new_element, item) {
    var new_node = new DNode(new_element),
        current = this.find(item);

    new_node.next = current.next;
    new_node.previous = current;
    current.next = new_node;
};

cities = new DLinkedList();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Carlisle", "Russellville");
cities.insert("Alma", "Carlisle");
cities.display();
print();
cities.remove("Carlisle");
cities.display();
print();
cities.dispReverse();

出力結果(Terminal, shell, SpiderMonkey)

$ jslint sample5.js

sample5.js is OK.
$ js sample5.js
Conway
Russellville
Carlisle
Alma

Conway
Russellville
Alma

Alma
Russellville
Conway
$ 

0 コメント:

コメントを投稿

Comments on Google+: