A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/linked-list-implementation-in-c-sharp/ below:

C# LinkedList - GeeksforGeeks

C# LinkedList

Last Updated : 11 Jul, 2025

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 list. In C#, LinkedList is the generic type of collection that is defined in the System.Collections.Generic namespace. It is a doubly linked list, therefore, each node points forward to the Next node and backward to the Previous node. It is a dynamic collection that grows, according to the needs of our program. It also provides fast inserting and removing elements

Example:

C#
// C# program to Add elements to a LinkedList
using System;
using System.Collections.Generic;

class Geeks {
    static void Main()
    {
        // Create a new LinkedList of strings
        LinkedList<int> l = new LinkedList<int>();

        // Add elements to the LinkedList

        // Adds at the end
        l.AddLast(10);
        // Adds at the beginning
        l.AddFirst(20);
        // Adds at the end
        l.AddLast(30);
        // Adds at the end
        l.AddLast(40);

        // Display the elements in the LinkedList
        Console.WriteLine("Elements in the LinkedList:");
        foreach(var i in l) { 
          Console.WriteLine(i); 
        }
    }
}

Output
Elements in the LinkedList:
20
10
30
40
Hierarchy of LinkedList Interfaces

In C#, the LinkedList<T> class implements the interfaces which are listed below:

Constructors

In C#, the LinkedList<T> class has 3 constructors which are used to create a LinkedList which are as follows:

Creating a LinkedList

Let’s see how to create an LinkedList using LinkedList() constructor:

 Step 1: Include System.Collections.Generic namespace in your program with the help of "using" keyword.

using System.Collections.Generic;

Step 2: Create a LinkedList using LinkedList class

LinkedList <String> l = new LinkedList <String>();

Performing Different Operations on LinkedList

1. Adding Elements: LinkedList class provides four different methods to insert nodes and these methods are listed below:

Example: This example demonstrates how to create a LinkedList<int>, adding elements to it usind AddLast() and displaying the elements using a foreach loop.

C#
// Creating and adding elements to the LinkedList
using System;
using System.Collections.Generic;

class Geeks {

    static void Main()
    {
        // Creating a linked l of integers
        LinkedList<int> l = new LinkedList<int>();

        // Adding elements to the LinkedList using AddLast()
        l.AddLast(10);
        l.AddLast(20);
        l.AddLast(30);
        l.AddLast(40);
        l.AddLast(50);

        Console.WriteLine("List of numbers:");

        // Accessing and displaying the elements using
        // foreach loop
        foreach(int num in l) { Console.WriteLine(num); }
    }
}

Output
List of numbers:
10
20
30
40
50

2. Removing Elements: LinkedList<T> class provides five different methods to remove elements and the methods are:

Example:

C#
// Remove elements from the LinkedList
using System;
using System.Collections.Generic;

class Geeks {

    static void Main()
    {
        // Creating a LinkedList of integers
        LinkedList<int> l = new LinkedList<int>();

        // Adding elements to the LinkedList using AddLast()
        l.AddLast(10);
        l.AddLast(20);
        l.AddLast(30);
        l.AddLast(40);
        l.AddLast(50);
        l.AddLast(60);

        // Initial list of numbers
        Console.WriteLine("Initial List of Numbers: "
                          + string.Join(" ", l));

        // Removing the first element using
        // Remove(LinkedListNode)
        l.Remove(l.First);
        Console.WriteLine(
            "\nAfter Removing the First Element: "
            + string.Join(" ", l));

        // Removing a specific element (20) using Remove(T)
        l.Remove(20);
        Console.WriteLine("\nAfter Removing Number 20: "
                          + string.Join(" ", l));

        // Removing the first element using RemoveFirst()
        l.RemoveFirst();
        Console.WriteLine(
            "\nAfter Removing the First Element Again: "
            + string.Join(" ", l));

        // Removing the last element using RemoveLast()
        l.RemoveLast();
        Console.WriteLine(
            "\nAfter Removing the Last Element: "
            + string.Join(" ", l));

        // Clearing the entire linkedlist
        l.Clear();
        Console.WriteLine(
            "\nNumber of elements in the list after clearing: "
            + l.Count);
    }
}

Output:

3. Checking the Availability of Elements in the LinkedList: LinkedList class provide Contains(T) method to check if the element is present in the LinkedList or not.

C#
// Checking the Availability of 
// Elements in the LinkedList
using System;
using System.Collections.Generic;

class Geeks {
    public static void Main(string[] args)
    {
        // Create a new LinkedList of integers
        LinkedList<int> l = new LinkedList<int>();

        // Add elements to the LinkedList using AddLast()
        l.AddLast(10);
        l.AddLast(20);
        l.AddLast(30);

        // Check if the element 20 is present in the LinkedList
        Console.WriteLine(
                "The element 20 is present in the LinkedList: "
                        + l.Contains(20));

        // Check if the element 100 is present in the LinkedList
        Console.WriteLine(
                "The element 100 is present in the LinkedList: "
                        + l.Contains(100));
    }
}

Output
The element 20 is present in the LinkedList: True
The element 100 is present in the LinkedList: False


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4