Efficiently Managing Data with LinkedList in C#
Written on
Understanding LinkedList in C#
The LinkedList class belongs to the System.Collections.Generic namespace and represents a doubly linked list. This data structure enables you to store and manipulate a collection of elements dynamically. Unlike arrays or standard lists, a LinkedList does not necessitate contiguous memory allocation, which allows for more efficient insertion and deletion operations within the list.
Creating a LinkedList
To start using a LinkedList, you can leverage the System.Collections.Generic namespace. Below is a sample code snippet for creating a LinkedList:
using System;
using System.Collections.Generic;
LinkedList<int> linkedFruitPrices = new LinkedList<int>();
LinkedList<string> linkedFruitNames = new LinkedList<string>();
Adding Elements to the LinkedList
You can insert elements into a LinkedList using the AddLast method to append to the end, or the AddFirst method to prepend to the beginning. Here’s how you can do it:
linkedFruitPrices.AddLast(12);
linkedFruitPrices.AddLast(16);
linkedFruitPrices.AddLast(22);
linkedFruitPrices.AddFirst(2);
linkedFruitPrices.AddFirst(19);
linkedFruitNames.AddLast("Apple");
linkedFruitNames.AddLast("Mango");
linkedFruitNames.AddLast("Cherry");
linkedFruitNames.AddLast("Orange");
linkedFruitNames.AddFirst("Grape");
linkedFruitNames.AddFirst("Kiwi");
Accessing Elements in the LinkedList
To retrieve elements from a LinkedList, you can iterate through the collection using a foreach loop, or access the first and last elements directly via the First and Last properties:
int firstPrice = linkedFruitPrices.First.Value; // Returns 2
int lastPrice = linkedFruitPrices.Last.Value; // Returns 22
string firstName = linkedFruitNames.First.Value; // Returns Grape
string lastName = linkedFruitNames.Last.Value; // Returns Orange
Inserting and Removing Elements
You can add elements at specific positions using the AddBefore and AddAfter methods, and remove elements with the Remove method. Here’s an example:
var nodePrice = linkedFruitPrices.Find(16); // Locate the node with value 16
linkedFruitPrices.AddBefore(nodePrice, 6); // Inserts 6 before the node with value 16
linkedFruitPrices.AddAfter(nodePrice, 2); // Inserts 2 after the node with value 16
linkedFruitPrices.Remove(1); // Removes the node with value 1
var nodeName = linkedFruitNames.Find("Mango"); // Find the node with the value "Mango"
linkedFruitNames.AddBefore(nodeName, "Peach"); // Inserts "Peach" before "Mango"
linkedFruitNames.AddAfter(nodeName, "Papaya"); // Inserts "Papaya" after "Mango"
linkedFruitNames.Remove("Orange"); // Removes the node with value "Orange"
Checking if the LinkedList is Empty
You can determine if a LinkedList is empty by checking the Count property:
bool isEmptyPrices = linkedFruitPrices.Count == 0;
bool isEmptyNames = linkedFruitNames.Count == 0;
Iterating Through a LinkedList
To loop through the elements in a LinkedList, you can use the foreach statement as shown below:
foreach (int item in linkedFruitPrices)
{
Console.WriteLine(item); // Prints 12, 6, 16, 2, 22
}
foreach (string item in linkedFruitNames)
{
Console.WriteLine(item); // Prints Grape, Kiwi, Apple, Peach, Mango, Papaya, Cherry
}
Clearing the LinkedList
To remove all elements from a LinkedList, the Clear method can be utilized:
linkedFruitPrices.Clear(); // Removes all elements from the prices list
linkedFruitNames.Clear(); // Removes all elements from the names list
LinkedList Example Code
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
LinkedList<int> linkedFruitPrices = new LinkedList<int>();
LinkedList<string> linkedFruitNames = new LinkedList<string>();
linkedFruitPrices.AddLast(12);
linkedFruitPrices.AddLast(16);
linkedFruitPrices.AddLast(22);
linkedFruitPrices.AddFirst(2);
linkedFruitPrices.AddFirst(19);
linkedFruitNames.AddLast("Apple");
linkedFruitNames.AddLast("Mango");
linkedFruitNames.AddLast("Cherry");
linkedFruitNames.AddLast("Orange");
linkedFruitNames.AddFirst("Grape");
linkedFruitNames.AddFirst("Kiwi");
foreach (int price in linkedFruitPrices)
{
Console.WriteLine(price);}
foreach (string name in linkedFruitNames)
{
Console.WriteLine(name);}
}
}
Summary
LinkedLists are particularly advantageous when dynamic insertion or deletion of elements is required within a collection. They are frequently employed in situations necessitating flexible data manipulation, such as implementing stacks and queues, managing action histories, or maintaining sorted lists.
For further insightful tutorials, visit the C-Sharp Tutorial website. If you found this article helpful, please show your support by clapping and following the author.