Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a LinkedList but unfortunatelly the one that the .NET provides didn't have the functionality I was looking for (not working with linq and the LinkedListNode is sealed). I created a replacement that works similar but is linq-compatible.

The main class is a LinkedObject<T> that might be used on it's own but is better suited to be used by composition and should provide all the functionality of a chain:

[DebuggerDisplay("Value = {Value?.ToString()}")]
class LinkedObject<T>
{
    private LinkedObject<T> _previous;
    private LinkedObject<T> _next;

    public LinkedObject() {}

    public LinkedObject(T value)
    {
        Value = value;
    }

    public LinkedObject<T> Previous
    {
        get { return _previous; }
        set
        {
            _previous = value;
            value._next = this;
        }
    }

    public LinkedObject<T> Next
    {
        get { return _next; }
        set
        {
            _next = value;
            value._previous = this;
        }
    }

    public T Value { get; set; }

    // or maybe precessors?
    public IEnumerable<LinkedObject<T>> Before
    {
        get
        {
            var item = this;
            while (item != null)
            {
                yield return item;
                item = item.Previous;
            }
        }
    }

    // or maybe successors?
    public IEnumerable<LinkedObject<T>> After
    {
        get
        {
            var item = this;
            while (item != null)
            {
                yield return item;
                item = item.Next;
            }
        }
    }

    public void Remove()
    {            
        _previous.Next = _next;
        _next.Previous = _previous;
        _previous = null;
        _next = null;
    }
}

Some custom object would use it like this (my real linked-object has a few more additional properties then this simple example):

class Word
{
    private readonly LinkedObject<Sentence> _linkedObject;

    public Word()
    {
        _linkedObject = new LinkedObject<Sentence>(this);
    }

    public string Value 
    { 
        get { return _linkedObject.Value; } 
        set { _linkedObject.Value = value; }
    }        

    public WordPrevious
    {
        get { return _linkedObject.Previous.Value; }
        set { _linkedObject.Previous = value._linkedObject; }
    }

    public Word Next
    {
        get { return _linkedObject.Next.Value; }
        set { _linkedObject.Next = value._linkedObject; }
    }

    public IEnumerable<Word> Before
    {
        get { return _linkedObject.Before.Select(x => x.Value); }
    }

    public IEnumerable<Word> After
    {
        get { return _linkedObject.After.Select(x => x.Value); }
    }
}

Example (I use a loop to create the real chain):

var foo = new LinkedObject<string>
{
    Value = "foo",
    Next = new LinkedObject<string>
    {
        Value = "bar",
        Next = new LinkedObject<string>() { Value = "baz" }
    }
};

// foo, bar, baz

foo.Next.Remove(); // foo, baz
share|improve this question
    
Just out of curiousity, which parts of LINQ aren't working for you with the .NET built-in LinkedList? Everything I'm testing in VS2015 is working for me. – Ben C 2 days ago
    
@BenC it's returning the actual values instead of LinkedListNode so after you've got something with Where etc. you're left without the possibility to use Next or Previous... you'd need to do another search with the Find method to get the LinkedListNode again. – t3chb0t 2 days ago
    
Another example how you can use it with linq word.After.Last().Next = new Word() or any other place. You can add/insert items where ever you want by only acting on the items themself that build the actuall list instead of being held by such. – t3chb0t 2 days ago
    
Why can't you use the List specific methods in first place? Sounds a reasonable trade for me. – Bruno Costa 2 days ago
1  
@BrunoCosta I cannot use it becasue it requires the object it stores and I need to find elements by various properties. – t3chb0t 2 days ago

Rule your own Find extension method instead of redoing it all over again.

public static LinkedListNode<T> Find<T>(this LinkedList<T> list, Predicate<T> pred)
{
    var node = list.First;
    while (node != null)
    {
        if (pred(node.Value))
        {
            return node;
        }
        node = node.Next;
    }
    return null;
} 
share|improve this answer
    
Oh, was this simple ;-) I guess I have to rethink my idea... – t3chb0t 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.