The LinkedList Collection represents a doubly linked list. It allows insertion, deletion and other operations from the linked list. This collection is found in System.Collections.Generic namespace.
The different constructors and their description is given as follows:
Table: Constructors in LinkedList Collection in C#
Source: MSDN
Constructors | Description |
---|---|
LinkedList<T>() | This constructor initializes a new instance of the LinkedList<T> class that is empty. |
LinkedList<T>(IEnumerable<T>) | This constructor initializes a new instance of the LinkedList<T> class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied. |
LinkedList<T>(SerializationInfo, StreamingContext) | This constructor initializes a new instance of the LinkedList<T> class that is serializable with the specified SerializationInfo and StreamingContext. |
The different properties and their description is given as follows:
Table: Properties in LinkedList Collection in C#
Source: MSDN
Properties | Description |
---|---|
Count | This property gets the number of nodes actually contained in the LinkedList<T>. |
First | This property gets the first node of the LinkedList<T>. |
Last | This property gets the last node of the LinkedList<T>. |
The different methods and their description is given as follows:
Table: Methods in LinkedList Collection in C#
Source: MSDN
Methods | Description |
---|---|
AddAfter(LinkedListNode<T>, LinkedListNode<T>) | This method adds the specified new node after the specified existing node in the LinkedList<T>. |
AddAfter(LinkedListNode<T>, T) | This method adds a new node containing the specified value after the specified existing node in the LinkedList<T>. |
AddBefore(LinkedListNode<T>, LinkedListNode<T>) | This method adds the specified new node before the specified existing node in the LinkedList<T>. |
AddBefore(LinkedListNode<T>, T) | This method adds a new node containing the specified value before the specified existing node in the LinkedList<T>. |
AddFirst(LinkedListNode<T>) | This method adds the specified new node at the start of the LinkedList<T>. |
AddFirst(T) | This method adds a new node containing the specified value at the start of the LinkedList<T>. |
AddLast(LinkedListNode<T>) | This method adds the specified new node at the end of the LinkedList<T>. |
AddLast(T) | This method adds a new node containing the specified value at the end of the LinkedList<T>. |
Clear() | This method removes all the nodes from the LinkedList<T>. |
Contains(T) | This method determines whether a value is in the LinkedList<T>. |
Equals(Object) | This method determines whether the specified object is equal to the current object. |
Find(T) | This method finds the first node that contains the specified value. |
FindLast(T) | This method finds the last node that contains the specified value. |
GetHashCode() | This method serves as the default hash function. |
Remove(LinkedListNode<T>) | This method removes the specified node from the LinkedList<T>. |
Remove(T) | This method removes the first occurrence of the specified value from the LinkedList<T>. |
RemoveFirst() | This method removes the node at the start of the LinkedList<T>. |
RemoveLast() | This method removes the node at the end of the LinkedList<T>. |
ToString() | This method returns a string that represents the current object. |
Before beginning with the Linked List operations, let us first see how to create a simple Linked List:
Source Code: Program to create a Linked List in C#
using System; using System.Collections.Generic; class Example { static void Main() { string [] empID = {"E0001","E0005","E0006"}; Console.WriteLine("Displaying elements of a Linked List: "); LinkedList<string> myList = new LinkedList<string>(empID); foreach (var res in myList) { Console.WriteLine(res); } } }
The output of the above program is as follows:
Displaying elements of a Linked List: E0001 E0005 E0006
Some of the Linked List operations are given as follows:
An element is added to the start of the Linked List using the AddFirst() method. Similarly, the AddLast() method can be used to add the element to the end of the linked list. The program that demonstrates this is given as follows:
Source Code: Program to add elements in Linked List in C#
using System; using System.Collections.Generic; namespace LinkedListDemo { class Example { static void Main(string[] args) { LinkedList <int> L = new LinkedList<int>(); L.AddFirst(5); L.AddFirst(2); L.AddFirst(8); L.AddLast(4); L.AddLast(9); L.AddLast(1); Console.Write("Linked List elements are: "); foreach (int i in L) { Console.Write(i + " "); } } } }
The output of the above program is as follows:
Linked List elements are: 8 2 5 4 9 1
An element can be deleted from the start of the Linked List using the RemoveFirst() method. Similarly, the RemoveLast() method can be used to delete the element from the end of the linked list. The program that demonstrates this is given as follows:
Source Code: Program to delete elements from Linked List in C#
using System; using System.Collections.Generic; namespace LinkedListDemo { class Example { static void Main(string[] args) { LinkedList <int> L = new LinkedList<int>(); L.AddFirst(5); L.AddFirst(2); L.AddFirst(8); L.AddLast(4); L.AddLast(9); L.AddLast(1); Console.Write("Original Linked List: "); foreach (int i in L) { Console.Write(i + " "); } L.RemoveFirst(); L.RemoveLast(); Console.WriteLine(); Console.Write("Linked List after deletion: "); foreach (int i in L) { Console.Write(i + " "); } } } }
The output of the above program is as follows:
Original Linked List: 8 2 5 4 9 1 Linked List after deletion: 2 5 4 9
The Contains method finds if an element is present in Linked List or not. If it is present, Contains() returns TRUE and otherwise it returns FALSE.
The program that demonstrates this is given as follows:
Source Code: Program to find an element in Linked List in C#
using System; using System.Collections.Generic; namespace LinkedListDemo { class Example { static void Main(string[] args) { LinkedList <int> list = new LinkedList<int>(); list.AddLast(4); list.AddLast(9); list.AddLast(1); list.AddLast(3); list.AddLast(6); list.AddLast(2); Console.Write("Linked List: "); foreach (int i in list ) { Console.Write(i + " "); } Console.WriteLine(); Console.WriteLine("The value 3 is present in Linked List: " + list.Contains(3)); Console.WriteLine("The value 5 is present in Linked List: " + list.Contains(5)); } } }
The output of the above program is as follows:
Linked List: 4 9 1 3 6 2 The value 3 is present in Linked List: True The value 5 is present in Linked List: False
Use the AddFirst() method to add a node at the first position in a Linked List. Let us see how:
Source Code: Program to add a node at the first position in a Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); myList.AddFirst("Calculators"); foreach (var d in myList) { Console.WriteLine(d); } } }
The output of the above program is as follows:
Smartphones Smartwatches New list... Calculators Smartphones Smartwatches
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); myList.AddFirst("Calculators"); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); myList.AddLast("PDAs"); foreach (var d in myList) { Console.WriteLine(d); } } }
Use the AddLast() method to add a node at the last position in a Linked List. Let us see how:
Source Code: Program to add a node at the last position in a Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); myList.AddFirst("Calculators"); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); myList.AddLast("PDAs"); foreach (var d in myList) { Console.WriteLine(d); } } }
The above program gives the following output:
Smartphones Smartwatches New list... Calculators Smartphones Smartwatches New list... Calculators Smartphones Smartwatches PDAs
If you want to add a node after the given node, use the AddAfter() method. Let us see an example:
Source Code: Program to add a node after the given node in a Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); var n = myList.AddFirst("Calculators"); myList.AddAfter(n, "PDAs"); foreach (var d in myList) { Console.WriteLine(d); } } }
The output of the above program is as follows:
Smartphones Smartwatches New list... Calculators PDAs Smartphones Smartwatches
If you want to add a node before the given node, use the AddBefore() method. Let us see an example:
Source Code: Program to add a node before the given node in a Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); var n = myList.AddFirst("Calculators"); myList.AddBefore(n, "PDAs"); foreach (var d in myList) { Console.WriteLine(d); } } }
The above program gives the following output:
Smartphones Smartwatches New list... PDAs Calculators Smartphones Smartwatches
To check whether a node is in a Linked List or not, the best way is to use the Contains() method:
Source Code: Program to check whether a node is in a Linked List or not in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("Smartphones in the list?: "+myList.Contains("Smartphones")); } }
The output:
Smartphones Smartwatches Smartphones in the list?: True
Use the Remove() method to remove the first occurrence of a node. Let us see an example:
Source Code: Program to remove the first occurrence of a node in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); var n = myList.AddFirst("Calculators"); myList.AddBefore(n, "PDAs"); foreach (var d in myList) { Console.WriteLine(d); } myList.Remove("Smartwatches"); Console.WriteLine("\nNew list after removing an element..."); foreach (var d in myList) { Console.WriteLine(d); } } }
The above program gives the following output:
Smartphones Smartwatches New list... PDAs Calculators Smartphones Smartwatches New list after removing an element... PDAs Calculators Smartphones
Use the RemoveLast() method to remove a node at the end of the Linked List.
The following is an example:
Source Code: Program to remove a node at the end of the Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Smartphones","Smartwatches", "Laptop"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); var n = myList.AddFirst("Calculators"); myList.AddBefore(n, "PDAs"); foreach (var d in myList) { Console.WriteLine(d); } myList.RemoveLast(); Console.WriteLine("\nNew list after removing the last element..."); foreach (var d in myList) { Console.WriteLine(d); } } }
The above program gives the following output:
Smartphones Smartwatches Laptop New list... PDAs Calculators Smartphones Smartwatches Laptop New list after removing the last element... PDAs Calculators Smartphones Smartwatches
Use the RemoveFirst() method to remove a node at the beginning of a Linked List. The following is an example demonstrating the same:
Source Code: Program to remove a node at the beginning of the Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Monitors","Headphones", "Laptop"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } Console.WriteLine("\nNew list..."); var n = myList.AddFirst("Printer"); myList.AddBefore(n, "PDAs"); foreach (var d in myList) { Console.WriteLine(d); } myList.RemoveFirst(); Console.WriteLine("\nNew list after removing the first node..."); foreach (var d in myList) { Console.WriteLine(d); } } }
The above program gives the following output:
Monitors Headphones Laptop New list... PDAs Printer Monitors Headphones Laptop New list after removing the first node... Printer Monitors Headphones Laptop
To clear a Linked List, use the Clear() method. Let us see an example:
Source Code: Program to clear a Linked List in C#
using System; using System.Collections.Generic; class Demo { static void Main() { string [] devices = {"Monitors","Headphones", "Laptop"}; LinkedList<string> myList = new LinkedList<string>(devices); foreach (var d in myList) { Console.WriteLine(d); } myList.Clear(); Console.WriteLine("\nLinked List cleared..."); Console.WriteLine("\nNew list..."); var n = myList.AddFirst("Printer"); myList.AddBefore(n, "PDAs"); foreach (var d in myList) { Console.WriteLine(d); } } }
The above program gives the following output:
Monitors Headphones Laptop Linked List cleared... New list... PDAs Printer
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *