i am practicing javascript, and i am creating a single linked list like example, but i'm getting at the last node [object] instead of adding a new node to the list.
This is my code right now, i hope you can help me, maybe my mistake is at the addNode method.
function Node(value) {
this.value = value;
this.nextNode = null;
}
Node.prototype.addNode = function(newNode) {
function append(node) {
if (node.nextNode == null) {
node.nextNode = newNode;
}
else {
return append(node.nextNode);
}
}
return append(this);
}
var firstNode = new Node(5);
var node2 = new Node(3);
var node3 = new Node(4);
var node4 = new Node(8);
var node5 = new Node(1);
console.log(firstNode);
firstNode.addNode(node2);
console.log(firstNode);
firstNode.addNode(node3);
console.log(firstNode);
firstNode.addNode(node4);
console.log(firstNode);
firstNode.addNode(node5);
console.log(firstNode);
this was printed with Node.js
Node { value: 5, nextNode: null } Node { value: 5, nextNode: Node { value: 3, nextNode: null } } Node { value: 5, nextNode: Node { value: 3, nextNode: Node { value: 4, nextNode: null } } } Node {
value: 5, nextNode: Node { value: 3, nextNode: Node { value: 4, nextNode: [Object] } } } Node { value: 5, nextNode: Node { value: 3, nextNode: Node { value: 4, nextNode: [Object] } } }
Use util.inspect() to investigate the full linked list.
var util = require('util');
console.log(util.inspect(myObject, {showHidden: false, depth: null}));
# alternative shortcut
console.log(util.inspect(myObject, false, null));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments