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.
- LinkedList<T> is a general-purpose linked list. It supports enumerators.
- Each node in a LinkedList<T> object is of the type LinkedListNode<T> which contains data and references to the previous and next nodes.
- The Count property is maintained internally and can be accessed in constant time without needing to traverse the list.
- The LinkedList class does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.
Example: This example demonstrates how to create a LinkedList, add elements using AddLast(), and print them using the for-each loop.
C#
// C# program to add elements to a LinkedList
using System;
using System.Collections.Generic;
public class Geeks
{
public static void Main(string[] args)
{
// Creating a LinkedList
LinkedList<string> l = new LinkedList<string>();
// Adding elements to the LinkedList using AddLast() method
l.AddLast("One");
l.AddLast("Two");
l.AddLast("Three");
// Printing the LinkedList
foreach (var i in l)
{
Console.WriteLine(i);
}
}
}
Declaration of LinkedList
In C#, the LinkedList is declared as:
LinkedList<T> linkedList = new LinkedList<T>();
Constructors
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.
C#
// C# program to demonstrates
// how count property works
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Strings
LinkedList<String> l= new LinkedList<String>();
// Adding nodes in LinkedList
l.AddLast("Java");
l.AddLast("C++");
l.AddLast("C#");
l.AddLast("python");
// Checking LinkedList is empty or not
if (l.Count > 0)
Console.WriteLine("LinkedList is not empty");
else
Console.WriteLine("LinkedList is empty");
}
}
OutputLinkedList is not empty
Properties
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.
C#
// C# program to demonstrates how the LinkedList class
// properties like count, first and last works
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Strings and adding nodes
LinkedList<string> l = new LinkedList<string>();
l.AddLast("Java");
l.AddLast("C++");
l.AddLast("C#");
l.AddLast("Python");
// Displays number of nodes
Console.WriteLine("Count: " + l.Count);
// Displays first node value
Console.WriteLine("First: " + l.First.Value);
// Displays last node value
Console.WriteLine("Last: " + l.Last.Value);
}
}
OutputCount: 4
First: Java
Last: Python
Methods
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.
C#
// C# program to check if the specified
// element is present ot not
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Strings
LinkedList<String> l = new LinkedList<String>();
// Adding nodes in LinkedList
l.AddLast("A");
l.AddLast("B");
l.AddLast("C");
l.AddLast("D");
l.AddLast("E");
// To check if a value is in LinkedList
Console.WriteLine("Element B is presene in the List: "
+ l.Contains("B"));
}
}
OutputElement 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.
C#
// C# Program to demonstrates how to
// remove specific node from the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Creating a LinkedList of Integers
LinkedList<int> l = new LinkedList<int>();
// Adding nodes in LinkedList
l.AddLast(2);
l.AddLast(4);
l.AddLast(6);
l.AddLast(8);
// To get the count of nodes in LinkedList
// before removing all the nodes
Console.WriteLine("Total nodes in LinkedList are : "
+ l.Count);
// Displaying the nodes in LinkedList
foreach(int i in l) {
Console.WriteLine(i);
}
// Removing the first node from the LinkedList
l.Remove(l.First);
// To get the count of nodes in LinkedList
// after removing all the nodes
Console.WriteLine("Total nodes in LinkedList are : "
+ l.Count);
// Displaying the nodes in LinkedList
foreach(int i in l) {
Console.WriteLine(i);
}
}
}
OutputTotal nodes in LinkedList are : 4
2
4
6
8
Total nodes in LinkedList are : 3
4
6
8
Similar Reads
C# LinkedList In C# a LinkedList is a linear data structure that stores elements in a non-contiguous location. The elements in a linked list are linked with each other using pointers. In other words, LinkedList consists of nodes where each node contains a data field and a reference(link) to the next node in the l
5 min read
C# Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t
3 min read
C# Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th
4 min read
Java Program For Deleting A Given Node In Linked List Under Given Constraints Given a Singly Linked List, write a function to delete a given node. Your function must follow the following constraints: It must accept a pointer to the start node as the first parameter and node to be deleted as the second parameter i.e., a pointer to the head node is not global.It should not retu
3 min read
Linked List in C A linked list is a linear data structure where each element (called a node) is connected to the next one using pointers. Unlike array, elements of linked list are stored in random memory locations.In this article, we will learn about the linked list, its types, representation of the linked list in C
7 min read