07 Feb 2024
LinkedList Generic Collections in C#
Definition:
LinkedList<T> is a generic collection class in the System.Collections.Generic namespace of C#. It represents a doubly linked list, where each element in the list is a node containing both data and references to the previous and next nodes. The T represents the type of elements stored in the linked list.
Key Features and Characteristics:
- Doubly Linked List: Each element in a LinkedList is a node containing data and references to the previous and next nodes, allowing bidirectional traversal.
- Generic Type Parameter: LinkedList is generic, allowing it to store elements of any data type specified by the developer.
- Dynamic Sizing: LinkedList can dynamically grow or shrink in size as elements are added or removed.
- Efficient Insertions and Deletions: Insertions and deletions at the beginning or end of the list are efficient operations, especially when compared to arrays.
- No Random Access: Unlike arrays or lists, LinkedList does not support random access to elements by index. Traversal is required to access elements at specific positions.
- Methods and Properties: LinkedList provides a variety of methods and properties for adding, removing, and accessing elements. Some common methods include AddFirst, AddLast, RemoveFirst, RemoveLast, AddAfter, AddBefore, Find, Remove, and more.
Uses:
- Modeling scenarios where efficient insertion and deletion operations are critical.
- Implementing data structures such as queues, stacks, and adjacency lists for graphs.
- Managing data with variable lengths or where elements are frequently added or removed.
Advantages:
- Efficient for scenarios requiring frequent insertions and deletions, especially at the beginning or end of the list.
- Supports bidirectional traversal, allowing efficient backward and forward iteration through elements.
- Dynamic resizing capability allows LinkedList to efficiently handle varying numbers of elements.
Disadvantages:
- Not suitable for scenarios requiring random access to elements by index.
- Higher memory overhead compared to arrays due to the additional references stored for each node.
- Traversal is required to access elements at specific positions, which can lead to slower access times compared to arrays.
Code Example:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Creating a LinkedList of strings
LinkedList<string> linkedList = new LinkedList<string>();
// Adding elements to the end of the linked list
linkedList.AddLast("Apple");
linkedList.AddLast("Banana");
linkedList.AddLast("Orange");
// Adding elements to the beginning of the linked list
linkedList.AddFirst("Pineapple");
// Removing elements from the end of the linked list
linkedList.RemoveLast();
// Removing elements from the beginning of the linked list
linkedList.RemoveFirst();
// Inserting an element after a specific node
LinkedListNode<string> node = linkedList.Find("Banana");
linkedList.AddAfter(node, "Grapes");
// Displaying the elements in the linked list
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
}
}
Explanation: This example demonstrates creating a LinkedList<string> and performing various operations on it. Elements are added to the end and beginning of the linked list using AddLast and AddFirst methods, respectively. Elements are removed from the end and beginning using RemoveLast and RemoveFirst methods. An element is inserted after a specific node using AddAfter method. Finally, a foreach loop is used to iterate over the elements in the linked list and print them.