Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LinkedList上调用Remove后的C#LinkedListNode引用_C#_Reference_Linked List - Fatal编程技术网

在LinkedList上调用Remove后的C#LinkedListNode引用

在LinkedList上调用Remove后的C#LinkedListNode引用,c#,reference,linked-list,C#,Reference,Linked List,我的问题:您好,方法-LinkedList.Remove(LinkedListNode n)是否更改LinkedList中其他元素的引用 应用程序有点复杂,所以至少我会尝试解释我是如何来到这里的,但可能会让人困惑 我有一个存储引用(LinkedListNodes)的程序 LinkedList)导入另一个iterable类。在某种程度上, 应用程序开始使用删除这些LinkedListNodes 方法移除(LinkedListNode节点)并从中删除此元素 存储这些引用的类。它可以运行一段时间,但是

我的问题:您好,方法-LinkedList.Remove(LinkedListNode n)是否更改LinkedList中其他元素的引用

应用程序有点复杂,所以至少我会尝试解释我是如何来到这里的,但可能会让人困惑

我有一个存储引用(LinkedListNodes)的程序 LinkedList)导入另一个iterable类。在某种程度上, 应用程序开始使用删除这些LinkedListNodes 方法移除(LinkedListNode节点)并从中删除此元素 存储这些引用的类。它可以运行一段时间,但是 在某种程度上,它会在我的课堂上失去一个参考,我会 当我想调用时,空引用(在myNode中) AddAfter(myNode,value)错误:“LinkedList节点 不属于当前LinkedList”

编辑:

我在翻译笔记等等。。。 所以我使用BinarySearchTree进行快速搜索,使用LinkedList进行正常迭代,使用队列删除旧元素

这是在我的树类中插入的:

 public Node Insert(DictionaryPair dictionaryPair, LinkedList<DictionaryPair> dictionary)
    {
        Node currentNode = nodeRoot;
        while (true)
        {
            if (currentNode == null)    //if i inserting 1st element
            {
                nodeRoot = new Node();   //creating node(root)
                dictionary.AddFirst(dictionaryPair);    //inserting into Linked list on 1st place
                nodeRoot.dictionaryNode = dictionary.First;  //and taking a reference
                return nodeRoot;  //end while
            }
            else if (currentNode.dictionaryNode.Value.CompareTo(dictionaryPair) >= 0)
            {   //sending element into left
                if (currentNode.left == null)   // and is empty
                {
                    currentNode.left = new Node();  //creating new node
                    currentNode.left.myParent = currentNode;    //reference to parent
                    currentNode.left.dictionaryNode = dictionary.AddBefore(currentNode.dictionaryNode, dictionaryPair);
                    //and inserting into dictionary (Before) current node and save the refence on it to left
                    return currentNode.left;  //end while
                }
                else
                {   //or shift to left
                    currentNode = currentNode.left;
                }
            }
            else
            {   //sending element into right
                if (currentNode.right == null)   // is null
                {
                    currentNode.right = new Node();  //create new node
                    currentNode.right.myParent = currentNode;   //reference on parent
                    currentNode.right.dictionaryNode = dictionary.AddAfter(currentNode.dictionaryNode, dictionaryPair);
                    //and insert into dictionary (After) current node and save  the refence on it to right
                    return currentNode.right;  //endwhile
                }
                else
                {   //or shift to right side
                    currentNode = currentNode.right;
                }
            }
        }            
    }
[…]方法LinkedList.Remove(LinkedListNode n)是否更改LinkedList中其他[s]元素的引用

当我们查看
LinkedList.Remove
方法的属性时,我们发现框架除了调整它们的
prev
next
指针之外,不会与其他元素发生冲突(以缩小删除所造成的间隙,并根据链表原则的定义)

除了边境案件外,它只是

node.next.prev = node.prev;
node.prev.next = node.next;
其他元素的对象(
内部数据结构中的项
)不会被
删除
操作修改。
Remove
操作本身所针对的对象也不会直接受到影响。当节点从列表中分离时,如果没有其他活动对象保留引用,则该节点有资格进行垃圾收集

