C# List & LinkedList

HTML
CSS
C#
SQL

Lists and LinkedList

Dynamic Arrays:

Lists are dynamic arrays that automatically resize to accommodate elements.

They are part of the System.Collections.Generic namespace.

// Example of a List

List<int> numbers = new List<int>();

numbers.Add(1);

numbers.Add(2);

numbers.Add(3);

Key Features:

Lists provide methods for efficiently adding, removing, and accessing elements.

They offer dynamic resizing, making them suitable for scenarios where the size of the collection changes frequently.

// Adding and accessing elements in a List

List<string> colors = new List<string>();

colors.Add(“Red”);

colors.Add(“Green”);

string firstColor = colors[0];

Iterating Through a List:

Use loops to iterate through elements in a List.

List<char> letters = new List<char> { ‘A’, ‘B’, ‘C’ };

foreach (char letter in letters)

{

    Console.WriteLine(letter);

}

LinkedLists in C#

Linked Nodes:

LinkedLists consist of nodes, each containing a value and a reference to the next and/or previous node.

They are part of the System.Collections.Generic namespace.

// Example of a LinkedList

LinkedList<string> names = new LinkedList<string>();

names.AddLast(“Alice”);

names.AddLast(“Bob”);

Key Features:

LinkedLists excel in scenarios where frequent insertions or removals in the middle of the collection are required.

They offer efficient node-based operations.

// Inserting and removing nodes in a LinkedList

LinkedList<int> numbers = new LinkedList<int>();

LinkedListNode<int> node = numbers.AddLast(1);

numbers.AddAfter(node, 2);

numbers.Remove(node);

Iterating Through a LinkedList:

Use loops to iterate through nodes in a LinkedList.

LinkedList<char> characters = new LinkedList<char> { ‘X’, ‘Y’, ‘Z’ };

foreach (char character in characters)

{

    Console.WriteLine(character);

}

Use Lists When:

Frequent Access and Iteration:

Lists are efficient for scenarios where you need to access elements randomly or iterate through the entire collection frequently.

Dynamic Resizing is Needed:

Lists automatically resize to accommodate elements, making them suitable for scenarios with varying collection sizes.

Use LinkedLists When:

Frequent Insertions and Removals:

LinkedLists shine when you need to insert or remove elements frequently, especially in the middle of the collection.

Node-Based Operations are Essential:

If your operations involve manipulating nodes directly, LinkedLists provide efficient support for these scenarios.

Choose Based on Operations:

Consider the nature of your operations (access, insertions, removals) when selecting between Lists and LinkedLists.

// Good: Using a List for frequent access and iteration

List<double> prices = new List<double>();

 

// Good: Using a LinkedList for frequent insertions and removals

LinkedList<string> tasks = new LinkedList<string>();

Be Mindful of Trade-offs:

Recognize that each collection type comes with trade-offs. Lists provide efficient random access, while LinkedLists excel in node-based operations.

// Good: Choosing a List for efficiency in access

List<int> numbers = new List<int>();

 

// Good: Choosing a LinkedList for efficient insertions and removals

LinkedList<char> characters = new LinkedList<char>();

Practices Tasks

1. In the Main method create a new instance of a LinkedList named names to store strings.Add the following strings to the LinkedList: “Sonoo”, “Ankit”, “Peter”, and “Irfan” using the AddLast method. Next, locate the node containing the value “Peter” using the Find method and assign it to a variable named node. Use the AddBefore method to insert the string “John” before the node containing “Peter”. Similarly, use the AddAfter method to insert the string “Lucy” after the node containing “Peter”.Finally, iterate over each element in the LinkedList using a foreach loop. Inside the loop, print each element to the console.

OUTPUT 

2. Create a new instance of a LinkedList named “names” to store strings.Add the following strings to the LinkedList: “Sonoo”, “Ankit”, “Peter”, and “Irfan”. Use the AddLast method to add “Sonoo”, “Ankit”, and “Peter”, and use the AddFirst method to add “Irfan”. Locate the node containing the value “Peter” using the Find method and assign it to a variable named “node”. Remove the node containing “Peter” from the list using the Remove method. Iterate over each element in the LinkedList using a foreach loop. Inside the loop, print each element to the console using Console.WriteLine.

OUTPUT