删除时出现Javascript链表问题
Javascript linked-list problems with deleting
有一段时间我一直在为此头疼。我不明白为什么该节点不会从我的链接列表中删除。我有一个链接列表,其中存储我浏览并渲染的图像。问题是它们仍在渲染而未被删除。我的代码有问题吗?对于javascript中的所有其他类型的链表,我的代码似乎都是一样的。
edit添加完整代码,因为它可能很有用:
var object_action_holder = function () {
this.skill_id = 0;
this.skill_type = 0;
this.image_src = 0;
this.x_pos = 0;
this.y_pos = 0;
this.turn_off = 0;
this._head = null;
};
object_action_holder.prototype = {
add: function ( skill_id , skill_type , image_src , x_pos , y_pos ) {
var node = {
skill_id:skill_id,
skill_type:skill_type,
image_src:image_src,
x_pos:x_pos,
y_pos:y_pos,
next:null
},
current;
if (this._head === null) {
this._head = node;
} else {
current = this._head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.skill_id = skill_id;
this.skill_type = skill_type;
this.image_src = image_src;
this.x_pos = x_pos;
this.y_pos = y_pos;
},
remove_node: function ( skill_id ) {
var current = this._head, previous;
if (skill_id != null && current != null ) {
while ( current.skill_id != skill_id ) {
previous = current;
current = current.next;
}
if ( current.skill_id == skill_id )
console.log('found the skill_id');
if (current != null) {
if ( current.next != null ) {
previous.next = current.next;
return current;
}else {
previous = null;
current = null;
return current;
}
}
}
return null;
},
get_action_holder: function () {
var current = this._head;
var object_array = [];
var i = 0;
while (current != null) {
object_array[i] = current;
i++;
current = current.next;
}
return object_array;
},
}
渲染
var action_image = main.action_holder.get_action_holder();
for(var i = 0; i < action_image.length; i++) {
main.game_handle.drawImage ( action_image[i].image_src , ( action_image[i].x_pos * 16 ) + main.player_x - ( main.game_x_pos * 16 ) , ( action_image[i].y_pos * 16 ) + main.player_y - ( main.game_y_pos * 16 ) );
if ( action_image[i].turn_off == true )
delete main.action_holder.remove_node(action_image[i].skill_id);
}
试试这个:
if (current != null) {
if (previous) {
previous.next = current.next;
}
if (current.next) {
current.next.previous = previous;
}
if (current == this._head) { // if the first node is removed, reset head to the next node
this._head = current.next;
}
return current;
}
内部add_node
方法:
if (this._head === null) {
this._head = node;
} else {
current = this._head;
while (current.next) {
current = current.next;
}
if (current != node) { // avoid circular reference
current.next = node;
node.previous = current; // set previous of the new node
}
}
测试
相关文章:
- 删除时出现Javascript链表问题
- JSP AJAX jQuery填充表问题
- highcharts IE8重绘图表问题
- 链接问题:下一个字符包含在<a>TinyMce ui TinyMce中的标签
- jquery对话框可见时自动下载链接问题
- 多个脚本导致链接问题?Javascript、CSS、HTML、Jquery
- JS中的专用链表
- 如何将 hasNext() 和 hasPrevious() 方法用于 Node 的“智能列表”链表模块
- 没有选项的表问题
- AngularJS:http 链调用问题和 NodeJS:在多个 http 调用中使用变量的问题
- 转.js目录链接问题
- 航点JS和锚链接问题
- 灯箱外部链接问题
- JavaScript:用于查找链表节点的递归函数返回错误的节点
- JavaScript 如何处理传递给与这个链表示例相关的函数的对象
- 字体在Safari上出现了很棒的链接问题,适用于Chrome和Firefox
- Javascript/jQuery注册表问题
- Jquerymobile面板链接问题
- IOS上ionic应用程序中的外部链接问题
- 如何在javascript中实现函数调用的任意链表