Reputation: 693
I am doing a simple Link List in JavaScript(I'm a newbie) and I Have the following code,
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;
}
};
When I am doing insertions it is doing fine like,
var myList = new List();
myList.insert(1);
myList.insert(5);
myList.insert(6);
myList.insert(2);
This gives me a List of,
{
"Head": {
"Node": {
"element": 1,
"Node": {
"element": 5,
"Node": {
"element": 6,
"Node": {
"element": 2
}
}
}
}
}
}
Now when I do a delete, it is not giving the right List:
myList.remove(5);
{
"Head": {
"Node": {
"element": 1,
"Node": {
"Node": {
"element": 6,
"Node": {
"element": 2
}
}
}
}
}
}
What I want to get is like this:
{
"Head": {
"Node": {
"element": 1,
"Node": {
"element": 6,
"Node": {
"element": 2
}
}
}
}
}
Any Ideas on how to solve this? Thanks in advance.
Upvotes: 0
Views: 1006
Reputation: 119867
It's because Node = Node.Node
is not assigning the next node as the current node. You are merely assigning Node.Node
to the variable Node
. With that, you are NOT overwriting. In a sense, you only get "read privileges".
To get around this and get the benefits of passing references, modify the property of the object your variable is referencing to. That way, you have read and modify privileges, so to speak.
A short example to explain your what happened in your code:
//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?
So instead, here's a simplified approach with a stripped version of the code:
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;
}
};
And just as an aside, name your variables, properties and stuff verbosely.
Upvotes: 1