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 回答