they changed my name
they changed my name

Reputation: 491

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

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.Value to null actually gets rid of it.

Upvotes: 7

Views: 26815

Answers (6)

ILIA BROUDNO
ILIA BROUDNO

Reputation: 1893

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

    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;
    }

Upvotes: 10

Davit Tvildiani
Davit Tvildiani

Reputation: 1965

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 (LinkedListNode<string> node = a.First; node != a.Last.Next; node = node.Next)
{
               // do something here 

}

Upvotes: 0

Mark Milbourne
Mark Milbourne

Reputation: 141

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

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

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

All this is using Linq extensions.

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;

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<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.

Upvotes: 1

JDMX
JDMX

Reputation: 1509

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;
  }
}

Upvotes: 0

Vlad
Vlad

Reputation: 35594

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

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

Upvotes: 1

Oli
Oli

Reputation: 239830

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.

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

Upvotes: 1

Related Questions