![]() |
VOOZH | about |
LinkedList<T> class in C# is the part of the removal namespace. This generic type allows fast inserting and removal of elements. It implements a classic linked list. Each object is separately allocated. In the LinkedList, certain operations do not require the whole collection to be copied. But in many common cases, LinkedList hinders performance.
Example: This example demonstrates how to create a LinkedList, add elements using AddLast(), and print them using the for-each loop.
One Two Three
In C#, the LinkedList is declared as:
LinkedList<T> linkedList = new LinkedList<T>();
| Constructor | Description |
|---|---|
| LinkedList() | Initializes a new instance of the LinkedList class that is empty. |
| LinkedList(IEnumerable) | Initializes a new instance of the LinkedList class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied. |
| LinkedList(SerializationInfo, StreamingContext) | Initializes a new instance of the LinkedList class that is serializable with the specified SerializationInfo and StreamingContext. |
Example: This example demonstrates how to check if the list is empty or not using Count property.
LinkedList is not empty
The LinkedList<T>class provides several properties to access its state.
| Properties | Description |
|---|---|
| Count | Gets the number of nodes actually contained in the LinkedList. |
| First | Gets the first node of the LinkedList. |
| Last | Gets the last node of the LinkedList. |
Example: This example, demonstrates how to access the Count, First and Last properities.
Count: 4 First: Java Last: Python
| Method | Description |
|---|---|
| AddAfter() | Adds a new node or value after an existing node in the LinkedList. |
| AddBefore() | Adds a new node or value before an existing node in the LinkedList. |
| AddFirst() | Adds a new node or value at the start of the LinkedList. |
| AddLast() | Adds a new node or value at the end of the LinkedList. |
| Clear() | Removes all nodes from the LinkedList. |
| Contains(T) | Determines whether a value is in the LinkedList. |
| CopyTo(T[], Int32) | Copies the entire LinkedList to a compatible one-dimensional Array, starting at the specified index of the target array. |
| Equals(Object) | Determines whether the specified object is equal to the current object. |
| Find(T) | Finds the first node that contains the specified value. |
| FindLast(T) | Finds the last node that contains the specified value. |
| GetEnumerator() | Returns an enumerator that iterates through the LinkedList. |
| GetHashCode() | Serves as the default hash function. |
| GetObjectData(SerializationInfo, StreamingContext) | Implements the ISerializable interface and returns the data needed to serialize the LinkedList instance. |
| GetType() | Gets the Type of the current instance. |
| MemberwiseClone() | Creates a shallow copy of the current Object. |
| OnDeserialization(Object) | Implements the ISerializable interface and raises the deserialization event when the deserialization is complete. |
| Remove(LinkedListNode) | Removes the specified node from the LinkedList. |
| Remove(T) | Removes the first occurrence of the specified value from the LinkedList. |
| RemoveFirst() | Removes the node at the start of the LinkedList. |
| RemoveLast() | Removes the node at the end of the LinkedList. |
| ToString() | Returns a string that represents the current object. |
Example 1: This example demonstrates how to check if the specified element present in the list using the Contains() method.
Element B is presene in the List: True
Example 2: This example demonstrates how to remove the first node using Remove() and display the list before and after removal.
Total nodes in LinkedList are : 4 2 4 6 8 Total nodes in LinkedList are : 3 4 6 8