简体   繁体   English

通过链表中的索引删除元素

[英]Removing an element by index in a linked list

A method for removing an element from a linked list has been implemented:已实现从链表中删除元素的方法:

public void remove(T e) {
 
    Node<T> node = first;
    Node<T> prevNode = null;
    while(node != null){
        if(e.equals(node)){
            if(prevNode  == null) {
                first = node.next;
            }
            else {
                prevNode.next = node.next;
            }
            size--;
        }
        else {
            prevNode = node;
        }
        node = node.next;
    }
}

How to correctly implement deleting an element by index?如何正确实现按索引删除元素? Using the capabilities of the remove method.使用 remove 方法的功能。

public void removeByIndex(int i) {
    remove(i);
}
remove(get(i));

This is not the most efficient solution, but a simple one which uses the remove(T) method.这不是最有效的解决方案,而是使用remove(T)方法的简单解决方案。 You call it with get(i) as the object to be removed - which is the element at the specified index.您使用get(i)调用它作为要删除的 object - 这是指定索引处的元素。

Note: This solution has some issues if the list has duplicate values, but in that case you shouldn't use the remove(T) method anyway.注意:如果列表具有重复值,此解决方案会出现一些问题,但在这种情况下,您无论如何都不应该使用remove(T)方法。 If you want it to be safe, iterate to the specified index:如果您希望它是安全的,请迭代到指定的索引:

    Node<T> node = first;       
    for(int i=0;i<index;i++){
        prevNode=node;
        node=node.next;
    }

and do this:并这样做:

node.prev.next=node.next;
node.next.prev=node.prev;
size--;

Of course, this is just a rough implementation.当然,这只是一个粗略的实现。 To ensure full compability, you should check if the index is valid and use the unlink(Node) method of LinkedList.为确保完全兼容,您应该检查索引是否有效并使用 LinkedList 的unlink(Node)方法。

The LinkedList also has an implementation for the remove(int) method: LinkedList 还有一个remove(int)方法的实现:

checkElementIndex(index);
return unlink(node(index));

Something like this should work:像这样的东西应该工作:

public void remove(int i) {
    if (i >= size || i < 0) {
        throw new ArrayIndexOutOfBoundsException();
    }
    Node<T> remove;
    if (i == 0) {
        remove = first;
        first = first.next;
        first.prev = null; // <- For double linked list
    } else {
        Node<T> node = first;
        for (int j = 0; j < i - 1; ++j) {
            node = node.next;
        }
        remove = node.next;
        if (i == size - 1) {
            node.next = null;
        } else {
            node.next.next.prev = node; // <- For double linked list
            node.next = node.next.next;
        }
    }
    // Clear links from removed Node
    remove.next = null;
    remove.prev = null; // <- For double linked list
    size--;
}

Find the node before position i.找到 position i 之前的节点。 Point that node to the "over next" node.将该节点指向“下一个”节点。

Edit The original code example was a rough sketch at best.编辑原始代码示例充其量只是一个粗略的草图。 Updated with a more complete version.更新为更完整的版本。

Edit #2编辑#2

Slight improvements:轻微改进:

  • Clears links from the removed node清除已移除节点中的链接
  • Also handles double linked lists还处理双链表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM