2012-09-06 162 views
1

一段時間以來,我一直在反對這個問題。我無法弄清楚爲什麼節點不會從我的鏈表中刪除。我有一個鏈接列表,存儲我經歷並呈現每一個的圖像。問題是他們仍然被渲染,並沒有被刪除。我的代碼有問題嗎?我的代碼似乎在JavaScript中的所有其他類型的鏈表。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); 
     } 
+0

在JS你爲什麼這樣做鏈表的時候,你可以做陣列本身?另外,你的意思是什麼?我的代碼中沒有看到任何DOM相關的東西?有沒有渲染功能的地方不在這裏? – Joseph

+0

@JosephtheDreamer我正在使用lls,因爲我正在從websocket連接接收信息並將數據存儲到lls中。然後渲染數據,然後刪除數據。還要檢查其他數據以確保其可以呈現。如果你有任何建議,會更好,我是耳朵。我對js編程真的很陌生。 – User

+0

@JosephtheDreamer添加完整的代碼。對不起,牆上的文字! – User

回答

2

試試這個:

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

+0

當它試圖刪除第一個節點時,它不起作用,但在正確刪除之後添加了其他任何節點。 – User

+0

它拋出了什麼錯誤? – timidboy

+0

不會拋出任何錯誤。 – User