Java LinkedList 移除方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/270950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:19:13  来源:igfitidea点击:

LinkedList remove method

javadata-structureslinked-list

提问by twodayslate

What is a doubly linked list's remove method?

什么是双向链表的删除方法?

采纳答案by CMS

The same algorithm that Bill the Lizardsaid, but in a graphical way :-)

蜥蜴比尔所说的算法相同,但以图形方式:-)

Remove From Linked List
(source: jaffasoft.co.uk)

从链表中删除
(来源:jaffasoft.co.uk

回答by Bill the Lizard

The general algorithm is as follows:

一般算法如下:

  • Find the node to remove.
  • node.previous.next = node.next
  • node.next.previous = node.previous
  • node.previous = null
  • node.next = null
  • Dispose of node if you're in a non-GC environment
  • 找到要移除的节点。
  • node.previous.next = node.next
  • node.next.previous = node.previous
  • node.previous = 空
  • node.next = null
  • 如果您处于非 GC 环境中,请处理节点

You have to check the previous and next nodes for null to see if you're removing the head or the tail, but those are the easy cases.

您必须检查前一个和下一个节点是否为空,以查看您是删除头部还是尾部,但这些都是简单的情况。

回答by kasperjj

Are you asking for the name of a method in the api? That answer would simply be remove, assuming you are asking about java.util.LinkedList which is in fact a double linked list.

您是在 api 中询问方法的名称吗?假设您问的是 java.util.LinkedList,它实际上是一个双链表,那么该答案将被简单地删除。

...or are you asking about what the name of the algorithm to remove an element from that type of data structure is called? Well.. the answer for that would also be to remove an element. Now for the actual algorithm to do it... it's really just a matter of changing the next pointer in the previous node and the last pointer in the next node. However, if you are using your data structure from multiple threads, you will need to either synchronize the remove method, or do the removal steps in an order that will make sense for your usage pattern for the data structure.

...或者您是在问从该类型的数据结构中删除元素的算法的名称是什么?嗯.. 答案也是删除一个元素。现在让实际的算法来做......它实际上只是改变前一个节点中的下一个指针和下一个节点中的最后一个指针的问题。但是,如果您从多个线程使用数据结构,则需要同步 remove 方法,或者按照对数据结构的使用模式有意义的顺序执行删除步骤。

回答by albertein

public void remove ()
{
    if (getPreviousNode () != null)
        getPreviousNode ().setNextNode (getNextNode ());
    if (getNextNode () != null)
        getNextNode ().setPreviousNode (getPreviousNode ());    
}

回答by twodayslate

What about the current pointer pointer? You have to move crnt to the next node. http://pastebin.ca/1249635

当前指针指针呢?您必须将 crnt 移动到下一个节点。 http://pastebin.ca/1249635

回答by ashokgelal

Doubly Linked List Implementation Remove Methods (from my second programming assignment):

双向链表实现删除方法(来自我的第二个编程任务):

public void remove(int index) {
    if(index<0 || index>size())
    throw new IndexOutOfBoundsException("Index out of bounds. Can't remove a node. No node exists at the specified index");
    if(size()==0) {
        throw new NullPointerException("Empty list");
    }
    if(!isEmpty()) {
        Node current;
        //starting next one to our head
        current = head.next;
        for(int i=0;i<index;i++) {
            current = current.next;
        }
        current.previous.next = current.next;
        current.next.previous = current.previous;
        numOfNodes--;
        sizeChangeCount++;
    }
}

public boolean remove(T o) {
    Node current = head;
    for(int i=0;i<size();i++) {
        current=current.next;
        if(current.data.equals(o)) {
            current.previous.next = current.next;
            current.next.previous = current.previous;
            numOfNodes--;
            sizeChangeCount++;
            return true;
        }           
    }
    return false;
}