0

私はJavaScriptで簡単なリンクリストを作成しています(私は初心者です)。次のコードがあります。

var List = function () {
  this.LinkedList = {
    "Head": {}
  };
};
List.prototype = {
  insert: function (element) {
    var Node = this.head();
    while (Node.hasOwnProperty("Node")) {
      Node = this.next(Node);
    }
    Node["Node"] = {
      "element": element
    };
  },
  remove: function (element) {
    var Node = this.head();
    while (Node.element != element) {
      Node = this.next(Node);
    }
    delete Node.element;
    Node = Node.Node; //overwriting Node with Node.Node
  },
  next: function (Node) {
    return Node.Node;
  },
  head: function () {
    return this.LinkedList.Head;
  },
  getList: function () {
    return this.LinkedList;
  }
};

挿入してる時は元気そうで、

var myList = new List();
myList.insert(1);
myList.insert(5);
myList.insert(6);
myList.insert(2);

これにより、次のリストが得られます。

    {
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "element": 5,
                "Node": {
                    "element": 6,
                    "Node": {
                        "element": 2
                    }
                }
            }
        }
    }
}

削除を行うと、正しいリストが表示されません。

myList.remove(5);

{
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "Node": {
                    "element": 6,
                    "Node": {
                        "element": 2
                    }
                }
            }
        }
    }
}

私が取得したいのは次のようなものです:

{
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "element": 6,
                "Node": {
                    "element": 2
                }
            }
        }
    }
}

これを解決する方法についてのアイデアはありますか? 前もって感謝します。

4

1 に答える 1

1

Node = Node.Nodeが次のノードを現在のノードとして割り当てていないためです。Node.Node変数に代入しているだけですNode。それで、あなたは上書きしていません。ある意味では、「読み取り権限」しか取得できません。

これを回避して参照を渡す利点を得るには、変数が参照しているオブジェクトのプロパティを変更します。そうすれば、いわば権限の読み取りと変更を行うことができます。

コードで何が起こったのかを説明する短い例:

//so we create an object
var foo = {}
  , bar;

//and assign a baz property carrying bam
foo.baz = 'bam';

//we reference foo.baz with bar
bar = foo.baz;

//so we expect that bar is bam
console.log(bar); //bam

//however, in this operation, we merely assign a value boom to bar
//and not modify foo.baz
bar = 'boom';

//with bar modified in that way, foo.baz remains as bam
console.log(bar); //boom
console.log(foo.baz) //bam?

代わりに、コードを取り除いたバージョンの単純化されたアプローチを次に示します。

var List = function () {
    this.head = {}
};
List.prototype = {
    insert: function (element) {
        var node = this.head;
        while (node.next) {
            node = node.next
        }
        node.next = {
            "element": element
        };
    },
    remove: function (element) {

        //so we start with head
        var node = this.head;

        //basically, we assign node as the next node
        //that way, we'll be operating on next instead of node
        //so we can modify the values

        //while the next isn't the one we are after
        while (node.next.element != element) {
            //point to the next
            node = node.next;
        }

        //when we get our target, delete it
        delete node.next.element;

        //we don't assign to the variable node, but to a property
        //of the object node is referencing to
        node.next = node.next.next;
    }
};

余談ですが、変数、プロパティなどには詳細な名前を付けてください。

于 2013-01-28T13:30:59.460 に答える