在LinkedList上调用Remove后的C#LinkedListNode引用
我的问题:您好,方法-LinkedList.Remove(LinkedListNode n)是否更改LinkedList中其他元素的引用 应用程序有点复杂,所以至少我会尝试解释我是如何来到这里的,但可能会让人困惑 我有一个存储引用(LinkedListNodes)的程序 LinkedList)导入另一个iterable类。在某种程度上, 应用程序开始使用删除这些LinkedListNodes 方法移除(LinkedListNode节点)并从中删除此元素 存储这些引用的类。它可以运行一段时间,但是 在某种程度上,它会在我的课堂上失去一个参考,我会 当我想调用时,空引用(在myNode中) AddAfter(myNode,value)错误:“LinkedList节点 不属于当前LinkedList” 编辑: 我在翻译笔记等等。。。 所以我使用BinarySearchTree进行快速搜索,使用LinkedList进行正常迭代,使用队列删除旧元素 这是在我的树类中插入的:在LinkedList上调用Remove后的C#LinkedListNode引用,c#,reference,linked-list,C#,Reference,Linked List,我的问题:您好,方法-LinkedList.Remove(LinkedListNode n)是否更改LinkedList中其他元素的引用 应用程序有点复杂,所以至少我会尝试解释我是如何来到这里的,但可能会让人困惑 我有一个存储引用(LinkedListNodes)的程序 LinkedList)导入另一个iterable类。在某种程度上, 应用程序开始使用删除这些LinkedListNodes 方法移除(LinkedListNode节点)并从中删除此元素 存储这些引用的类。它可以运行一段时间,但是
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
节点.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));
}