Javascript链表

Javascript链表,javascript,linked-list,Javascript,Linked List,我试图写一个链表函数,可以删除节点的搜索值。如果值匹配,则删除节点并将上一个节点链接到下一个节点。我编写了伪代码,但在实现上遇到了问题。该函数称为removedSelected() 一种常见的技术是创建双向链接,其中包含指向列表中上一个和下一个对象的指针,以便于删除列表项和修补上一个对象的下一个值 但是,鉴于此列表仅在一个方向上链接,因此在遍历列表时跟踪列表中相对于被检查节点的前一个节点应允许在一次遍历中删除具有匹配值的节点 这个例子展示了如何做到这一点。(测试都是你的:-) 说明: 声明一个变

我试图写一个链表函数,可以删除节点的搜索值。如果值匹配,则删除节点并将上一个节点链接到下一个节点。我编写了伪代码,但在实现上遇到了问题。该函数称为removedSelected()


一种常见的技术是创建双向链接,其中包含指向列表中上一个和下一个对象的指针,以便于删除列表项和修补上一个对象的下一个值

但是,鉴于此列表仅在一个方向上链接,因此在遍历列表时跟踪列表中相对于被检查节点的前一个节点应允许在一次遍历中删除具有匹配值的节点

这个例子展示了如何做到这一点。(测试都是你的:-)

说明:

  • 声明一个变量“previous”,以跟踪最后一个变量的值 已访问的节点和一个变量“current”来存储 当前节点已访问
  • 检查第一个节点的值是否等于所选节点的值 搜索。如果值相等,则跳过第一个节点并 下一个节点成为第一个节点
  • 在遍历链表时,如果我们找到任何节点 包含搜索的值,我们将前一个节点的下一个链接到 当前节点的下一个元素,从而跳过当前节点

  • 首先,感谢您发布了一个完整、可用的示例。今晚我没有太多时间,但我会看看我能做些什么。@JeremyJStarcher感谢你发表评论,感谢一篇发表完整示例的帖子;)你能解释一下为什么这个代码能回答这个问题吗?代码唯一的答案是,因为他们不教解决方案。
    var LinkedList = function(){
      var list = {};
      //this should always point to the first node in the list 
      list.head = null;
      list.tail = null;
    
    
      list.addToTail = function(value){
        var newNode = Node(value);
    
        if(!list.head){
          this.head = newNode;
          this.tail = newNode;
        } else {
          this.tail.next = newNode;
          this.tail = newNode;
        }
      };
    
       list.removeSelected = function(target){
    //loop through the linked list 
        //if head value is not equal to target
        var node = this.head;
        while(node.value !== target){
    
        }
        //keep going through the list
            //if this.head === target
              //point the previous next to the next node            
      };
    
      list.indexCount = function(){
        return this.indexCount;
      }
    
      list.removeHead = function(){
        var deleted = this.head.value;
        this.head = this.head.next;
        if(this.head === null){
          this.tail = null;
        }
        return deleted;
      };
    
      list.contains = function(target){
        var node = this.head;
        while(node !== null){
          if(node.value === target){
           return true;
          }
          node = node.next;
        }
        return false; 
      };
      return list;
    };
    
    var Node = function(value){
      var node = {};
    
      node.value = value;
      node.next = null;
    
      return node;
    };
    
    var a = new LinkedList();
    a.addToTail(1);
    a.addToTail(5);
    a.addToTail(55);
    a.removeSelected(5);
    
    list.removeSelected = function( target) {
        for( var previous = null, node = this.head;
                node;
                    previous = node, node = next)
        {   var next = node.next;
            if( node.value == target)
            {   if( previous)
                {   previous.next = next;
                }
                else
                {   this.head = next;
                }
                if( !next)
                    this.tail = previous;
                node.next = null;
                break;
            }
        }
        return node;  // the node removed or null if not found.
    };
    
    var removeSelected = function(head, val) {
                        var previous = null;
                        var current = head;
                  while(current !== null){
                            if(current.val === val){
                                if(previous === null){
                                    head = current.next;
                                }else{
                                    previous.next = current.next;
                                }
                            }else{
                                previous = current;
                            }
                            current = current.next;  
                        }
                        return head;
                    };