链表用其他语言非常好实现,但对于js这种弱类型的语言 以其他语言的思路去思考行不通. 以下是困我半天的代码
//create class [DirNode] start
diqye.html5.DirNode = function(value) {
//public field
this.id = new Date().getTime();
console.log("the id in class [DirNode] that value is " + value + ":" + this.id);
this.value = value;
this.next = null;
}
//create class [GpsDirManager] start
diqye.html5.GpsDirManager = function() {
//private field
this._node = null;
this._head = this._node;
}
diqye.html5.GpsDirManager.prototype.getHead = function() {
return this._head;
}
diqye.html5.GpsDirManager.prototype.add = function(value) {
if(this._node == null) {
if(value.constructor == diqye.html5.DirNode) {
this._node = value;
console.log("enter first init the value is " + this._node.value);
}else{
this._node = new diqye.html5.DirNode(value);
}
return this;
}
if(value.constructor == diqye.html5.DirNode) {
this._node.next = value;
}else{
this._node.next = new diqye.html5.DirNode(value);
}
this._node = this._node.next;
console.log("enter other init the value is " + this._node.value);
return this;
}
diqye.html5.GpsDirManager.prototype.tapNode = function(id){
var tempNode = this._head;
while(tempNode != null) {
if(tempNode.id == id) {
tempNode.next = null;
return true;;
}
tempNode = tempNode.next.constructor;
}
return false;
}
diqye.html5.GpsDirManager.prototype.log = function() {
var logs = new Array();
var i = 0;
var tempNode = this._head.constructor;
console.log("enter log method");
while(tempNode != null) {
console.log("enter loop [while]");
logs[i] = tempNode.value;
tempNode = tempNode.next;
}
console.log(logs.join("=>"));
}
上述代码是根本实现不了的,据我调查的原因是Js中自定义对象不存在指针的:
比如:
var o1 = new Object();
var 02 = o1; //这里o2是得到o1的原型
o1 = "sdfasd"; //这里o1改变其值 而o2是不会改变的
这就导致链表连接不起来的原因了,后来我也分别实验var 02 = o1.constructor; 和var 02 = o1.prototype都不行的 存在各种问题
用Json 数据格式实现链表,但只能实现增加,在删除却实现不了,,,,,,,,,
代码如下:
//create class [GpsDirManager] start
diqye.html5.GpsDirManager = function() {
//private field
this._head = null;
}
diqye.html5.GpsDirManager.prototype.getHead = function() {
return this._head;
}
diqye.html5.GpsDirManager.prototype.add = function(value) {
this._head = {"id":((this._head==null) ? 1:this._head.id+1),
"value":value,
"next":this._head||null};
return this;//这样开销难免很大
}
diqye.html5.GpsDirManager.prototype.tapNode = function(id){
var tempNode = this._head; //这里用的是_head的副本 (只是我个人的判断请看下面实验)
while(tempNode != null) {
if(tempNode.id == id) {
tempNode.next = null; //这些值根本没有在_head下改动
return true;
}
tempNode = tempNode.next;
}
return false;
}
diqye.html5.GpsDirManager.prototype.log = function() {//这个方法是可以的
var logs = new Array();
var tempNode = this._head;
while(tempNode != null) {
console.log("enter loop [while]");
logs.push(tempNode.value);
tempNode = tempNode.next;
}
console.log(logs.join("=>"));
}
希望高手能指点一二
刚进行实验了一下还是可以实现删除的,
var manager = new diqye.html5.GpsDirManager();
manager
.add("a")
.add("b")
.add("c")
.add("d");
manager.log();
console.log(manager.tapNode(3));
manager.log();
结果如下:
但这样就把我之前的理论给推翻了,但至少证明了一点,json格式传递的不是副本
等待解释中,,,,,,,,,,,,,,,