Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to create a LinkedList
A LinkedList in C# is a generic collection that stores elements in nodes, where each node contains the data and references to the next and previous nodes. Unlike arrays, LinkedList elements are not stored in contiguous memory locations and allow efficient insertion and removal operations.
Syntax
Following is the syntax for creating a LinkedList −
LinkedList<T> listName = new LinkedList<T>();
You can also create a LinkedList from an existing collection −
LinkedList<T> listName = new LinkedList<T>(collection);
Creating LinkedList from Array
You can create a LinkedList by passing an array to its constructor. This automatically adds all array elements as nodes to the LinkedList −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] students = {"Tim","Jack","Henry","David","Tom"};
LinkedList<string> list = new LinkedList<string>(students);
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}
The output of the above code is −
Tim Jack Henry David Tom
Creating Empty LinkedList and Adding Elements
You can create an empty LinkedList and add elements using methods like AddFirst(), AddLast(), and AddAfter() −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<int> numbers = new LinkedList<int>();
numbers.AddFirst(10);
numbers.AddLast(20);
numbers.AddLast(30);
numbers.AddFirst(5);
Console.WriteLine("LinkedList elements:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine("\nFirst element: " + numbers.First.Value);
Console.WriteLine("Last element: " + numbers.Last.Value);
}
}
The output of the above code is −
LinkedList elements: 5 10 20 30 First element: 5 Last element: 30
Common LinkedList Operations
LinkedList provides various methods for manipulating nodes. Here's an example demonstrating insertion, removal, and search operations −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<string> colors = new LinkedList<string>();
// Adding elements
colors.AddLast("Red");
colors.AddLast("Blue");
colors.AddLast("Green");
// Insert after specific node
LinkedListNode<string> blueNode = colors.Find("Blue");
colors.AddAfter(blueNode, "Yellow");
Console.WriteLine("After adding Yellow after Blue:");
foreach (string color in colors) {
Console.WriteLine(color);
}
// Remove specific element
colors.Remove("Blue");
Console.WriteLine("\nAfter removing Blue:");
foreach (string color in colors) {
Console.WriteLine(color);
}
Console.WriteLine("\nTotal count: " + colors.Count);
}
}
The output of the above code is −
After adding Yellow after Blue: Red Blue Yellow Green After removing Blue: Red Yellow Green Total count: 3
Key Properties and Methods
| Property/Method | Description |
|---|---|
Count |
Gets the number of nodes in the LinkedList |
First |
Gets the first node of the LinkedList |
Last |
Gets the last node of the LinkedList |
AddFirst() |
Adds a node at the beginning |
AddLast() |
Adds a node at the end |
Remove() |
Removes the first occurrence of a value |
Find() |
Finds the first node containing the specified value |
Conclusion
LinkedList in C# provides a doubly-linked list implementation that allows efficient insertion and removal operations at any position. It's particularly useful when you need frequent insertions and deletions, as these operations have O(1) time complexity when you have a reference to the node.
