Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the LinkedList in .NET a circular linked list?

I need a circular linked list, so I am wondering if LinkedList is a circular linked list?

like image 457
Joan Venge Avatar asked Jun 22 '09 16:06

Joan Venge


People also ask

Are linked lists circular?

A linked list is called circular if it is not NULL-terminated and all nodes are connected in the form of a cycle. Below is an example of a circular linked list. An empty linked list is considered as circular.

What is the difference between linked list and circular linked list?

A circular linked list is a variation of a singly linked list. The only difference between the singly linked list and a circular linked list is that the last node does not point to any node in a singly linked list, so its link part contains a NULL value.

Is .NET list a linked list?

Essentially, a List<> in . NET is a wrapper over an array. A LinkedList<> is a linked list.

Is linked list cyclic?

Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.


5 Answers

A quick solution to using it in a circular fashion, whenever you want to move the "next" piece in the list:

current = current.Next ?? current.List.First; 

Where current is LinkedListNode<T>.

like image 199
Ady Kemp Avatar answered Sep 18 '22 06:09

Ady Kemp


No. It is a doubly linked list, but not a circular linked list. See MSDN for details on this.

LinkedList<T> makes a good foundation for your own circular linked list, however. But it does have a definite First and Last property, and will not enumerate around these, which a proper circular linked list will.

like image 32
Reed Copsey Avatar answered Sep 20 '22 06:09

Reed Copsey


While the public API of the LinkedList is not circular, internally it actually is. Consulting the reference source, you can see how it's implemented:

// This LinkedList is a doubly-Linked circular list.
internal LinkedListNode<T> head;

Of course, to hide the fact that it's circular, properties and methods that traverse the list make checks to prevent wrapping back to the head.

LinkedListNode:

public LinkedListNode<T> Next {
    get { return next == null || next == list.head? null: next;}
}

public LinkedListNode<T> Previous {
    get { return prev == null || this == list.head? null: prev;}
}

LinkedList.Enumerator:

public bool MoveNext() {
    if (version != list.version) {
        throw new InvalidOperationException(SR.GetString(SR.InvalidOperation_EnumFailedVersion));
    }

    if (node == null) {
        index = list.Count + 1;
        return false;
    }

    ++index;
    current = node.item;   
    node = node.next;  
    if (node == list.head) {
        node = null;
    }
    return true;
}
like image 26
Arturo Torres Sánchez Avatar answered Sep 22 '22 06:09

Arturo Torres Sánchez


If you need a circular data structure, have a look at the C5 generic collections library. They have any collection that's imaginably useful in there, including a circular queue (which might help you).

like image 44
Jacob Avatar answered Sep 19 '22 06:09

Jacob


No, its not. See MSDN

like image 38
Lucas Jones Avatar answered Sep 20 '22 06:09

Lucas Jones