C# 这是遍历 .NET LinkedList 并删除元素的好方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2349589/
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-07 01:40:29  来源:igfitidea点击:

Is this a good way to iterate through a .NET LinkedList and remove elements?

c#.netiterator

提问by they changed my name

I'm thinking of doing the following:

我正在考虑执行以下操作:

for(LinkedListNode<MyClass> it = myCollection.First; it != null; it = it.Next)
{
    if(it.Value.removalCondition == true)
        it.Value = null;
}

What I'm wondering is: if simply pointing the it.Valueto null actually gets rid of it.

我想知道的是:如果简单地指向it.Valuenull 实际上摆脱它。

采纳答案by Oli

Surely (with a linked list) you need to change the link.

当然(使用链表)您需要更改链接。

Eg, if you want to remove B from the LL A-B-C, you need to change A's link to B to C.

例如,如果要从 LL ABC 中删除 B,则需要将 A 与 B 的链接更改为 C。

I'll admit I'm not familiar with the .NET implementation of linked lists but hopefully that's a start for you.

我承认我不熟悉链表的 .NET 实现,但希望这对您来说是一个开始。

回答by Vlad

You are changing the value pointed to by a LinkedListNode; beware that your list will contain a hole (emptynode) now.

您正在更改 a 指向的值LinkedListNode;请注意,您的列表现在将包含一个洞(节点)。

Instead of A - B - Cyou are going to have A - null - C, if you "delete" B. Is that what you want to achieve?

而不是A - B - C你将要拥有A - null - C,如果你“删除” B。这就是你想要达到的目标吗?

回答by JDMX

I assume something like this is required

我认为需要这样的东西

for ( LinkedListNode<MyClass> it = myCollection.First; it != null; it = it.Next ) {
  if ( it.Value.removalCondition == true ) {
    if ( it.Previous != null && it.Next != null ) {
      it.Next.Previous = it.Previous;
      it.Previous.Next = it.Next;
    } else if ( it.Previous != null )
      it.Previous.Next = it.Next;
    } else if ( it.Next != null )
      it.Next.Previous = it.Previous;
    it.Value = null;
  }
}

回答by Mark Milbourne

If you can convert to using List<> rather than LinkedList<> then you can use the RemoveAll() operation. Pass an anonymous delegate like this;

如果您可以转换为使用 List<> 而不是 LinkedList<> 那么您可以使用 RemoveAll() 操作。像这样传递一个匿名委托;

List<string> list = new List<string>()
{
    "Fred","Joe","John"
};

list.RemoveAll((string val) =>
{
    return (0 == val.CompareTo("Fred"));
});

All this is using Linq extensions.

所有这些都是使用 Linq 扩展。

If you can't convert to using a list then you can use the ToList<>() method to convert it. But you'll then have to do some clear and insertion operations. Like this;

如果您不能转换为使用列表,那么您可以使用 ToList<>() 方法来转换它。但是您随后必须执行一些清除和插入操作。像这样;

LinkedList<string> str = new LinkedList<string>();
str.AddLast("Fred");
str.AddLast("Joe");
str.AddLast("John");

List<string> ls = str.ToList();
ls.RemoveAll((string val) => val.CompareTo("Fred") == 0);
str.Clear();
ls.ForEach((string val) => str.AddLast(val));

If all this still isn't palatable then try doing a copy of the LinkedList like this;

如果所有这些仍然不好吃,那么尝试像这样复制 LinkedList;

LinkedList<string> str = new LinkedList<string>();
str.AddLast("Fred");
str.AddLast("Joe");
str.AddLast("John");

LinkedList<string> strCopy = new LinkedList<string>(str);
str.Clear();
foreach (var val in strCopy)
{
    if (0 != val.CompareTo("Fred"))
    {
        str.AddLast(val);
    }
}

I hope that helps.

我希望这有帮助。

回答by Davit Tvildiani

As far as I understood You want to iterate in linkedlist with for cycle which olso contains null -s , so you can use folowing :

据我了解,您想使用 for 循环在链表中进行迭代,其中 olso 包含 null -s ,因此您可以使用以下内容:

for (LinkedListNode<string> node = a.First; node != a.Last.Next; node = node.Next)
{
               // do something here 

}

回答by ILIA BROUDNO

Setting the it.Value to null will not remove the node from the list Here is one way:

将 it.Value 设置为 null 不会从列表中删除节点这是一种方法:

    for(LinkedListNode<MyClass> it = myCollection.First; it != null; )
    {
        LinkedListNode<MyClass> next = it.Next;
        if(it.Value.removalCondition == true)
              myCollection.Remove(it); // as a side effect it.Next == null

        it = next;
    }