简体   繁体   English

删除/撤消双向链表中的节点

[英]Removing/undoing a node in doubly linked list

I know the time complexity for removing a node in a doubly linked list is O(1). 我知道删除双链表中的节点的时间复杂度为O(1)。 I've seen a lot of examples of code where people use a loop in remove method in doubly linked list and still the time complexity is O(1). 我看到了很多代码示例,其中人们在双链表的remove方法中使用循环,但时间复杂度仍为O(1)。 I've written a code which undoes, ie removes a node in a doubly linked list but my question is: would my code undo/remove a node in a O(1) in undo() method as I call getNode() method that contains a loop? 我已经编写了撤消代码,即删除了双向链表中的节点,但是我的问题是:我的代码会撤消/删除undo()方法中O(1)中的节点,因为我调用了getNode()方法,包含一个循环? I'd appreciate explanations! 我会很高兴的解释! Thanks in advance! 提前致谢!

private Node <T> getNode( int idx ){
        return getNode( idx, 0, size( ) - 1 );
    }
private Node<T> getNode( int idx, int lower, int upper ){
        Node<T> p;
        if( idx < lower || idx > upper ){
            throw new IndexOutOfBoundsException( "getNode index: " + idx + "; size: " + size( ) );
        }
        if( idx < size( ) / 2 )
        {
            p = beginMarker.next;
            for( int i = 0; i < idx; i++ )
                p = p.next;            
        }
        else
        {
            p = endMarker;
            for( int i = size( ); i > idx; i-- )
                p = p.prev;
        } 
        return p;
    }
public void undo(){
        if(isEmpty()){
            throw new RuntimeException("Undo history is empty");
        }
        else{
            T data = undoStackDatas.topAndPop();
            int index = undoStackIndexes.topAndPop();
            //Push data and index back into redo stacks
            redoStackDatas.push(data);
            redoStackIndexes.push(index);

            Node<T> obj = getNode(index);
            if(obj == beginMarker){
                beginMarker = obj.next;
            }
            else{
                obj.prev.next = obj.next; 
            }

            if(obj == endMarker){
                endMarker = obj.prev;
            }
            else{
                obj.next.prev = obj.prev;
            }
            theSize--;
            modCount--;
        }
    }

Removal of a node from a double linked list is O(1) if you are given the node you must delete. 如果您得到必须删除的节点,则从双链表中删除该节点的操作是O(1)。

But in that case that you must search for the node, then your complexity will change due to the searching. 但是在这种情况下,您必须搜索该节点,那么复杂度将因搜索而改变。 In this case your complexity will change to O(n). 在这种情况下,您的复杂度将变为O(n)。

you will have added time-complexity of for loop because when you trace the execution path of the code, you need to traverse those for loops to fully execute your for loop. 您将添加for循环的时间复杂性,因为当您跟踪代码的执行路径时,需要遍历那些for循环才能完全执行for循环。 you just cant decrease the time complexity using a method call, because during your program execution all the statements that is executed is counted. 您不能使用方法调用来减少时间复杂度,因为在程序执行期间,所有执行的语句都会被计数。

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

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