How do I extract a LinkedListNode<T> from a LinkedList<T>
Asked Answered
A

1

9

I have a LinkedList that I am using to track consecutive numbers that are sent to this class. (I ultimately want to find the missing numbers).

I now need to use the method ranges.AddAfter(recentNode,someNewNode), but I can't do it by casting. What am I missing?

class ContiguousData 
{
    LinkedList<ContiguousDataValue> ranges = new LinkedList<ContiguousDataValue>();

    public void AddValue(int val)
    {
        LinkedListNode<ContiguousDataValue> recentNode = null;

        foreach (var range in ranges)
        {
            if (val > range.UpperInt)
            {
                if (val == range.UpperInt + 1)
                    range.UpperInt = val;
                else
                {
                    if (recentNode == null)
                        ranges.AddFirst(new ContiguousDataValue() { UpperInt = val, LowerInt = val });
                    else
                        ranges.AddAfter(recentNode, new ContiguousDataValue() { UpperInt = val, LowerInt = val });
                }
                break;
            }
            else if (val < range.LowerInt)
            {
                if (val == range.LowerInt - 1)
                    range.LowerInt = val;
                else
                {
                    // do  more logic (incomplete)
                }
            }

           // Compiler error
            recentNode = (LinkedListNode<ContiguousDataValue>)range;
        }

        if (ranges.Count == 0)
        {
            ranges.AddFirst(new ContiguousDataValue() { UpperInt = val, LowerInt = val });
            return;
        }
    }

    internal class ContiguousDataValue
    {
        public int UpperInt { get; set; }
        public int LowerInt { get; set; }
    }
}

Since casting doesn't work, how do I convert range to a LinkedListNode<T>?

Ait answered 13/5, 2012 at 4:58 Comment(6)
Suppose you have the node. What you expect to do with it?Ulrica
My intent is to do ranges.AddAfter(recentNode, something)Ait
@makerofthings7: For starters, that is not going to work during an enumeration.Witchhunt
@makerofthings7: Looking at the MSDN docs, it appears you want a something to iterate over the nodes instead of the values. Something like the Find and FindLast methods? Except not quite.Witchhunt
@Witchhunt - I think Find will do it. Perhaps my class of T needs to have something implemented ICompareable<T>?Ait
First off, if you want to iterate through the nodes, you have to use the actual links of the LinkedList (ranges.First, range.Next etc.). LinkedList implements IEnumerable<T>, not IEnumerable<LinkedListNode<T>> so the objects you are iterating through have no relation to the other nodes. Second, what you are doing would technically modify a collection while it is iterating that collection and that'll throw an error.Barabbas
W
12

I think you are looking for;

var ranges = new LinkedList<ContiguousDataValue>();

for (var recentNode = ranges.First;
     recentNode != null;
     recentNode = recentNode.Next)
{
  var range = recentNode.Value; 
  ranges.AddAfter(recentNode,someNewNode);
}
Witchhunt answered 13/5, 2012 at 5:18 Comment(4)
I expanded the code in my question. I'm using a linked list to identify contiguous number ranges sent out of order (but mostly in increasing order). I'll have to think if this can help.Ait
Find should work if all the values are unique, but hardly efficient.Witchhunt
@makerofthings7: I slightly modified the example, so you can use it as a drop in replacement for foreach.Witchhunt
Aahh, so a for loop allows the better casting, and a foreach is only for type of T. Leppie, is it common for a "foreach" type to differ from the type returned in the "for" loop?Ait

© 2022 - 2024 — McMap. All rights reserved.