top

Search

C# Tutorial

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.Constructors in LinkedList CollectionThe different constructors and their description is given as follows:Table: Constructors in LinkedList Collection in C#Source: MSDNConstructorsDescriptionLinkedList<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.Properties in LinkedList CollectionThe different properties and their description is given as follows:Table: Properties in LinkedList Collection in C#Source: MSDNPropertiesDescriptionCountThis property gets the number of nodes actually contained in the LinkedList<T>.FirstThis property gets the first node of the LinkedList<T>.LastThis property gets the last node of the LinkedList<T>.Methods in LinkedList CollectionThe different methods and their description is given as follows:Table: Methods in LinkedList Collection in C#Source: MSDNMethodsDescriptionAddAfter(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.Create a Linked ListBefore 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 E0006Linked List OperationsSome of the Linked List operations are given as follows:Adding elements in Linked ListAn 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 1Deleting elements from Linked ListAn 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 9Finding an element in Linked ListThe 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: FalseAdd a node at the first position in a Linked ListUse 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 Smartwatchesusing 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);          }    } }Add a node at the last position in a Linked ListUse 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 PDAsAdd a node after the given node in a Linked ListIf 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 SmartwatchesAdd a node before the given node in a Linked ListIf 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 SmartwatchesCheck whether a node is in a Linked List or notTo 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?: TrueRemove the first occurrence of a node in a Linked ListUse 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 SmartphonesRemove a node at the end of the Linked ListUse 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 SmartwatchesRemove a node at the beginning of a Linked ListUse 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 LaptopClear a Linked ListTo 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
logo

C# Tutorial

LinkedList Collection in C#

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.

Constructors in LinkedList Collection

The different constructors and their description is given as follows:

Table: Constructors in LinkedList Collection in C#

Source: MSDN

ConstructorsDescription
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.

Properties in LinkedList Collection

The different properties and their description is given as follows:

Table: Properties in LinkedList Collection in C#

Source: MSDN

PropertiesDescription
CountThis property gets the number of nodes actually contained in the LinkedList<T>.
FirstThis property gets the first node of the LinkedList<T>.
LastThis property gets the last node of the LinkedList<T>.

Methods in LinkedList Collection

The different methods and their description is given as follows:

Table: Methods in LinkedList Collection in C#

Source: MSDN

MethodsDescription
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.

Create a Linked List

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

Linked List Operations

Some of the Linked List operations are given as follows:

Adding elements in Linked List

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

Deleting elements from Linked List

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

Finding an element in Linked List

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

Add a node at the first position in a Linked List

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);  
       }
   }
}

Add a node at the last position in a Linked List

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

Add a node after the given node in a Linked List

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

Add a node before the given node in a Linked List

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

Check whether a node is in a Linked List or not

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

Remove the first occurrence of a node in a Linked List

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

Remove a node at the end of the Linked List

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

Remove a node at the beginning of a Linked List

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

Clear a Linked List

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial