Linked List in C#

I was using a circular linked list in C and want to convert the code to C#.  What is the equivalent of a circular linked list in C#?  Thanks,
khybermanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
It's not there, but it's quite easy to program yourself. JUst inherit from LinkedList ort Arraylist or build it from scratch by inheriting from IList.
edin_el_bosnewiCommented:
There is no predefined linked list in C#.  You can however easily convert the code from C to C#.  If you need help, provide the code!
Bob LearnedCommented:
Here is what I have (not a circular linked list):

// Source URL:
// http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=176

using System;

namespace Utility.Structures.CSharp
{

  /// <summary>
  /// Class to represent one node in a list
  /// </summary>
  public class ListNode
  {
    private object _data;
    private ListNode _next;

    /// <summary>
    /// Constructor to Create ListNode that stores 'data'
    /// and is last node in list

    /// </summary>
    /// <param name="data"></param>
    public ListNode(object data)
    {
    }

    /// <summary>
    /// Constructor to Create ListNode that stores 'data'
    /// and refers to the next node in list.
    /// </summary>
    public ListNode(object data, ListNode next)
    {
      _data = data;
      _next = next;
    }


    /// <summary>
    /// Property Next refers to next node in list.
    /// </summary>
    public ListNode Next
    {
      get { return _next; }
      set { _next = value; }
    }

    /// <summary>
    /// Property Data returns the contents of ListNode
    /// </summary>
    public object Data
    {
      get { return _data; }
    }

  }

 
  public class LinkedList
  {

    private ListNode _firstNode = null;
    private ListNode _lastNode = null;
            
    private int _listCount = 0;

    /// <summary>
    /// IsEmpty Property Check if the list is Empty, returns true if list
    /// is Empty
    /// </summary>
    public bool IsEmpty
    {
      get
      {
        lock(this)
        {
          return _firstNode == null;
        }
      }
    }

    /// <summary>
    /// return number of elemets in List
    /// </summary>
    public int Count
    {
      get
      {
        return _listCount;
      }
    }

    /// <summary>
    /// return first element in List
    /// </summary>
    public ListNode First
    {
      get
      {
        return _firstNode;
      }
    }

    /// <summary>
    /// return last element in List
    /// </summary>
    public ListNode Last
    {
      get
      {
        return _lastNode;
      }
    }


    /// <summary>
    /// Insert Object at front of list. If list is empty, first node &
    /// last node will refer to same object. Otherwise, first node
    /// refers to new node
    /// </summary>
    /// <param name="item">Object value to be inserted</param>
    public void InsertAtFront(object item)
    {
      lock(this)
      {
        if(IsEmpty)
          _firstNode = _lastNode = new ListNode(item);
        else
          _firstNode = new ListNode(item, _firstNode);

        _listCount++;
      }
    }


    /// <summary>
    /// Insert Object at end of list. If list is empty, first node &
    /// last node will refer to same object. Otherwise, last node
    /// refers to new node
    /// </summary>
    /// <param name="item">Object value to be inserted</param>
    public void InsertAtBack(object item)
    {
      lock(this)
      {
        if(IsEmpty)
          _firstNode = _lastNode = new ListNode(item);
        else
          _lastNode = _lastNode.Next = new ListNode(item);

        _listCount++;
      }
    }


    /// <summary>
    /// Insert and object in specific place in list
    /// </summary>
    /// <param name="item">Object value to be inserted</param>
    /// <param name="Index">Index of the new object to be inserted</param>
    public void InsertAt(object item, int Index)
    {

      // Critical section.
      lock(this)
      {

        if (Index > _listCount || Index < 0)
          throw new ArgumentOutOfRangeException(Index.ToString());

        if (Index == 0)
          this.InsertAtFront(item);
        else if (Index == (_listCount - 1))
          this.InsertAtBack(item);
        else
        {

          // Start with the first node.
          ListNode currentNode = _firstNode;

          // Iterate through the list, until the
          // desired position is found.
          for (int i=1; i<Index; i++)
            currentNode = currentNode.Next;

          // Add a new node.
          ListNode newNode = new ListNode(item, currentNode.Next);
          currentNode.Next = newNode;

          // Increment the Count.
          _listCount++;

        }
      }
    }


    /// <summary>
    /// Remove first node from list.
    /// </summary>
    /// <returns></returns>
    public void RemoveFromFront()
    {

      // Critical section.
      lock(this)
      {

        // Reset first node and last node references.
        if (_firstNode == _lastNode)
          _firstNode = _lastNode = null;
        else
          _firstNode = _firstNode.Next;

        _listCount--;

      }
    }


    /// <summary>
    /// Remove last node from list.
    /// </summary>
    /// <returns></returns>
    public void RemoveFromBack()
    {

      // Critical section.
      lock(this)
      {

        // Reset first node and last node references.
        if (_firstNode == _lastNode)
          _firstNode = _lastNode = null;
        else
        {

          // Find the node before the last node in the list.
          ListNode currentNode = _firstNode;
          while (currentNode.Next != _lastNode)
            currentNode = currentNode.Next;

          // Move to last node back one slot.
          _lastNode = currentNode;

          // Mark the end of the list.
          currentNode.Next = null;
                              
        }

        // Decrement the Count.
        _listCount--;

      }
    }
  }

}

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.