Sunday, April 3, 2011

Creating a circually linked list in C#?

What would be the best way to create a circually linked list in C#. Should I derive it from the LinkedList< T> collection? I'm planning on creating a simple address book using this Linked List to store my contacts (it's gonna be a suck-y address book, but I don't care cause I'll be the only one to use it). I mainly just want to create the crucially linked list so that I can use it again in other projects.

If you don't think the Linked List is the right way to go let me know which way would be better.

From stackoverflow
  • I don't think a circular linked list is the right data structure for a contacts list. A simple List<> or Collection<> should suffice.

  • It would likely be a bad idea to derive from the BCL LinkedList class. That class is designed to be a non-circular list. Trying to make it circular will only cause you problems.

    You're probably much better off writing your own.

  • Do you have a specific requirement to use a circularly linked list (i.e. homework)? If not, I would suggest using the simple List<T> class to store your contacts.

  • How about this CList based circular list.

  • Circularly-linked lists are often implemented using arrays which makes them very fast and by their nature do not require dynamic resizing. You just need a quick check on the read and the write indexes to see if they fell off the end and if so, reset it to zero (or one, whatever).

    However, they are generally used for things like input buffers, where the data has no real value once read. Contact lists have lasting value and new contacts will overwrite older contacts once the list fills up, which might be ok unless you overwrite your grandmom who is leaving you a bunch of cash in her will.


    I do not think that a linked list is the most efficient way to go for a circular buffer (the original question).

    The purpose of a circular buffer is speed and an array simply cannot be beaten for speed in the context of a circular buffer. Even if you keep a pointer to your last accessed linked list item, an array will still be more efficient. Lists have dynamic resizing capabilities (overhead) that are unneeded for circular buffers. Having said that, I think a circular buffer is probably not the right structure for the application (contact list) you mention.

  • As most of these answers don't actually get at the substance of the question, merely the intention, perhaps this will help:

    As far as I can tell the only difference between a Linked List and a Circular Linked List is the behavior of iterators upon reaching the end or beginning of a list. A very easy way to support the behavior of a Circular Linked List is to write an extension method for a LinkedListNode that returns the next node in the list or the first one if no such node exists, and similarly for retrieving the previous node or the last one if no such node exists. The following code should accomplish that, although I haven't tested it:

    static class CircularLinkedList {
        public static LinkedListNode NextOrFirst(this LinkedListNode current) {
            if (current.Next == null)
                return current.List.First;
            return current.Next;
        }
    
        public static LinkedListNode PreviousOrLast(this LinkedListNode current) {
            if (current.Previous == null)
                return current.List.Last;
            return current.Previous;
        }
    }
    

    Now you can just call myNode.NextOrFirst() instead of myNode.Next and you will have all the behavior of a circular linked list. You can still do constant time removals and insert before and after all nodes in the list and the like. If there's some other key bit of a circular linked list I am missing, let me know.

  • @Clueless your suggested code worked for me. thanks.

    Hightechrider : Use `Add comment` to add a comment to an answer rather than posting a new answer.

0 comments:

Post a Comment