linkedlist使用javascript问题

linkedlist使用javascript问题,javascript,jquery,Javascript,Jquery,我正在使用javascript编写linkedlist数据结构。我在特定位置插入新节点时遇到问题。我有以下代码 var node3 = { data: 4, next: null }, node2 = { data: 3, next: node3 }, node1 = { data: 1, next: node2 }, newNode = { data: 2, next: null }, head = node1, current

我正在使用javascript编写linkedlist数据结构。我在特定位置插入新节点时遇到问题。我有以下代码

var node3 = {
    data: 4,
    next: null
},
node2 = {
    data: 3,
    next: node3
},
node1 = {
    data: 1,
    next: node2
},
newNode = {
    data: 2,
    next: null
},
head = node1, current = head, pos = 3, i = 1, prev;

while (current.next) {
    if (pos === i) {
        prev.next = newNode;
        newNode.next = current;
        break;
    }
    prev = current;
    i++;
    current = current.next;
}

我最初创建了3个节点,最后创建了一个
newNode
。我想在位置3插入此
newNode
,即
pos=3
,这意味着列表结束。我上面的代码将节点插入到其他位置,但不会在末尾插入为什么以及如何解决这个问题?

当您在node3上迭代时,
current.next==null
,因此循环不会执行,因此您需要为循环提出另一个条件


此外,您还应该使用
break
而不是
return
,因为您不在函数中

您可以尝试这样动态创建链表

var Linkedlist = function() {
this.head = null;
this.tail = null;
})

}

})

})

})

})

可以按如下所示进行实例化

linkedlist = new Linkedlist();
linkedlist.insert(1);
linkedlist.insert(2);
linkedlist.insert(3);
linkedlist.insert(4);
linkedlist.remove(2);
linkedlist.largest();

对于LinkedList,插入位置不是常见的做法,因为LinkedList不维护每个节点的位置,而是维护节点之间的链接。通常我们希望在节点之前/之后插入。更常见的情况是,我们希望在列表的末尾插入。下面是在sourceNode之后插入的代码

this.insertAfter = function(sourceNode, node) {
    var curNode = head;
    while (curNode != null) {
        if (curNode === sourceNode) {
            curNode.setNext(node.setNext(curNode.getNext()));
            return;
        }
        else curNode = curNode.getNext(); 
    }
};

对于您的结构,理想情况下,insertAt函数如下所示:

var insertAt = function(head, item, index) {
    var curr = head;
    var count = 0;
    while (curr !== null) {
        if (count === index) {
            var newNode = { data: item, next: curr.next };
            curr.next = newNode;
            break;
        }
        count++;
        curr = curr.next;
    }
};

insertAt(head, 2, 3);  
让我知道这是否有效。
再看看我创建的这个LinkedList类,
insertAt
函数当前缺失,但从这个堆栈问题来看,我也计划将它添加到我的类中。



您可以添加另一个条件的示例吗?您可以尝试将其改为
current.next | | prev.next
。它将产生的区别是当
current.next
失败且
previous.next
为true时,它将位于结束节点。我可以在哪里添加
current.next | | prev.next
?参考链接-类-NPM包-完整文档-
Linkedlist.prototype.remove =  function( val ) {
var node = this.search(val);
if ( node != null ) {
    if ( node[ "next" ] === null && node[ "prev" ] === null ) {
        this.head = null;
        this.tail = null;
    } else {
        if ( node[ "key"] === this.head[ "key" ] ) {
            this.head = node[ "next" ];
            this.head[ "prev" ] = null;
        } else if ( node[ "key"] === this.tail[ "key" ]) {
            this.tail = node[ "prev" ];
            this.tail[ "next" ] = null;
        } else {
            node[ "prev" ][ "next" ] = node[ "next" ];
            node[ "next" ][ "prev" ] = node[ "prev" ];
        }
    }
    console.log( this );
} else {
    console.log("key doesnt exist");
}
Linkedlist.prototype.largest = function() {
var node = this.head;
var largest = this.head;
while ( node[ "next"] !== null ) {
    if ( node[ "key"] > largest[ "key" ] ) {
        largest = node;
    }
    node = node[ "next" ];
}
return largest;
linkedlist = new Linkedlist();
linkedlist.insert(1);
linkedlist.insert(2);
linkedlist.insert(3);
linkedlist.insert(4);
linkedlist.remove(2);
linkedlist.largest();
this.insertAfter = function(sourceNode, node) {
    var curNode = head;
    while (curNode != null) {
        if (curNode === sourceNode) {
            curNode.setNext(node.setNext(curNode.getNext()));
            return;
        }
        else curNode = curNode.getNext(); 
    }
};
var insertAt = function(head, item, index) {
    var curr = head;
    var count = 0;
    while (curr !== null) {
        if (count === index) {
            var newNode = { data: item, next: curr.next };
            curr.next = newNode;
            break;
        }
        count++;
        curr = curr.next;
    }
};

insertAt(head, 2, 3);