1

Been banging my head against this for a while. I cant figure out why the node will not remove from my linked list. I have a linked-list that stores images that I go through and render each one. Problem is they are still being rendered and not removed. Is there any problems with my code? My code seems the same for all other type of linked-lists in javascript.

edit Adding full code since it might be useful:

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

rendering

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);
        }
3
  • Why are you doing linked lists in JS when you can do arrays natively? Also, what do you mean by rendered? I don't see any DOM related stuff in your code? Is there a rendering function somewhere that isn't here? Commented Sep 6, 2012 at 2:40
  • @JosephtheDreamer I am using lls because I am receiving information from a websocket connection and storing the data into lls. And then rendering data and then removing the data. Also checking against the ll to other data to make sure its okay to render. If you have any suggestions that would be better I am all ears. I am really new to js programing. Commented Sep 6, 2012 at 2:45
  • @JosephtheDreamer Added the full code. Sorry for wall of text! Commented Sep 6, 2012 at 2:51

1 Answer 1

2

Try this:

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

Inside add_node method:

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

Sign up to request clarification or add additional context in comments.

6 Comments

When it tries to remove the first node it doesn't work, but any other node added after that properly deletes.
Modified some code in add_node, but not sure if it fixes it.
I added it, it did not fix it however. :(
Now that should do it. Forgot to reset the list's head if it is removed.
MAN! Thank you so much. I would have NEVER even knew what to do! :-). Another question if you could please let me know in a comment, what type of container is best the fastest in javascript?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.