您看到的例外情况是:

如果此验证在
AddAfter
中失败,则可能意味着:

  • 调用代码试图引用一个根本没有附加到任何
    LinkedList
    的现有节点,例如以前从列表中删除的节点。在您发布的代码中,这将是
    currentNode.dictionaryNode
    ,我将重点放在调试时分配的行上
  • 调用代码试图引用属于
    LinkedList
    的另一个实例的现有节点

我在BinarySearchTree中发现了DeleteMe方法的实现错误。我没有更改在找到的currentNode下的节点的父引用。但是谢谢你的帮助。祝您度过愉快的一天…

在您的问题中包含一个最小的、完整的代码示例,这样我们就可以看到您正在做什么以及可能出现的问题。“运行良好一段时间”通常是并行性出错的指标,或者是算法中的缺陷。您的问题非常模糊,您的应用程序描述不足以让其他人帮助您。例如,你说的“元素”是指“节点”还是其他更广泛的东西?显然,删除节点时,
节点.Previous
节点.Next
列表.First
列表.Last
的引用可能都会更改。听起来您的问题是这个其他的iterable类,或者至少是您的应用程序如何使用它。。
public LinkedListNode<DictionaryPair> DeleteMe()
    { 
        LinkedListNode<DictionaryPair> deletedNode = this.dictionaryNode;
        if (this.left == null && this.right == null)
        {   //Delete leaf
            if(myParent.left == this)
            {
                myParent.left = null;
            }
            else // else if(myParent.right == this)
            {
                myParent.right = null;
            }
        }
        else if (this.left != null && this.right == null)
        {
            this.right = this.left.right;
            this.dictionaryNode = this.left.dictionaryNode;
            this.left = this.left.left;               

        }
        else if (this.left == null && this.right != null)
        {
            this.left = this.right.left;                
            this.dictionaryNode = this.right.dictionaryNode;
            this.right = this.right.right;
        }
        else
        {   //on left and right are tries
            Node currentNode = this.left; //throught the left side
            bool oneCycle = false;  //possibility of not iterating once thought the left side into the right (so it would be left)
            while (currentNode.right != null)
            {    //searching element most to the right
                currentNode = currentNode.right;
                oneCycle = true;
            }
            if (currentNode.left == null)
            {   //i am leaf
                if (oneCycle)
                {
                    currentNode.myParent.right = null;   //deleting refence on me
                    this.dictionaryNode = currentNode.dictionaryNode;   //and change a value
                }
                else
                {
                    currentNode.myParent.left = null;   //deleting refence on me
                    this.dictionaryNode = currentNode.dictionaryNode;   //and change a value
                }
            }
            else
            {   //im not leaf
                if (oneCycle)
                {
                    currentNode.myParent.right = currentNode.left;  //change refence on my tree
                    this.dictionaryNode = currentNode.dictionaryNode;   //and change value
                }
                else
                {
                    currentNode.myParent.left = currentNode.left;  //change refence on my tree
                    this.dictionaryNode = currentNode.dictionaryNode;   //and change value
                }
            }             
        }
        return deletedNode;
    }
    private LinkedList<DictionaryPair> data;    //for iterating search
    private Tree binarySearchTree;  //for quick search
    private Queue<Node> queue;   //references on added nodes 
    //in binarySearchTree... they are ready to be deleted
    private int maxCount;
    private bool maxCountReached;
public void Insert(DictionaryPair input)
    {
        if (!maxCountReached)
        {
            if (queue.Count() >= maxCount)
            {
                maxCountReached = true;
            }
        }
        if (maxCountReached)
        {
            data.Remove(queue.Dequeue().DeleteMe());
        }
        queue.Enqueue(binarySearchTree.Insert(input, data));
    }
node.next.prev = node.prev;
node.prev.next = node.next;
if ( node.list != this) {
    throw new InvalidOperationException(SR.GetString(SR.ExternalLinkedListNode));
}