Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a LinkedListNode<T> from a LinkedList<T>

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

like image 208
makerofthings7 Avatar asked May 13 '12 04:05

makerofthings7


People also ask

How to delete a node in LinkedList in c#?

C# | Removing the specified node from the LinkedList<T>Remove(LinkedListNode<T>) method is used to remove the specified node from the LinkedList<T>. Syntax: public void Remove (System. Collections.

What is linked list in c sharp?

In C#, LinkedList is the generic type of collection which is defined in System. Collections. Generic namespace. It is a doubly linked list, therefore, each node points forward to the Next node and backward to the Previous node. It is a dynamic collection which grows, according to the need of your program.

What is node in c sharp?

It's a collection of elements. Element is called as Node . Each element has value(data) and reference of next node. The very first node is called as Head and last element has reference to null value.


1 Answers

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);
}
like image 134
leppie Avatar answered Sep 21 '22 05:09

leppie