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

makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • Suppose you have the node. What you expect to do with it? – Ivo May 13 '12 at 05:02
  • 1
    My intent is to do `ranges.AddAfter(recentNode, something)` – makerofthings7 May 13 '12 at 05:03
  • @makerofthings7: For starters, that is not going to work during an enumeration. – leppie May 13 '12 at 05:07
  • @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. – leppie May 13 '12 at 05:14
  • @leppie - I think Find will do it. Perhaps my class of T needs to have something implemented ICompareable? – makerofthings7 May 13 '12 at 05:19
  • 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, not IEnumerable> 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. – Jacob Proffitt May 13 '12 at 05:39

1 Answers1

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);
}
leppie
  • 115,091
  • 17
  • 196
  • 297
  • 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. – makerofthings7 May 13 '12 at 05:22
  • `Find` should work if all the values are unique, but hardly efficient. – leppie May 13 '12 at 05:23
  • @makerofthings7: I slightly modified the example, so you can use it as a drop in replacement for `foreach`. – leppie May 13 '12 at 05:25
  • 1
    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? – makerofthings7 May 13 '12 at 05:31