0
0
C Sharp (C#)programming~7 mins

LinkedList usage in C Sharp (C#)

Choose your learning style9 modes available
Introduction

A linked list helps you store items in order, where each item points to the next one. It is useful when you want to add or remove items easily without moving everything.

When you want to add or remove items from the middle of a list quickly.
When you do not know the total number of items in advance.
When you want to keep items in order but expect frequent changes.
When you want to avoid the cost of shifting items in an array.
When you want to traverse items one by one in sequence.
Syntax
C Sharp (C#)
public class Node<T>
{
    public T Value { get; set; }
    public Node<T>? Next { get; set; }

    public Node(T value)
    {
        Value = value;
        Next = null;
    }
}

public class LinkedList<T>
{
    private Node<T>? head;

    public LinkedList()
    {
        head = null;
    }

    public void AddFirst(T value)
    {
        Node<T> newNode = new Node<T>(value);
        newNode.Next = head;
        head = newNode;
    }

    public void AddLast(T value)
    {
        Node<T> newNode = new Node<T>(value);
        if (head == null)
        {
            head = newNode;
            return;
        }

        Node<T> current = head;
        while (current.Next != null)
        {
            current = current.Next;
        }
        current.Next = newNode;
    }

    public bool Remove(T value)
    {
        if (head == null) return false;

        if (head.Value!.Equals(value))
        {
            head = head.Next;
            return true;
        }

        Node<T>? current = head;
        while (current.Next != null && !current.Next.Value!.Equals(value))
        {
            current = current.Next;
        }

        if (current.Next == null) return false;

        current.Next = current.Next.Next;
        return true;
    }

    public void PrintAll()
    {
        Node<T>? current = head;
        while (current != null)
        {
            Console.Write(current.Value + " ");
            current = current.Next;
        }
        Console.WriteLine();
    }
}

The Node<T> class holds the value and a reference to the next node.

The LinkedList<T> class manages the nodes starting from head.

Examples
This shows an empty linked list with no items.
C Sharp (C#)
// Empty list
LinkedList<int> emptyList = new LinkedList<int>();
emptyList.PrintAll(); // prints nothing
Adding one item at the start creates a list with one node.
C Sharp (C#)
// One element
LinkedList<string> singleItemList = new LinkedList<string>();
singleItemList.AddFirst("Hello");
singleItemList.PrintAll(); // prints: Hello
Adding items at the end appends nodes after the last one.
C Sharp (C#)
// Add at end
LinkedList<int> numbers = new LinkedList<int>();
numbers.AddFirst(1);
numbers.AddLast(2);
numbers.AddLast(3);
numbers.PrintAll(); // prints: 1 2 3
Removing a node with value 20 deletes it from the list.
C Sharp (C#)
// Remove element
LinkedList<int> list = new LinkedList<int>();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
list.Remove(20);
list.PrintAll(); // prints: 10 30
Sample Program

This program creates a linked list of integers. It shows the list when empty, adds items at the start and end, removes an item, and tries to remove a non-existing item. It prints the list after each step.

C Sharp (C#)
using System;

public class Node<T>
{
    public T Value { get; set; }
    public Node<T>? Next { get; set; }

    public Node(T value)
    {
        Value = value;
        Next = null;
    }
}

public class LinkedList<T>
{
    private Node<T>? head;

    public LinkedList()
    {
        head = null;
    }

    public void AddFirst(T value)
    {
        Node<T> newNode = new Node<T>(value);
        newNode.Next = head;
        head = newNode;
    }

    public void AddLast(T value)
    {
        Node<T> newNode = new Node<T>(value);
        if (head == null)
        {
            head = newNode;
            return;
        }

        Node<T> current = head;
        while (current.Next != null)
        {
            current = current.Next;
        }
        current.Next = newNode;
    }

    public bool Remove(T value)
    {
        if (head == null) return false;

        if (head.Value!.Equals(value))
        {
            head = head.Next;
            return true;
        }

        Node<T>? current = head;
        while (current.Next != null && !current.Next.Value!.Equals(value))
        {
            current = current.Next;
        }

        if (current.Next == null) return false;

        current.Next = current.Next.Next;
        return true;
    }

    public void PrintAll()
    {
        Node<T>? current = head;
        while (current != null)
        {
            Console.Write(current.Value + " ");
            current = current.Next;
        }
        Console.WriteLine();
    }
}

class Program
{
    static void Main()
    {
        LinkedList<int> numbers = new LinkedList<int>();

        Console.WriteLine("Initial list (empty):");
        numbers.PrintAll();

        numbers.AddFirst(5);
        numbers.AddLast(10);
        numbers.AddLast(15);

        Console.WriteLine("After adding 5 at start, then 10 and 15 at end:");
        numbers.PrintAll();

        bool removed = numbers.Remove(10);
        Console.WriteLine($"Removing 10: {removed}");
        numbers.PrintAll();

        removed = numbers.Remove(100);
        Console.WriteLine($"Trying to remove 100 (not in list): {removed}");
        numbers.PrintAll();
    }
}
OutputSuccess
Important Notes

Adding or removing at the start is very fast (constant time).

Adding at the end takes time proportional to the list length (linear time) if no tail pointer is used.

Removing requires searching, which takes linear time.

Common mistake: forgetting to update the head when removing the first node.

Use linked lists when you need fast insertions/removals and don't need fast random access.

Summary

A linked list stores items in nodes linked by references.

You can add items at the start or end, and remove items by value.

Linked lists are good for dynamic collections with frequent changes.