【问题标题】:Javascript linked-list problems with deletingJavascript链表删除问题
【发布时间】:2012-09-06 02:32:08
【问题描述】:

一段时间以来一直在努力解决这个问题。我无法弄清楚为什么该节点不会从我的链表中删除。我有一个链接列表,用于存储我浏览并渲染每个图像的图像。问题是它们仍在被渲染而不是被移除。我的代码有问题吗?对于 javascript 中的所有其他类型的链表,我的代码似乎都相同。

编辑添加完整代码,因为它可能有用:

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);
        }

【问题讨论】:

  • 既然可以原生做数组,为什么还要在JS里做链表呢?另外,渲染是什么意思?我在您的代码中没有看到任何与 DOM 相关的内容?是否有某个地方不存在的渲染功能?
  • @JosephtheDreamer 我使用 lls 是因为我从 websocket 连接接收信息并将数据存储到 lls 中。然后渲染数据,然后删除数据。还要检查其他数据的 ll 以确保它可以渲染。如果您有任何更好的建议,我会全力以赴。我对 js 编程真的很陌生。
  • @JosephtheDreamer 添加了完整代码。对不起,文字墙!

标签: javascript linked-list


【解决方案1】:

试试这个:

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
    }
}

Test

【讨论】:

  • 当它试图删除第一个节点时它不起作用,但在此之后添加的任何其他节点都会正确删除。
  • 修改了add_node中的一些代码,但不确定是否修复。
  • 我添加了它,但是它并没有修复它。 :(
  • 现在应该可以了。如果它被删除,忘记重置列表的头部。
  • 男人!非常感谢。我永远都不知道该怎么做! :-)。另一个问题,如果你能在评论中告诉我,什么类型的容器在 javascript 中最快?
猜你喜欢
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2011-05-12
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多