LinkedList<T> in C#
DevSniper
190 Views
Namespace :- System.Collections.Generic
Assembly :- System.Collections.dll
Signature :-
public class LinkedList<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
- Allocation of LinkedList objects takes place on the heap.
- LinkedList can have duplicate elements.
- LinkedList provide provision to add or remove element at before/begin/front or last/end.
- The type of each LinkedList<T> node is LinkedListNode<T>.
- Generic LinkedList<T> is a doubly linked list.
- The First and Last properties contain null If the LinkedList is empty.
- LinkedList is not Thread Safe.
LinkedList<T> Constructors
- public LinkedList ();
- public LinkedList (System.Collections.Generic.IEnumerable<T> collection);
LinkedList<T> Properties
- Count :- public int Count { get; }Gets the number of elements that are actually present in the LinkedList<T>.
- First :- public System.Collections.Generic.LinkedListNode<T>? First { get; }Gets the first node of the LinkedList<T>.
- Last :- public System.Collections.Generic.LinkedListNode<T>? Last { get; }Gets the last node of the LinkedList<T>.
Creating and adding element in LinkedList<T>
- you can not create LinkedList<T> object using Collection initializer,unlike List<T>.
using System; using System.Collections.Generic; class Program { static void Main() { var linkedlist = new LinkedList<string>(); linkedlist.AddLast("Python"); linkedlist.AddLast("Java"); linkedlist.AddLast("C#"); linkedlist.AddFirst("React"); linkedlist.AddFirst("C++"); linkedlist.AddLast("Go"); foreach (var lst in linkedlist) { Console.WriteLine(lst); } } }
C++ React Python Java C# Go